简体   繁体   中英

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. The data spans over 4 days, and each bar represents a 30 minute time period.

The graph produces 192 tick labels that overlap each other which is obviously undesirable appearance-wise.

To fix this, I have added the following lines to my code:

myFmt = mdates.DateFormatter('%d')
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. This problem doesn't happen when there are 5 or more days. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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