Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling above/below matplotlib line plot

Tags:

I'm using matplotlib to create a simple line plot. My plot is a simple time-series data set where I have time along the x-axis and the value of something I am measuring on the y-axis. y values can have postitive or negative values and I would like to fill in the area above and below my line with the color blue if the y-value is > 0 and red if the y values is < 0. Here's my plot:

enter image description here

As you can see, I can get the blue color to fill in correctly, but I can not get the red color to fill in properly. Here's the basic code I am using:

plt.plot(x, y, marker='.', lw=1) d = scipy.zeros(len(y)) ax.fill_between(xs,ys,where=ys>=d, color='blue') ax.fill_between(xs,0,where=ys<=d, color='red') 

How can I get the area from a positive y-value to the x-axis to be blue and the area from a negative y-value to the x-axis to be red? Thanks for the help.

like image 500
user1728853 Avatar asked Jun 04 '13 12:06

user1728853


People also ask

How do I fill in below a line in Matplotlib?

To fill the area under the curve, put x and y with ste="pre", using fill_between() method. Plot (x, y1) and (x, y2) lines using plot() method with drawstyle="steps" method. To display the figure, use show() method.

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().

How do I fill between Matplotlib?

Matplotlib fill between three linesFirstly we fill the area, between y1 and y2 by using the fill_between() method and we set the color red by using the parameter color. Then we fill the area, between y2 and y3 by using the fill_between() method and we set its color to yellow by using a color parameter.

How to fill area between the line plots in Matplotlib?

You can easily fill the area with any color between the lines or under a curve in Matplotlib Line Plots using plt.fill_between (). matplotlib.pyplot.fill_between (x, y1, y2= 0, where= None, interpolate= False, step= None, *, data= None, **kwargs)

How to fill color above the curve in Matplotlib?

To fill color above the curve, we can take the following steps − Initialize the variable n. Initialize x and y data points using numpy. Create a figure and a set of subplots, fig and ax. Plot the curve using plot () method.

How to fill the area between the lines or under a curve?

You can easily fill the area with any color between the lines or under a curve in Matplotlib Line Plots using plt.fill_between (). matplotlib.pyplot.fill_between (x, y1, y2= 0, where= None, interpolate= False, step= None, *, data= None, **kwargs) The x coordinates of the nodes defining the curves.

Can you add patterns to matplotlib plots?

For now, unfortunately, matplotlib has rather limited functionality for this purpose. Moreover, there is no unique approach for different types of plots. In this article, we’re going to explore how to add patterns to bar plots, histograms, box plots, and pie charts.


Video Answer


1 Answers

The code snippet you provided should be corrected as follows:

plt.plot(x, y, marker='.', lw=1) d = scipy.zeros(len(y)) ax.fill_between(xs, ys, where=ys>=d, interpolate=True, color='blue') ax.fill_between(xs, ys, where=ys<=d, interpolate=True, color='red') 

The fill_between method takes at least two arguments x and y1, while it also has a parameter y2 with default value 0. The method will fill the area between y1 and y2 for the specified x-values.

The reason why you didn't get any filling below the x-axis, is due to the fact that you had specified that the fill_between method should fill the area between y1=0 and y2=0, i.e. no area. To make sure that the fill does not only appear on explicit x-values, specify that the method should interpolate y1 as to find the intersections with y2, which is done by specifying interpolate=True in the method call.

like image 179
sodd Avatar answered Nov 04 '22 03:11

sodd