12

I want to know how it is possible to draw a figure like the one below in python using matplotlib. enter image description here

What one of the curves, let's say the yellow one, show is that for a set of list say l1 to l10 where each one has length of 25k, it calculates the mean and draws it as a solid yellow line and also draws mean +/- standard deviation as transparant, shaded areas around the mean.

How can we plot such a thing using matplotlib? I used the links the comments for the example in code below.

from matplotlib import pyplot as pl
import numpy as np


l = []
for _ in xrange(20):
    l.append(np.random.uniform(0, 1, 100))

mean = np.mean(l, axis=0)
standard_dev = np.std(l, axis=0)

pl.plot(mean)
pl.fill_between(mean, mean-standard_dev, mean+standard_dev)
pl.show()

But what I get is:

enter image description here

user491626
  • 357
  • 1
  • 2
  • 14
  • Looks like you can fill an area between lines: https://matplotlib.org/gallery/lines_bars_and_markers/fill_between_demo.html#sphx-glr-gallery-lines-bars-and-markers-fill-between-demo-py. – OrderAndChaos Aug 03 '18 at 21:34
  • Try looking at these examples: https://stackoverflow.com/questions/43064524/plotting-shaded-uncertainty-region-in-line-plot-in-matplotlib-when-data-has-nans and https://stackoverflow.com/questions/12957582/plot-yerr-xerr-as-shaded-region-rather-than-error-bars – Sheldore Aug 03 '18 at 21:34
  • @Bazingaa Thanks, I used it but it doesn't work as it should for me. Do you know what's wrong with my code? – user491626 Aug 03 '18 at 21:54
  • 11
    Here you go: `pl.fill_between(range(100), mean-standard_dev, mean+standard_dev, alpha = 0.5)`. You forgot to define the `x` axis values for plotting your error bars. `alpha=0.5` controls the transparency of the shaded region. – Sheldore Aug 03 '18 at 21:57
  • Seaborn does it automatically by using ci="sd". See https://seaborn.pydata.org/generated/seaborn.lineplot.html – Gouz Nov 02 '21 at 23:37

0 Answers0