Akimi_Usami 2022-08-01 12:31 采纳率: 100%
浏览 121
已结题

用matplotlib的monthlocator()绘制的月度时间横坐标如何修改?

如图,横轴是一组时间序列的日期,用matplotlib.dates的monthlocator('%m')表示为月,纵轴是时间序列的值。结果在12月后面多了一个1月,而且怎么删也删不掉,包括设置monthlocator的内部参数bymonthday也没用。于是我怀疑是数据的问题,但是数据看上去也很正常。求问如何把最后一个01删掉?

代码:

fig = plt.figure(figsize = [10, 5])
ax = fig.add_subplot(1, 1, 1)
ax.plot(df_2.loc[:, '2022'], color = 'red', linestyle = 'solid', label = '2022')
ax.plot(df_2.loc[:, '2021'], color = 'grey', linestyle = 'solid', label = '2021')
ax.plot(df_2.loc[:, '2020'], color = 'pink', linestyle = 'dashed', label = '2020')
ax.plot(df_2.loc[:, '2019'], color = 'black', linestyle = 'dashed', label = '2019')
plt.xticks(rotation='horizontal')
ax.xaxis.set_major_locator(MonthLocator())
ax.xaxis.set_major_formatter(DateFormatter('%m'))
ax.set_ylim([0, 19000])
plt.figtext(0.85, 0.825, df_type, fontsize = 'large',
horizontalalignment = 'right',
verticalalignment = 'top')
plt.figtext(0.115, 0.925, '万吨', fontsize = 'large',
horizontalalignment = 'right',
verticalalignment = 'top')
ax.legend(loc = 'best', bbox_to_anchor = (0.75, 1.095), borderpad=0.5, labelspacing=0.5, ncol = 4)

成图:

img

下面是数据:

img

  • 写回答

6条回答

  • herosunly Python领域优质创作者 2022-08-01 13:18
    关注

    把下列代码换成里面的代码试试:
    ax.xaxis.set_major_locator(MonthLocator())
    ax.xaxis.set_major_formatter(DateFormatter('%m'))

    #Add to plot settings:
    ax.set_xlim(np.datetime64('1970-01-01'), np.datetime64('1970-12-31'))
    ax.xaxis.set_major_locator(MonthLocator(bymonthday=1))
    ax.xaxis.set_major_formatter(md.DateFormatter('%b'))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 8月10日
  • 已采纳回答 8月2日
  • 请采纳用户回复 8月1日
  • 创建了问题 8月1日

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包