2020-08-10

Python中使用Matplotlib绘图示例详解(15)--FillBetweenx演示

  • 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()
    标题
    标题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱转呼啦圈的小兔子

觉得文章不错?请小编喝杯咖啡吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值