从matplotlib中的x轴时间标签中删除秒数

Osm*_*hop 4 python matplotlib

我想从我的x轴标签中删除秒,因为它们是不必要的.

此外,我想中心对齐刻度标签,而不是让它们位于刻度线的左侧.

有关如何做到这一点的任何建议?

以下是我使用的一些代码,如果这有帮助的话

fig=plt.figure()
ax=fig.add_subplot(111)
line1 = plot(table.index,table[data],color=colors[0])

fig.autofmt_xdate(rotation=0)   

tickFormat =  matplotlib.ticker.LinearLocator(numticks=5)
ax.xaxis.set_major_locator(tickFormat)            
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Osm*_*hop 8

from matplotlib.dates import DateFormatter
formatter = DateFormatter('%H:%M')
plt.gcf().axes[0].xaxis.set_major_formatter(formatter)  
Run Code Online (Sandbox Code Playgroud)

  • tacaswell:*“还有一种方法可以调整自动格式化程序”* - 如何?此外,如果您还使用带有浮点值的 `ax.set_xticks()`,则此解决方案会中断: *"ValueError: DateFormatter found a value of x=0, 这是一个非法日期。这通常是因为您没有通知轴,它正在绘制日期,例如,使用 ax.xaxis_date()"*。你怎么能解决这个问题? (2认同)