Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between fill_between and fill_betweenx matplotlib

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?

like image 971
user566577 Avatar asked Oct 18 '25 20:10

user566577


1 Answers

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)

enter image description here

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)

enter image description here

like image 136
ImportanceOfBeingErnest Avatar answered Oct 21 '25 09:10

ImportanceOfBeingErnest