Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill area under step curve using pyplot?

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.

like image 811
Battery_Al Avatar asked Nov 03 '17 21:11

Battery_Al


People also ask

How do you fill an area in Python?

fill_between() is used to fill area between two horizontal curves. Two points (x, y1) and (x, y2) define the curves. this creates one or more polygons describing the filled areas. The 'where' parameter can be used to selectively fill some areas.

How do you fill an area between two lines in python?

You can easily fill in the area between values in a Matplotlib plot by using following functions: fill_between(): Fill the area between two horizontal curves. fill_betweenx(): Fill the area between two vertical curves.

How do you fill a line plot in Python?

You can easily fill the area with any color between the lines or under a curve in Matplotlib Line Plots using plt. fill_between().


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

like image 92
ImportanceOfBeingErnest Avatar answered Sep 20 '22 08:09

ImportanceOfBeingErnest