I cannot understand how to use fill_betweenx()
in matplotlib. How it is different from fill_between()
? After reading the documentation of fill_betweenx()
I tried to implement it:
x=np.linspace(0,2*3.14,50)
y=np.sin(x)
plt.figure(figsize=(10,5))
plt.fill_betweenx(y,2,3,color='b')
plt.plot(x,y)
As per my understanding, it should have filled the sine curve between x=2 and x=3 with a blue color, but I got:
Can anyone explain to me why it wasn't filled?
It seems you want to fill the sine curve, e.g. between y=0 and the sine. You may limit this fill to a range of x coordinates using where
.
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0,2*3.14,50)
y=np.sin(x)
plt.fill_between(x,y,0,where=(x>2) & (x<=3),color='b')
plt.plot(x,y)
In contrast you would use fill_betweenx
if you wanted to fill between a curve in x direction. E.g.
plt.fill_betweenx(x,y,where=(x>2) & (x<=3), color='b')
plt.plot(y,x)
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