首页 > 解决方案 > 如何将日期时间 x 轴绘制为每月?

问题描述

我想让 xtick 显示从 2019 年 11 月到 2021 年 2 月的所有月份。使用以下代码,xtick 在图中的间隔为 2 个月。
我应该如何将其更改为 1 个月的间隔?

我使用 mdate 修复它,但输出是一样的。

# create figure and axis objects with subplots()
fig,ax = plt.subplots()
# make a plot
ax.plot(df.Date, df.Close, color="yellow",)
# set x-axis label
ax.set_xlabel("date",fontsize=14)
ax.set_xticklabels(df.Date, Rotation=45) 
# set y-axis label
ax.set_ylabel("gold price",color="red",fontsize=14)

# twin object for two different y-axis on the sample plot
ax2=ax.twinx()

# make a plot with different y-axis using second axis object
ax2.plot(us_ncase.date,us_ncase.new_cases ,color="red")
ax2.set_ylabel("Covid New Case",color="blue",fontsize=14)
plt.show()

标签: pythondatetimematplotlib

解决方案


推荐阅读