Search code examples
pythonpandasmatplotlibplot

Plot shaded lines for std from different dataframes in one graph


I am stuck with this problem. I got two dataframes. df is my mean and error the corresponding std. I would like to have a normal line graph for every column of df and shaded areas over and under these lines from error.

This is what I tried so far, but I get this error.

fig, axs = plt.subplots(figsize =(20,10))

axs.set_ylim(0,4)
axs.plot(df)
axs.fill_between(df, df - error, df + error)
plt.show() 

File "C:\Users\mobil\anaconda3\lib\site-packages\matplotlib\__init__.py", line 1565, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "C:\Users\mobil\anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 5156, in fill_between
    raise ValueError('Input passed into argument "%r"' % name +
ValueError: Input passed into argument "'x'"is not 1-dimensional.

These are the dataframes:

df
          0         1         2         3         4         5
0  0.000000  0.000000  0.000000  0.000000  0.185890  0.054603
1  0.000000  0.000000  0.000000  0.000000  0.204758  0.073861
2  0.000000  0.000000  0.000000  0.506930  0.278364  0.116938
3  0.000000  0.000000  0.463905  0.738385  0.563741  0.222716
4  0.000000  0.479647  0.616823  1.197356  0.715571  0.281495
5  0.223208  1.674243  1.581247  2.028640  0.735176  0.268374
6  0.638434  2.255282  1.747558  2.060295  0.685145  0.251289
7  0.896262  2.570574  1.839729  2.180433  0.629562  0.363102


error
          0         1         2         3         4         5
0  0.000000  0.000000  0.000000  0.000000  0.025165  0.024576
1  0.000000  0.000000  0.000000  0.000000  0.043171  0.025021
2  0.000000  0.000000  0.000000  0.249822  0.066857  0.031510
3  0.000000  0.000000  0.351071  0.410710  0.099998  0.062855
4  0.000000  0.439829  0.426086  0.578717  0.074479  0.087265
5  0.163256  1.261762  0.823696  0.743150  0.028472  0.139199
6  0.415963  1.595399  0.810581  0.722636  0.047193  0.151555
7  0.469144  1.590634  0.760402  0.700425  0.062162  0.153096

All I was able to get so far was the plot without the shaded std. enter image description here

Hopefully someone has an idea?


Solution

  • Simple case of plot each of the error areas individually

    import matplotlib.colors
    
    fig, axs = plt.subplots(figsize =(10,6))
    
    axs.set_ylim(0,4)
    
    for i, c in enumerate(df.columns):
        color = plt.rcParams['axes.prop_cycle'].by_key()["color"][i]
        axs.fill_between(df.index, (df - error)[c], (df+error)[c], 
                         facecolor=matplotlib.colors.to_rgba(color, .2), edgecolor=color)
    df.plot(ax=axs, lw=5)
    
    

    enter image description here