【问题标题】:plotting dates backwards matplotlib / pandas向后绘制日期 matplotlib / pandas
【发布时间】:2019-11-08 05:19:27
【问题描述】:

我想在 x 轴的左侧而不是右侧绘制一张最近日期的图表。

有没有办法在 pandas 和 matplotlib 中做到这一点并且仍然获得日期轴?

Invert an axis in a matplotlib grafic

展示了如何使用 invert_yaxis() 对 y 轴执行此操作。但是,这不适用于 xaxis。

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    从 pyplot 中设置 xlim()。让我们举个例子:

    period = pd.period_range("1.1.2013","12.1.2013",freq="M")
    data = np.arange(12)
    s = pd.Series(data=data,index=period)
    #Output
    2013-01     0
    2013-02     1
    2013-03     2
    2013-04     3
    2013-05     4
    2013-06     5
    2013-07     6
    2013-08     7
    2013-09     8
    2013-10     9
    2013-11    10
    2013-12    11
    

    将 xlim 的第一个值设置为系列的最后一个索引,将第二个值设置为第一个索引,如下所示:

    s.plot()
    plt.xlim(s.index[-1],s.index[0])
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2019-05-14
      • 1970-01-01
      • 2014-03-24
      • 2020-11-08
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      相关资源
      最近更新 更多