美文网首页
matplot_01

matplot_01

作者: 平头哥2 | 来源:发表于2019-10-27 09:30 被阅读0次

    1.多边形绘图

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    """
    Created on Sun Oct 27 08:13:22 2019
    
    @author: snow
    """
    
    import matplotlib.pyplot as plt # 绘图的库
    import numpy as np #python科学计算的库
    
    
    x = np.linspace(0,2*np.pi,num=500) #x轴取值
    y = np.sin(x) #y轴取值
    
    #fill()填充函数曲线与坐标轴之间的区域:  alpha表示透明度
    plt.fill(x,y, color="cornflowerblue",alpha=0.2)
    # plot 绘制多边形的边框 color表示使用的颜色
    plt.plot(x,y, color="black",alpha=0.8)
    
    # 绘制中间红色横线
    plt.plot([x[0],x[-1]],[y[0],y[-1]], color="red",alpha=1) 
    
    plt.ylim(0,2*np.pi) # 表示设置x左右边界
    #plt.xlim(left=5)
    
    left, right = plt.xlim()# 表示获取x轴左右边界
    print(left)
    print(right)
    
    plt.ylim(-1.1,1.1)
    
    # np.arange(0,16.1,1) :产生从0到16.1的步长是1的序列
    #plt.xticks(np.arange(0,16.1,1)) # 绘制x轴显示的数值
    #plt.yticks(np.arange(0,16.1,1))
    plt.savefig("a.png")
    plt.show()#显示图片
    

    图片如下:


    a.png

    2. fill_between的使用

    ![b](D:\python\workspace\b.png)import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0,2,500)
    y1 = np.sin(2*np.pi*x)
    y2 = 1.2*np.sin(3*np.pi*x)
    
    
    fig,ax = plt.subplots(3,1,sharex="all")
    
    ax[0].fill_between(x,0,y2,alpha=0.5)
    ax[0].set_ylim(-1.3,1.3)
    
    
    ax[1].fill_between(x,y2,1.1,alpha=0.5)
    ax[1].set_ylim(-1.3,1.3)
    
    """
    x:第一个参数表示覆盖的区域,我直接复制为x,表示整个x都覆盖
    y1:表示覆盖的下限
    y2:表示覆盖的上限是y这个曲线
    color :覆盖区域的颜色
    alpha:覆盖区域的透明度[0,1],其值越大,表示越不透明
    """
    ax[2].fill_between(x,y1,y2,alpha=0.7,color='red')
    ax[2].plot(x,y1)
    ax[2].plot(x,y2,color="black")
    ax[2].set_xlim(0,2)
    ax[2].set_ylim(-1.3,1.3)
    
    plt.savefig("b.png")
    plt.show()
    
    b.png

    3. where 的使用

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0,2,500)
    y1 = np.sin(2*np.pi*x)
    y2 = 1.1*np.sin(3*np.pi*x)
    
    fig = plt.figure()
    
    ax = fig.add_subplot(111)
    
    # plot y1 and plot y2
    ax.plot(x,y1,color="k",lw=1,ls="-")
    ax.plot(x,y2,color="k",lw=1,ls="-")
    
    # "where y1 <= y2"
    # 结果如图所示
    ax.fill_between(x,y1,y2,where=y2>=y1,interpolate=True,
                    facecolor="cornflowerblue",alpha=0.7)
    
    # where y1>= y2
    ax.fill_between(x,y1,y2,where=y2<=y1,interpolate=True,
                    facecolor="darkred",alpha=0.7)
    
    ax.set_xlim(0,2)
    ax.set_ylim(-1.2,1.2)
    
    # 添加虚线方格
    ax.grid(ls=":",lw=1,color="gray",alpha=0.5)
    
    plt.savefig("04.png")
    plt.show()
    
    04.png

    4. x 和 y 互换

    import matplotlib.pyplot as plt
    import numpy as np
    
    y = np.linspace(0,2,500)
    x1 = np.sin(2*np.pi*y)
    x2 = 1.1*np.sin(3*np.pi*y)
    
    fig = plt.figure()
    
    ax = fig.add_subplot(111)
    
    # plot x1 and plot x2
    ax.plot(x1,y,color="k",lw=1,ls="-")
    ax.plot(x2,y,color="k",lw=1,ls="-")
    
    # "where x1 <= x2"
    ax.fill_betweenx(y,x1,x2,where=x2>=x1,facecolor="cornflowerblue",alpha=0.7)
    
    # where x1>= x2
    ax.fill_betweenx(y,x1,x2,where=x2<=x1,facecolor="darkred",alpha=0.7)
    
    ax.set_xlim(-1.2,1.2)
    ax.set_ylim(0,2)
    
    ax.grid(ls=":",lw=1,color="gray",alpha=0.5)
    
    plt.savefig("05.png")
    plt.show()
    
    05.png

    **5. mask **

    import matplotlib.pyplot as plt
    import numpy as np
    
    fig,ax = plt.subplots(1,2)
    
    # subplot(121) data
    x = np.linspace(0,2,500)
    y1 = np.sin(2*np.pi*x)
    y2 = 1.2*np.sin(3*np.pi*x)
    
    y2 = np.ma.masked_greater(y2,1.0)
    
    # plot y1 and plot y2
    ax[0].plot(x,y1,color="k",lw=1,ls="-")
    ax[0].plot(x,y2,color="k",lw=1,ls="-")
    
    # "where y1 <= y2"
    ax[0].fill_between(x,y1,y2,where=y2>=y1,facecolor="cornflowerblue",alpha=0.7)
    
    # where y1>= y2
    ax[0].fill_between(x,y1,y2,where=y2<=y1,facecolor="darkred",alpha=0.7)
    
    ax[0].set_xlim(0,2)
    ax[0].set_ylim(-1.2,1.2)
    
    ax[0].grid(ls=":",lw=1,color="gray",alpha=0.5)
    
    # subplot(122) data
    y = np.linspace(0,2,500)
    x1 = np.sin(2*np.pi*y)
    x2 = 1.2*np.sin(3*np.pi*y)
    
    x2 = np.ma.masked_greater(x2,1.0)
    
    # plot x1 and plot x2
    ax[1].plot(x1,y,color="k",lw=1,ls="-")
    ax[1].plot(x2,y,color="k",lw=1,ls="-")
    
    # "where x1 <= x2"
    ax[1].fill_betweenx(y,x1,x2,where=x2>=x1,facecolor="cornflowerblue",alpha=0.7)
    
    # where x1>= x2
    ax[1].fill_betweenx(y,x1,x2,where=x2<=x1,facecolor="darkred",alpha=0.7)
    
    ax[1].set_xlim(-1.2,1.2)
    ax[1].set_ylim(0,2)
    
    ax[1].grid(ls=":",lw=1,color="gray",alpha=0.5)
    
    plt.savefig("06.png")
    plt.show()
    
    06.png

    https://www.jianshu.com/p/7f299f1c1169

    相关文章

      网友评论

          本文标题:matplot_01

          本文链接:https://www.haomeiwen.com/subject/atspvctx.html