How to fill area under step curve using pyplot?

Viewed 16097

I have plotted two step curves using pyplot.step(), and I would like to shade in the area beneath these curves (ideally with transparent shading). pyplot.fill_between() assumes linear interpolation, whereas I want to see step interpolation, as displayed below:step curves without shading

How can I shade in the region beneath these curves? Transparent coloring would be great, as this would make clear where these curves overlap.

1 Answers

You can use the alpha value of the fill_between to make it semi-transparent.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,50,35)
y = np.random.exponential(1, len(x))
y2 = np.random.exponential(1, len(x))

plt.fill_between(x,y, step="pre", alpha=0.4)
plt.fill_between(x,y2, step="pre", alpha=0.4)

plt.plot(x,y, drawstyle="steps")
plt.plot(x,y2, drawstyle="steps")

plt.show()

enter image description here

Related