Matplotlib x轴重叠

问题描述:

我有两个列表,x_axis是时间列表,格式为"12:30:00". y轴是百分比值.我需要在一张图上绘制所有值,但是由于x轴字符串太长,它们会重叠.无论如何,我可以让matplotlib不在x轴上每次都显示吗?任何帮助,将不胜感激.

I have two lists, x_axis which is list of time in the format of '12:30:00'. The y-axis is percent values. I need to plot all the values on a graph, however since x-axis string is too long they overlap. Is there anyway I can have matplotlib not show every single time on x-axis? Any help would be appreciated.

您可以旋转并打印每个第二个ticklabel:

You could rotate and print every 2nd ticklabel:

_ = plt.plot(df['str_time'], df.Pct, 'ro')
ax = plt.gca()
plt.axis([0,24,0,50])
plt.xticks(rotation=90)
for label in ax.get_xaxis().get_ticklabels()[::2]:
    label.set_visible(False)

输出: