zoukankan      html  css  js  c++  java
  • Matplotlib新手上路(中)

    上回继续

    一、多张图布局(subplot)

    1.1 subplot布局方式

    import matplotlib.pyplot as plt
    
    plt.figure()
    
    plt.subplot(3, 2, 1)  # 3行2列的第1张图
    plt.plot([0, 1], [0, 1])
    
    plt.subplot(322)  # 等效于plt.subplot(2,2,2) 3行2列的第2张图
    plt.plot([1, 1], [0, 2])
    plt.plot([0, 2], [1, 1])
    
    plt.subplot(3, 1, 2)  # 3行1列的第"2"张图,3行1列的"前提"下,上面一行已占用了1个位置,所以这里是位置2
    plt.scatter([0, 1, 2], [1, 1, 1], c="r", s=50)
    
    plt.subplot(3, 3, 7)  # 第3行的第1张图,3行3列的"前提"下,前面二行,已经用掉了6个位置,所以这里是位置7
    plt.plot([6, 9], [9, 6])
    
    plt.subplot(3, 3, 8)  # 第3行中间的位置
    plt.plot([1, 2], [2, 2])
    
    plt.subplot(3, 3, 9)  # 第3行右侧的位置
    plt.plot([1, 3], [2, 4])
    
    plt.show()
    

    上面演示的是“行合并”的布局示例,如果想要“列合并”的效果,参考下面的代码:

    import matplotlib.pyplot as plt
    
    plt.figure()
    
    plt.subplot(2, 2, 1)  # 2行2列的位置1
    plt.plot([0, 1], [0, 1])
    plt.text(0.5, 0, "figure-1", )
    
    plt.subplot(1, 2, 2)  # 1行2列的位置2
    plt.plot([0, 1], [0, 1])
    plt.text(0.5, 0, "figure-2")
    
    plt.subplot(2, 2, 3)  # 2行2列的位置3
    plt.plot([0, 1], [0, 1])
    plt.text(0.5, 0, "figure-3")
    
    plt.show()
    

    1.2 subplot2grid布局方式 

    这种方式类似于网页制作中的table布局

    import matplotlib.pyplot as plt
    
    plt.figure()
    
    ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)  # 3行3列, 第0行0列,合并3列
    ax1.text(0.5, 0.5, r"$ax-1$")
    
    ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)  # 3行3列, 第1行0列(即:第二行最左边的位置),合并2列
    ax2.text(0.5, 0.5, r"$ax-2$")
    
    ax3 = plt.subplot2grid((3, 3), (2, 0))  # 3行3列, 第1行0列(即:第三行第1个位置)
    ax3.text(0.5, 0.5, r"$ax-3$")
    
    ax4 = plt.subplot2grid((3, 3), (2, 1))  # 3行3列, 第2行1列(即:第三行第2个位置)
    ax4.text(0.5, 0.5, r"$ax-4$")
    
    ax5 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)  # 3行3列, 第1行2列(即:第二行第3个位置),跨2行
    ax5.text(0.5, 0.5, r"$ax-5$")
    
    plt.show()
    

    1.3 gridspec布局方式

    这与1.2很类似,只是换一个写法而已

    import matplotlib.pyplot as plt
    import matplotlib.gridspec as gridspec
    
    gs = gridspec.GridSpec(3, 3)  # 定义3行3列的网络
    ax1 = plt.subplot(gs[0:1, 0:3])  # 第0行,[0,3)之间的列合并
    ax1.text(0.5, 0.5, r"$ax-1$")
    
    ax2 = plt.subplot(gs[1, :-1])  # 第1行,[0,倒数第1列]之间的列合并
    ax2.text(0.5, 0.5, r"$ax-2$")
    
    ax3 = plt.subplot(gs[2, 0])  # 第2行,第0列
    ax3.text(0.5, 0.5, r"$ax-3$")
    
    ax4 = plt.subplot(gs[2, 1])  # 第2行,第1列
    ax4.text(0.5, 0.5, r"$ax-4$")
    
    ax5 = plt.subplot(gs[1:0, 2])  # [1,最后1列]行合并,第2列
    ax5.text(0.5, 0.5, r"$ax-5$")
    
    plt.show()

    、柱状图

    import matplotlib.pyplot as plt
    import numpy as np
    
    X = [1, 2, 3, 4]
    Y1 = [1000, 1500, 1200, 1800]
    Y2 = np.array(Y1) * (-1)
    
    plt.bar(X, Y1, 0.4, color="green", label="label1")
    plt.bar(X, Y2, 0.4, color="orange", label="label2")
    
    plt.xticks(X)
    
    ax1 = plt.gca()
    ax1.set_xticklabels(["Q1", "Q2", "Q3", "Q4"])
    ax1.spines['top'].set_color('none')
    ax1.spines['right'].set_color('none')
    
    ax1.spines['bottom'].set_position(('data', 0))
    
    plt.legend()
    
    plt.show()
    

     

    、3D图

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib import cm
    from mpl_toolkits.mplot3d import Axes3D
    
    X = np.arange(-5, 5, 0.25)
    Y = np.arange(-5, 5, 0.25)
    X, Y = np.meshgrid(X, Y)
    
    Z = np.sin(X) + np.cos(Y)
    
    fig = plt.figure()
    ax = Axes3D(fig)
    ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.viridis)
    
    plt.show()
    

      

  • 相关阅读:
    Javascript操纵Cookie--转
    java使用jsp servlet来防止csrf 攻击的实现方法
    Creating a CSRF protection with Spring 3.x--reference
    Preventing CSRF in Java web apps---reference
    Linux服务器集群系统(四)--转
    Linux服务器集群系统(三)--转
    UVA 10529 Dumb Bones 可能性dp 需求预期
    android 渐变drawable
    定义你自己ViewGroup
    Android系统关机或几种方式重启
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/matplotlib-tutorial-2.html
Copyright © 2011-2022 走看看