Create a plot with x axis as timestamp and y axis as shifted price

时间:2017-12-18 07:25:40

标签: python matplotlib time-series

I am new to time-series programming with pandas. Can somebody help me with this.

Create a plot with x axis as timestamp and y axis as shifted price. In the plot draw the following dotted lines:

  • Green dotted line which indicates mean

    • Say mean of shifted price distribution is 0.5 and standard deviation is 2.25
    • Line should be y = 0.5 ie horizontal line parallel to x-axis
  • Red dotted lines which indicates one standard deviation above and below x-axis.

    • Line should be y=2.25 and y=-2.25

Following is a sample image which shows the shifted price in y-axis, time in x-axis, green dotted line on mean and red dotted line on +- standard deviation

enter image description here

here is the sample data:

0 2017-11-05 09:20:01.134  2123.0  12.23  34.12         300.0
1 2017-11-05 09:20:01.789  2133.0  32.43  45.62         330.0
2 2017-11-05 09:20:02.238  2423.0  35.43  55.62           NaN
3 2017-11-05 09:20:02.567  3423.0  65.43  56.62           NaN
4 2017-11-05 09:20:02.948  2463.0  45.43  58.62           NaN

1 个答案:

答案 0 :(得分:0)

将您的价格视为系列,并将其绘制如下:

    import numpy as np
    import pandas as pd

    # Date 
    rng = pd.date_range('1/1/2000', periods=1000)
    # Create a Random Series 
    ts = pd.Series(np.random.randn(len(rng)), index=rng)

    # Create plot 
    ax = ts.plot()

    # Plot de mean
    ax.axhline(y=ts.mean(), color='r', linestyle='--', lw=2)

    # Plot CI
    ax.axhline(y=ts.mean() + 1.96*np.sqrt(np.var(ts)), color='g', linestyle=':', lw=2)
    ax.axhline(y=ts.mean() - 1.96*np.sqrt(np.var(ts)), color='g', linestyle=':', lw=2)