绘图图:显示Bar-chart中的出现数量

发布于 2025-01-30 19:25:28 字数 1219 浏览 4 评论 0原文

我尝试从Givin DataFrame绘制一个栏形图。

  • X轴=日期
  • y轴=每个月的出现数量

应为Barchart。每个X都是出现的。

xxxx
2020-12020-22020-32020-42020-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.

xxxx
2020-12020-22020-32020-42020-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷︶言冷语的世界 2025-02-06 19:25:28

删除最后一行,然后写:
图show()

编辑:
我尚不清楚您是否有1个维度或2个维度数据。假设您有1D数据,这只是您想要在条形图中汇总的一堆日期,只需做到这一点:

# initialize list of lists
data = ['2022-01-05', '2022-02-14',  '2022-02-15', '2022-05-14']
# Create the pandas DataFrame
df = pd.DataFrame(data)
# plot dataframe 
fig = px.bar(df)

如果相反,您拥有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:

# initialize list of lists
data = ['2022-01-05', '2022-02-14',  '2022-02-15', '2022-05-14']
# Create the pandas DataFrame
df = pd.DataFrame(data)
# plot dataframe 
fig = px.bar(df)

If, instead, you have 2d data then what you want is a scatter plot, not a bar chart.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文