【问题标题】:matplotlib: change timestamp format of x-grid in a bar-chartmatplotlib:在条形图中更改 x-grid 的时间戳格式
【发布时间】:2017-06-27 22:57:04
【问题描述】:

我有以下代码:

 my_df.plot(x='my_timestamp', y='data_count', kind='bar')

输出如下所示:

我想知道是否可以更改 x-grid 格式,所以它只会显示 2016-03-14 而不是 2016-03-14 00:00:00

谢谢!

【问题讨论】:

    标签: python-3.x pandas matplotlib bar-chart


    【解决方案1】:

    可能有一个更优雅的解决方案,但绝对有效的是循环 xticklabels,将它们拆分并仅使用字符串的第一部分作为新的 ticklabel。

    ax = df.plot(x='my_timestamp', y='data_count', kind='bar')
    
    ax.set_xticklabels([t.get_text().split(" ")[0] for t in ax.get_xticklabels()])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      • 2018-11-27
      • 1970-01-01
      相关资源
      最近更新 更多