Search code examples
pythonpandasline-plot

Plot the mean and SD plot using two pandas series in python


I have two pandas series: mean_Series and STD_series. length of each of these series is 160.

I want to plot a line chart of the mean_series and have a shaded region around it using the STD_series.

Data:

> mean_series        STD_series
>     1121.22                9.1121
>     1132.22                9.4663
>     1124.22                9.4405
>        .                      .
>        .                      .
>        .                      .
>     1522.25                12.5521
>     1562.25                12.7721

code I have used:

plt.plot(mean_series,label='mean', color='red')
plt.show()

I want to add shaded region around the mean line plot using STD_series.


Solution

  • What you're looking for is ax.fill_between. See it in action:

    x = [90,100,110]
    mean_series = pd.Series([10,11,10.5],index=x)
    STD_series = pd.Series([1,2,.5],index=x)
    ax = mean_series.plot(ylim=(0,20))
    lower_bound = mean_series - STD_series
    upper_bound = mean_series + STD_series
    ax.fill_between(x,lower_bound,upper_bound,alpha=.3)
    

    mean + shaded SD