'time series bar plot showing the values being the sum for a given time period
Solution 1:[1]
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()
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()
Solution 2:[2]
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:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |




