猿问

Plotly - 如何将行数添加到折线图上的 Y 轴?

我正在尝试创建一个简单的折线图,其中 X 轴上包含日期字段,Y 轴上包含行数。我使用以下代码:


import plotly.express as px


data = {'Project':  ['Project 1', 'Project 2', ' Project 3', 'Project 4', 'Project 5', 'Project 6', 'Project 7', 'Project 8', 'Project 9', 'Project 10'],

        'Date': ['10/1/2020', '10/1/2020', '11/1/2020', '12/1/2020', '12/1/2020', '12/1/2020', '2/1/2021', '2/1/2021', '3/1/2021', '4/1/2021']}


df2 = pd.DataFrame(data, columns = ['Project','Date'])


fig = px.line(df2, x= "Date", y = "Project", title='<b>Project</b>')

fig.show()

但是,当我这样做时,项目名称位于 X 轴上,而不是每个日期的项目计数。


有谁知道如何添加行数,以便在折线图上显示每个日期的项目数?


慕容森
浏览 158回答 2
2回答

蛊毒传说

在发送到绘图之前,您需要groupby和countpandas 中的行进行比较。另外,您的示例没有显示它,但是如果您期望同一个月内有不同的日期并且您只关心年/月,那么您需要在分组之前应用一些舍入(或从日期中提取年和月data['Date'].dt.year,data['Date'].dt.month无论你喜欢哪个)。取这个稍微不同的样本,其中添加了 10/2import plotly.express as pxdata = {'Project':&nbsp; ['Project 1', 'Project 2', ' Project 3', 'Project 4', 'Project 5', 'Project 6', 'Project 7', 'Project 8', 'Project 9', 'Project 10'],&nbsp; &nbsp; &nbsp; &nbsp; 'Date': ['10/1/2020', '10/2/2020', '11/1/2020', '12/1/2020', '12/2/2020', '12/1/2020', '2/1/2021', '2/1/2021', '3/1/2021', '4/1/2021']}df2 = pd.DataFrame(data, columns = ['Project','Date'])df2['Date'] = pd.to_datetime(df2['Date'])df_grouped = (&nbsp; &nbsp; df2.groupby(&nbsp; &nbsp; &nbsp; &nbsp; # normalize all dates to start of month&nbsp; &nbsp; &nbsp; &nbsp; df2['Date'].astype('datetime64[M]')&nbsp; &nbsp; )['Project'].count().rename('Count').to_frame())df_grouped['Names'] = (&nbsp; &nbsp; df2.groupby(df2['Date'].astype('datetime64[M]')&nbsp; &nbsp; )['Project'].agg(',<br>&nbsp; &nbsp; '.join))print(df_grouped)fig = px.line(&nbsp; &nbsp; df_grouped, y='Count', title='<b>Projects per month</b>',&nbsp; &nbsp; hover_data=['Names'])fig.write_html('fig1.html', auto_open=True)更新:根据要求,此新代码在悬停时显示项目名称。输出&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Count&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NamesDate2020-10-01&nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Project 1,<br>&nbsp; &nbsp; Project 22020-11-01&nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Project 32020-12-01&nbsp; &nbsp; &nbsp; 3&nbsp; Project 4,<br>&nbsp; &nbsp; Project 5,<br>&nbsp; &nbsp; Project 62021-02-01&nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Project 7,<br>&nbsp; &nbsp; Project 82021-03-01&nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Project 92021-04-01&nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Project 10

倚天杖

您可以使用Matplotlib 的 hist轻松实现这一点例子:from datetime import datetimeimport matplotlibimport pandas as pdfrom matplotlib.pyplot import histdf = pd.DataFrame(    {'a': [1, 2, 3, 4]},     index=[datetime(2020, 9, 24),            datetime(2020, 9, 24),            datetime(2020, 9, 24),            datetime(2020, 9, 25)])hist(df.index)不是最漂亮的直方图,但我相信您可以从这里根据您的需要调整它;)
随时随地看视频慕课网APP

相关分类

Python
我要回答