【问题标题】:Matplotlib x-axis overlapMatplotlib x 轴重叠
【发布时间】:2018-04-26 00:41:36
【问题描述】:

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

【问题讨论】:

  • 如果数据是字符串,Matplotlib 只会将每个单独的坐标显示为刻度。请改用日期时间。

标签: python matplotlib jupyter-notebook


【解决方案1】:

您可以旋转并打印每第二个刻度标签:

_ = 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)

输出:

【讨论】:

    【解决方案2】:

    您可以使用以下代码旋转标签以显示列表时间。

    plt.xticks(rotation=90)
    

    【讨论】:

      【解决方案3】:

      我需要步进 x 轴数字而不是旋转。

      ax.set_xticks(np.arange(0, max_number, 5)) #step 5 digits

      输出:

      【讨论】:

        【解决方案4】:

        自动执行此操作的一种方法是使用 autofmt_xdate

        fig.autofmt_xdate():
        

        要获取 fig 对象,您必须调用 subplot 函数

        fig, ax = plt.subplots()
        

        效果很好

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-11-11
          • 2020-10-14
          • 1970-01-01
          • 2016-04-17
          • 1970-01-01
          • 1970-01-01
          • 2012-03-14
          • 1970-01-01
          相关资源
          最近更新 更多