在 matplotlib 中使用年-周列作为条形图的 x 轴
Using year-week column as x-axis for bar chart in matplotlib
我尝试用 matplotlib
绘制 bar plot
以显示聚合数随时间每周的变化(我的数据集大约有 1000 周)。通常,当我绘制这样的时间序列时,我有每天的数据,我用 pd.to_datetime()
转换日期。但在每周的情况下,不知道如何指定格式以获得准确的 x-axis
年份标签,而不是相互重叠的星期。
这将是一个简单的数据集示例:
YearWeek Output
0 2022-01 7.3
1 2022-02 5.3
2 2022-03 7.2
3 2022-04 4.8
4 2022-05 5.8
5 2022-06 9.2
6 2022-07 5.3
7 2022-08 5.3
8 2022-09 7.5
9 2022-10 9.2
10 2022-11 5.4
11 2022-12 4.8
在多周的情况下,x-axis
不可读,标签相互重叠。我尝试只获取年度标签。
这将是我正在使用的真实数据集的图表:
为了可重复性:
import pandas as pd
import matplotlib.pyplot as plt
df1 = pd.DataFrame({
'YearWeek':['2022-01', '2022-02', '2022-03', '2022-04', '2022-05', '2022-06', '2022-07', '2022-08', '2022-09', '2022-10', '2022-11', '2022-12'],
'Output':[7.3, 5.3, 7.2, 4.8, 5.8, 9.2, 5.3, 5.3, 7.5, 9.2, 5.4, 4.8]})
plt.bar(df1['YearWeek'], df1['Output'])
非常感谢!
一个解决方案可能是旋转它们:
...
plt.xticks(rotation=45)
plt.show()
Edit1:在 x-axis 上添加 plt.xticks(df1['YearWeek'],df1['YearWeek'].str[:4])
以仅显示年份而不是 year-week。
Edit2:x-axis 每年只显示一次。
您可以使用 plt.xticks(rotation=90)
.
将 x-axis 个刻度旋转 90 度或 45 度
例如:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df1 = pd.DataFrame({
'YearWeek':['2021-01', '2021-02', '2021-03', '2021-04', '2021-05', '2021-06', '2021-07', '2021-08', '2021-09', '2021-10', '2021-11', '2021-12', '2022-01', '2022-02', '2022-03', '2022-04', '2022-05', '2022-06', '2022-07', '2022-08', '2022-09', '2022-10', '2022-11', '2022-12'],
'Output':[6.3, 5.3, 7.2, 4.8, 8.8, 9.2, 5.3, 5.3, 4.5, 9.2, 5.4, 4.8, 7.3, 5.3, 8.2, 4.8, 5.8, 9.2, 5.3,2.3, 7.5, 9.2, 5.4, 9.8]})
df1['Year'] = df1['YearWeek'].str[:4]
plt.bar(df1['YearWeek'], df1['Output'])
# plt.xticks(rotation=90)
plt.xticks(df1['YearWeek'],df1['Year'])
plt.xticks(np.arange(6, len(df1), 12))
plt.show()
Edit2 的输出:
我尝试用 matplotlib
绘制 bar plot
以显示聚合数随时间每周的变化(我的数据集大约有 1000 周)。通常,当我绘制这样的时间序列时,我有每天的数据,我用 pd.to_datetime()
转换日期。但在每周的情况下,不知道如何指定格式以获得准确的 x-axis
年份标签,而不是相互重叠的星期。
这将是一个简单的数据集示例:
YearWeek Output
0 2022-01 7.3
1 2022-02 5.3
2 2022-03 7.2
3 2022-04 4.8
4 2022-05 5.8
5 2022-06 9.2
6 2022-07 5.3
7 2022-08 5.3
8 2022-09 7.5
9 2022-10 9.2
10 2022-11 5.4
11 2022-12 4.8
在多周的情况下,x-axis
不可读,标签相互重叠。我尝试只获取年度标签。
这将是我正在使用的真实数据集的图表:
为了可重复性:
import pandas as pd
import matplotlib.pyplot as plt
df1 = pd.DataFrame({
'YearWeek':['2022-01', '2022-02', '2022-03', '2022-04', '2022-05', '2022-06', '2022-07', '2022-08', '2022-09', '2022-10', '2022-11', '2022-12'],
'Output':[7.3, 5.3, 7.2, 4.8, 5.8, 9.2, 5.3, 5.3, 7.5, 9.2, 5.4, 4.8]})
plt.bar(df1['YearWeek'], df1['Output'])
非常感谢!
一个解决方案可能是旋转它们:
...
plt.xticks(rotation=45)
plt.show()
Edit1:在 x-axis 上添加 plt.xticks(df1['YearWeek'],df1['YearWeek'].str[:4])
以仅显示年份而不是 year-week。
Edit2:x-axis 每年只显示一次。
您可以使用 plt.xticks(rotation=90)
.
例如:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df1 = pd.DataFrame({
'YearWeek':['2021-01', '2021-02', '2021-03', '2021-04', '2021-05', '2021-06', '2021-07', '2021-08', '2021-09', '2021-10', '2021-11', '2021-12', '2022-01', '2022-02', '2022-03', '2022-04', '2022-05', '2022-06', '2022-07', '2022-08', '2022-09', '2022-10', '2022-11', '2022-12'],
'Output':[6.3, 5.3, 7.2, 4.8, 8.8, 9.2, 5.3, 5.3, 4.5, 9.2, 5.4, 4.8, 7.3, 5.3, 8.2, 4.8, 5.8, 9.2, 5.3,2.3, 7.5, 9.2, 5.4, 9.8]})
df1['Year'] = df1['YearWeek'].str[:4]
plt.bar(df1['YearWeek'], df1['Output'])
# plt.xticks(rotation=90)
plt.xticks(df1['YearWeek'],df1['Year'])
plt.xticks(np.arange(6, len(df1), 12))
plt.show()
Edit2 的输出: