绘图图:显示Bar-chart中的出现数量
我尝试从Givin DataFrame绘制一个栏形图。
- X轴=日期
- y轴=每个月的出现数量
应为Barchart。每个X都是出现的。
x | xx | x | ||
---|---|---|---|---|
2020-1 | 2020-2 | 2020-3 | 2020-4 | 2020-5 |
我尝试过,但没有得到以上所需的结果。
import datetime as dt
import pandas as pd
import numpy as np
import plotly.offline as pyo
import plotly.graph_objs as go
# initialize list of lists
data = [['a', '2022-01-05'], ['a', '2022-02-14'], ['a', '2022-02-15'],['a', '2022-05-14']]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Name', 'Date'])
# print dataframe.
df['Date']=pd.to_datetime(df['Date'])
# plot dataframe
trace1=go.Bar(
#
x = df.Date.dt.month,
y = df.Name.groupby(df.Date.dt.month).count()
)
data=[trace1]
fig=go.Figure(data=data)
pyo.plot(fig)
I try to plot a bar-chart from a givin dataframe.
- x-axis = dates
- y-axis = number of occurences for each month
The result should be a barchart. Each x is an occurrence.
x | xx | x | ||
---|---|---|---|---|
2020-1 | 2020-2 | 2020-3 | 2020-4 | 2020-5 |
I tried but don't get the desired result as above.
import datetime as dt
import pandas as pd
import numpy as np
import plotly.offline as pyo
import plotly.graph_objs as go
# initialize list of lists
data = [['a', '2022-01-05'], ['a', '2022-02-14'], ['a', '2022-02-15'],['a', '2022-05-14']]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Name', 'Date'])
# print dataframe.
df['Date']=pd.to_datetime(df['Date'])
# plot dataframe
trace1=go.Bar(
#
x = df.Date.dt.month,
y = df.Name.groupby(df.Date.dt.month).count()
)
data=[trace1]
fig=go.Figure(data=data)
pyo.plot(fig)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除最后一行,然后写:
图show()
编辑:
我尚不清楚您是否有1个维度或2个维度数据。假设您有1D数据,这只是您想要在条形图中汇总的一堆日期,只需做到这一点:
如果相反,您拥有2D数据,那么您想要的是散点图,而不是条形图。
Remove the last line and write instead:
fig.show()
Edit:
It's unclear to me whether you have 1 dimensional or 2 dimensional data here. Supposing you have 1d data, this is, just a bunch of dates that you want to aggregate in a bar chart, simply do this:
If, instead, you have 2d data then what you want is a scatter plot, not a bar chart.