简体   繁体   English

在matplotlib中编辑堆积条形图的x轴刻度标签

[英]Editing the x-axis tick labels of a stacked bar graph in matplotlib

I have written code to produce a stacked bar graph that plots 192 bars of data. 我编写了代码来生成堆积条形图,绘制192条数据。 The data spans over 4 days, and each bar represents a 30 minute time period. 数据跨越4天,每个条形代表30分钟的时间段。

The graph produces 192 tick labels that overlap each other which is obviously undesirable appearance-wise. 该图形产生192个刻度标签,它们彼此重叠,这在外观上显然是不合需要的。

To fix this, I have added the following lines to my code: 为了解决这个问题,我在代码中添加了以下几行:

myFmt = mdates.DateFormatter('%d') myFmt = mdates.DateFormatter('%d')
ax.xaxis.set_major_formatter(myFmt) ax.xaxis.set_major_formatter(myFmt)

I want to have one tick label for each day, but matplotlib automatically generates unwanted, extra tick labels when my graph spans over 4 days or less. 我希望每天都有一个刻度标签,但是当我的图表跨越4天或更短时间时,matplotlib会自动生成不需要的额外刻度标签。 This problem doesn't happen when there are 5 or more days. 当有5天或更多天时,不会发生此问题。 Is there any way I can force the graph to just have only one tick for each day? 有什么方法可以强制图表每天只有一个刻度吗?

在此输入图像描述

You just need to use the proper locator: 你只需要使用正确的定位器:

ax.xaxis.set_major_locator(matplotlib.dates.DayLocator())

(doc) (DOC)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM