0

enter image description here

I'm not sure how to call this exactly, but I want to create a graph the looks like the figure above in PyPlot. Focusing only on a single color, I have 10 independent data sets containing (x, y) points I could plot as a graph. However I would like to present them together: one data set is presented normally, and there is a "range' shade around each point showing the minimum and maximum y for a certain x.

Zephyr
  • 11,891
  • 53
  • 45
  • 80
corazza
  • 31,222
  • 37
  • 115
  • 186
  • I think what I'm looking for is described here https://matplotlib.org/3.3.4/gallery/recipes/fill_between_alpha.html – corazza Aug 28 '21 at 10:15

1 Answers1

1

I suppose you have data store like this dataframe:

N = 20
df = pd.DataFrame({'x': np.linspace(0, 150000, N),
                   'y': np.random.random(N)})
df['y_max'] = df['y'] + np.random.random(N)
df['y_min'] = df['y'] - np.random.random(N)
              x         y     y_max     y_min
0      0.000000  0.856374  1.685085  0.181898
1   7894.736842  0.471733  0.713564  0.128606
2  15789.473684  0.817586  1.453245  0.520492
3  23684.210526  0.352486  0.464310  0.093795
4  31578.947368  0.188503  0.427685 -0.203351
5  39473.684211  0.192593  1.018089 -0.586906
6  47368.421053  0.143718  0.375640 -0.833777
7  55263.157895  0.288232  0.764800  0.035718
8  63157.894737  0.047860  0.802160 -0.776364
9  71052.631579  0.647542  1.389724  0.290451
...

Where 'y' is the actual value and 'y_min' and 'y_max' are minimum and maximum values get from other dataset you have.
Then you can plot 'y' with matplotlib.axes.Axes.plot and the shaded area with matplotlib.axes.Axes.fill_between:

fig, ax = plt.subplots()

ax.plot(df['x'], df['y'], color = 'blue')
ax.fill_between(df['x'], df['y_max'], df['y_min'], color = 'blue', alpha = 0.5)

plt.show()

Complete Code

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


N = 20
df = pd.DataFrame({'x': np.linspace(0, 150000, N),
                   'y': np.random.random(N)})
df['y_max'] = df['y'] + np.random.random(N)
df['y_min'] = df['y'] - np.random.random(N)


fig, ax = plt.subplots()

ax.plot(df['x'], df['y'], color = 'blue')
ax.fill_between(df['x'], df['y_max'], df['y_min'], color = 'blue', alpha = 0.5)

plt.show()

enter image description here

Zephyr
  • 11,891
  • 53
  • 45
  • 80