时间序列条图显示值是给定时间段的总和

发布于 2025-01-29 03:28:50 字数 782 浏览 3 评论 0原文

有一个时间序列数据,例如以下数据。

import pandas as pd
data = {'Time': ['2/10/2019', '3/3/2019', '3/15/2019', '3/25/2019', '4/16/2019', '4/17/2019', '5/6/2019', '5/18/2019'],
        'Order nun': [200, 150, 50, 100, 90, 190, 120, 110]}
df = pd.DataFrame(data)
        Time  Order nun
0  2/10/2019        200
1   3/3/2019        150
2  3/15/2019         50
3  3/25/2019        100
4  4/16/2019         90
5  4/17/2019        190
6   5/6/2019        120
7  5/18/2019        110

如何基于月度值的总和来生成时间序列栏图。

There has a time series data, such as the following ones.

import pandas as pd
data = {'Time': ['2/10/2019', '3/3/2019', '3/15/2019', '3/25/2019', '4/16/2019', '4/17/2019', '5/6/2019', '5/18/2019'],
        'Order nun': [200, 150, 50, 100, 90, 190, 120, 110]}
df = pd.DataFrame(data)
        Time  Order nun
0  2/10/2019        200
1   3/3/2019        150
2  3/15/2019         50
3  3/25/2019        100
4  4/16/2019         90
5  4/17/2019        190
6   5/6/2019        120
7  5/18/2019        110

How to generate a time series bar plot based on the sum of monthly value.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

最终幸福 2025-02-05 03:28:50

您可以将time设置为索引并使用pd.grouper(freq ='m') to groupby月

import matplotlib.pyplot as plt
import matplotlib.dates as mdates

df['Time'] = pd.to_datetime(df['Time'])
out = df.set_index('Time').groupby(pd.Grouper(freq='M'))['Order number'].sum()

fig, ax = plt.subplots()
bars = ax.bar(out.index, out)

ax.bar_label(bars)

ax.set_xlabel("Time (month)")
ax.set_ylabel("Order number")

ax.set_xticks(out.index)
ax.set_yticks(range(200, 800, 200))
ax.xaxis.set_major_formatter(mdates.DateFormatter("%b %Y"))

plt.show()

“在此处输入图像说明”

bar如此薄月。您可以使用字符串使其正常。

df['Time'] = pd.to_datetime(df['Time']).dt.strftime('%b %Y')
out = df.groupby('Time')['Order number'].sum()

fig, ax = plt.subplots()
bars = ax.bar(out.index, out)

ax.bar_label(bars)

ax.set_xlabel("Time (month)")
ax.set_ylabel("Order number")

ax.set_xticks(out.index)
ax.set_yticks(range(200, 800, 200))

plt.show()

You can set the Time as index and use pd.Grouper(freq='M') to groupby month

import matplotlib.pyplot as plt
import matplotlib.dates as mdates

df['Time'] = pd.to_datetime(df['Time'])
out = df.set_index('Time').groupby(pd.Grouper(freq='M'))['Order number'].sum()

fig, ax = plt.subplots()
bars = ax.bar(out.index, out)

ax.bar_label(bars)

ax.set_xlabel("Time (month)")
ax.set_ylabel("Order number")

ax.set_xticks(out.index)
ax.set_yticks(range(200, 800, 200))
ax.xaxis.set_major_formatter(mdates.DateFormatter("%b %Y"))

plt.show()

enter image description here

The reason why the bar is so thin is that the bar only takes one day in a month. You can use string instead to make it normal.

df['Time'] = pd.to_datetime(df['Time']).dt.strftime('%b %Y')
out = df.groupby('Time')['Order number'].sum()

fig, ax = plt.subplots()
bars = ax.bar(out.index, out)

ax.bar_label(bars)

ax.set_xlabel("Time (month)")
ax.set_ylabel("Order number")

ax.set_xticks(out.index)
ax.set_yticks(range(200, 800, 200))

plt.show()

enter image description here

云归处 2025-02-05 03:28:50
import seaborn as sns
import matplotlib.pyplot as plt

df['Time'] = pd.to_datetime(df['Time'])
plotme = df.resample('M', on='Time').sum()
sns.barplot(y=plotme['Order nun'], x=plotme['Time'].dt.strftime('%b %Y'))
plt.show()

输出:

import seaborn as sns
import matplotlib.pyplot as plt

df['Time'] = pd.to_datetime(df['Time'])
plotme = df.resample('M', on='Time').sum()
sns.barplot(y=plotme['Order nun'], x=plotme['Time'].dt.strftime('%b %Y'))
plt.show()

Output:

bargraph

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