【问题标题】:Shift all time ticks by some amount in Matplotlib?在 Matplotlib 中将所有时间刻度移动一定量?
【发布时间】:2021-09-14 01:55:21
【问题描述】:

我在 Matplotlib 的 x 轴上显示时间(小时、分钟)刻度

plt.gca().xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%H:%M"))

但是,发生的事情是我的刻度不是“好”数字。刻度的自动定位给了我 10:34、11:04、11:34 等,而我想要 10:30、11:00、11:30 等。

如何将刻度固定在我想要的时间?有没有办法在使用时间时(在我的示例中为 4 分钟)将所有刻度数移动一定量?

【问题讨论】:

    标签: python datetime matplotlib xticks


    【解决方案1】:

    尝试使用MinuteLocator 将滴答声限制在第 0 分钟和第 30 分钟:

    minloc = matplotlib.dates.MinuteLocator(byminute=[0, 30])
    plt.gca().xaxis.set_major_locator(minloc)
    

    【讨论】:

      猜你喜欢
      • 2014-08-03
      • 2015-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      相关资源
      最近更新 更多