nykon
nykon

Reputation: 625

matplotlib fill_between issue with x argument

I try to fill a space below my plot, the plot is y=x so it is a straight line with an angle of 45 deg. I try to fill the area below the curve from x=1 to x=10, how to do that using fill_between?

Upvotes: 1

Views: 1720

Answers (1)

ev-br
ev-br

Reputation: 26040

That's what where keyword argument is for.

where : If None, default to fill between everywhere. If not None, it is an N-length numpy boolean array and the fill will only happen over the regions where where==True.

For example:

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> x = np.linspace(0, 10, 50)
>>> y = x**2
>>> ax.plot(x, y, 'r-')
[<matplotlib.lines.Line2D object at 0x1e91250>]
>>> wh = (x>1) & (x<10)    
>>> ax.fill_between(x, y, where=wh, alpha=0.2)
<matplotlib.collections.PolyCollection object at 0x24dd210>
>>> plt.show()

Upvotes: 2

Related Questions