Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:显示月底而不是月的第一天_Python_Pandas_Matplotlib - Fatal编程技术网

Python:显示月底而不是月的第一天

Python:显示月底而不是月的第一天,python,pandas,matplotlib,Python,Pandas,Matplotlib,当我使用matplotlib绘制数据帧时,它会显示月份的第一天。相反,我真正想要的是显示月底。如何使用matplotlib Date Miles 2020-01-31 410.364502 2020-02-29 1190.701827 2020-03-31 2400.855076 2020-04-30 526.747960 2020-05-31 2044.

当我使用
matplotlib
绘制数据帧时,它会显示月份的第一天。相反,我真正想要的是显示月底。如何使用
matplotlib

Date                  Miles  
2020-01-31            410.364502
2020-02-29           1190.701827
2020-03-31           2400.855076
2020-04-30            526.747960
2020-05-31           2044.158990
2020-06-30          13983.470617
2020-07-31          18911.396787
2020-08-31           9219.378732
2020-09-30          16039.311330
2020-10-31          16847.898328
2020-11-30           7452.507720


fig, ax = plt.subplots(dpi=800)
ax.plot(general_trend.index, general_trend['Miles'], linewidth=0.8,color='red')
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax.legend(loc='upper right', ncol=2,fontsize=4.5)
ax.tick_params(axis='both', which='major', labelsize=6)
fig.autofmt_xdate()
plt.show()

可能还有很多其他方法,但您可以使用定位器和格式化程序指定月末,显示基准为“bymonthday=-1”

general_trend['Date'] = pd.to_datetime(general_trend['Date'])
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

fig, ax = plt.subplots(figsize=(4,3),dpi=200)
ax.plot(general_trend['Date'], general_trend['Miles'], linewidth=0.8, color='red', label='Miles')

months = mdates.MonthLocator(interval=1, bymonthday=-1)
months_fmt = mdates.DateFormatter('%Y-%m-%d')
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(months_fmt)

ax.legend(loc='upper right', ncol=2, fontsize=4.5)
ax.tick_params(axis='x', which='major', labelsize=6, labelrotation=45)

plt.show()

日期的格式是什么?他们在名单上吗?