matplotlib Mathplotlib自动添加日期

ymdaylpp  于 2023-03-03  发布在  其他
关注(0)|答案(1)|浏览(136)

为什么mathplotlib要在图中添加日期。例如:2023-02-15

df = pd.DataFrame()
df['YearMonth'] = [202303,202302,202301,202212,202211]
df['total_burgers'] = [800,900,1200,1100,1000]
df['YearMonth'] = pd.to_datetime(df['YearMonth'], format='%Y%m')

x=df['YearMonth']
y1=df['total_burgers']
plt.plot(x, y1)

pzfprimi

pzfprimi1#

Matplotlib会自动选择可以像这样定义的xtick。

df = pd.DataFrame()
df['YearMonth'] = [202303,202302,202301,202212,202211]
df['total_burgers'] = [800,900,1200,1100,1000]
df['YearMonth'] = pd.to_datetime(df['YearMonth'], format='%Y%m')

x = df['YearMonth']
y1 = df['total_burgers']
plt.plot(x, y1)

plt.xticks(x, x.dt.strftime('%Y-%m'), rotation=45)

plt.show()

相关问题