FillBetweenx演示 import matplotlib.pyplot as plt
import numpy as np
y=np.arange(0.0,2.0,0.01)
x1=np.sin(2*np.pi*y)
x2=1.2*np.sin(4*np.pi*y)
fig,[ax1,ax2,ax3]=plt.subplots(1,3,sharey=True,figsize=(6,6))
#首先就是创建数据,然后打开画板,设置画布
ax1.fill_betweenx(y,0,x1)
#fill_between(y,0,x1)实际上是一个y配上0和x1
ax1.set_title("between (x1,0)")
ax1.set_xlabel("x")
#set_xlabel()就是对x轴进行标记
ax2.fill_betweenx(y,x1,1)
ax2.set_title("between (x1,1")
ax2.set_xlabel("x")
ax3.fill_betweenx(y,x1,x2)
#注意这里使用的是fill_betweenx()哦
ax3.set_title("between (x1,x2)")
fig,[ax,ax1]=plt.subplots(1,2,sharey=True,figsize=(6,6))
ax.plot(x1,y,x2,y,color="black")
ax.fill_betweenx(y,x1,x2,where=x2>=x1,facecolor="green")
ax.fill_betweenx(y,x1,x2,where=x2<=x1,facecolor="red")
ax.set_title("fill_between where")
#因为x1和x2之间有大有小,为了突出进行显示,进行了where设置,并将其设置成不同的颜色
x2=np.ma.masked_greater(x2,1.0)
#numpy.ma.masked_greater(x,value,copy=True)屏蔽大于1.0的值
ax1.plot(x1,y,x2,y,color="black")
#使用plot绘制两条曲线的时候,x和y要一一对应,但是使用fill_between()的时候则要可以一对多
ax1.fill_betweenx(y,x1,x2,where=x2>=x1,facecolor="green")
ax1.fill_betweenx(y,x1,x2,where=x2<=x1,facecolor="red")
ax1.set_title("regions with x2>1 are masked")
plt.show()