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:
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.
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.
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 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.
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)
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With