【问题标题】:Time series dataframe. Plot two distinct time ranges with a relative starting point时间序列数据框。绘制具有相对起点的两个不同时间范围
【发布时间】:2019-06-14 08:45:35
【问题描述】:

我有一个包含时间序列的数据框(单只股票的每日价格)。我想采用两个不同的时间范围并将它们覆盖在相对起点为 0 而不是日期的绘图上。

在下面的示例中,如果我绘制 1962 年和 2018 年,它将使用日期作为 x 轴而不是相对起点。

SPY = pd.read_csv('GSPC.csv', parse_dates=['dDate'], index_col='dDate')

SPY1962 = SPY['1962']
SPY2018 = SPY['2018']

firstprice62 = SPY1962['nAdjClose'].iloc[0]
firstprice18 = SPY2018['nAdjClose'].iloc[0]

normal62 = SPY1962['nAdjClose'].div(firstprice62).mul(100)
normal18 = SPY2018['nAdjClose'].div(firstprice18).mul(100)

A picture of what I'm trying to accomplish

【问题讨论】:

    标签: python python-3.x matplotlib time-series


    【解决方案1】:

    想通了。

    normal18 = normal18.reset_index()
    normal62 = normal62.reset_index()
    
    normal62['nAdjClose'].plot()
    normal18['nAdjClose'].plot()
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 2016-07-31
      • 1970-01-01
      • 2017-05-03
      • 2020-07-18
      • 1970-01-01
      • 2013-12-25
      相关资源
      最近更新 更多