• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Matplotlib 绘制多图

武飞扬头像
Red Car
帮助1

Matplotlib 绘制多图

我们可以使用 pyplot 中的 subplot() 和 subplots() 方法来绘制多个子图。

subplot() 方法在绘图时需要指定位置,subplots() 方法可以一次生成多个,在调用时只需要调用生成对象的 ax 即可。

subplot

subplot(nrows, ncols, index, **kwargs)
subplot(pos, **kwargs)
subplot(**kwargs)
subplot(ax)

以上函数将整个绘图区域分成 nrows 行和 ncols 列,然后从左到右,从上到下的顺序对每个子区域进行编号 1...N ,左上的子区域的编号为 1、右下的区域编号为 N,编号可以通过参数 index 来设置。

设置 numRows = 1,numCols = 2,就是将图表绘制成 1x2 的图片区域, 对应的坐标为:

(1, 1), (1, 2)

plotNum = 1, 表示的坐标为(1, 1), 即第一行第一列的子图。

plotNum = 2, 表示的坐标为(1, 2), 即第一行第二列的子图。

实例

  1.  
    import matplotlib.pyplot as plt
  2.  
    import numpy as np
  3.  
     
  4.  
    #plot 1:
  5.  
    xpoints = np.array([0, 6])
  6.  
    ypoints = np.array([0, 100])
  7.  
     
  8.  
    plt.subplot(1, 2, 1)
  9.  
    plt.plot(xpoints,ypoints)
  10.  
    plt.title("plot 1")
  11.  
     
  12.  
    #plot 2:
  13.  
    x = np.array([1, 2, 3, 4])
  14.  
    y = np.array([1, 4, 9, 16])
  15.  
     
  16.  
    plt.subplot(1, 2, 2)
  17.  
    plt.plot(x,y)
  18.  
    plt.title("plot 2")
  19.  
     
  20.  
    plt.suptitle("W3Cschool subplot Test")
  21.  
    plt.show()
学新通

显示结果如下:

学新通

设置 numRows = 2,numCols = 2,就是将图表绘制成 2x2 的图片区域, 对应的坐标为:

(1, 1), (1, 2)
(2, 1), (2, 2)

plotNum = 1, 表示的坐标为(1, 1), 即第一行第一列的子图。

plotNum = 2, 表示的坐标为(1, 2), 即第一行第二列的子图。

plotNum = 3, 表示的坐标为(2, 1), 即第二行第一列的子图。

plotNum = 4, 表示的坐标为(2, 2), 即第二行第二列的子图。

实例

  1.  
    import matplotlib.pyplot as plt
  2.  
    import numpy as np
  3.  
     
  4.  
    #plot 1:
  5.  
    x = np.array([0, 6])
  6.  
    y = np.array([0, 100])
  7.  
     
  8.  
    plt.subplot(2, 2, 1)
  9.  
    plt.plot(x,y)
  10.  
    plt.title("plot 1")
  11.  
     
  12.  
    #plot 2:
  13.  
    x = np.array([1, 2, 3, 4])
  14.  
    y = np.array([1, 4, 9, 16])
  15.  
     
  16.  
    plt.subplot(2, 2, 2)
  17.  
    plt.plot(x,y)
  18.  
    plt.title("plot 2")
  19.  
     
  20.  
    #plot 3:
  21.  
    x = np.array([1, 2, 3, 4])
  22.  
    y = np.array([3, 5, 7, 9])
  23.  
     
  24.  
    plt.subplot(2, 2, 3)
  25.  
    plt.plot(x,y)
  26.  
    plt.title("plot 3")
  27.  
     
  28.  
    #plot 4:
  29.  
    x = np.array([1, 2, 3, 4])
  30.  
    y = np.array([4, 5, 6, 7])
  31.  
     
  32.  
    plt.subplot(2, 2, 4)
  33.  
    plt.plot(x,y)
  34.  
    plt.title("plot 4")
  35.  
     
  36.  
    plt.suptitle("W3Cschool subplot Test")
  37.  
    plt.show()
学新通

显示结果如下:

学新通

subplots()

subplots() 方法语法格式如下:

matplotlib.pyplot.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)

参数说明:

  • nrows:默认为 1,设置图表的行数。
  • ncols:默认为 1,设置图表的列数。
  •  
  • sharex、sharey:设置 x、y 轴是否共享属性,默认为 false,可设置为 'none'、'all'、'row' 或 'col'。 False 或 none 每个子图的 x 轴或 y 轴都是独立的,True 或 'all':所有子图共享 x 轴或 y 轴,'row' 设置每个子图行共享一个 x 轴或 y 轴,'col':设置每个子图列共享一个 x 轴或 y 轴。
  • squeeze:布尔值,默认为 True,表示额外的维度从返回的 Axes(轴)对象中挤出,对于 N*1 或 1*N 个子图,返回一个 1 维数组,对于 N*M,N>1 和 M>1 返回一个 2 维数组。如果设置为 False,则不进行挤压操作,返回一个元素为 Axes 实例的2维数组,即使它最终是1x1。
  • subplot_kw:可选,字典类型。把字典的关键字传递给 add_subplot() 来创建每个子图。
  • gridspec_kw:可选,字典类型。把字典的关键字传递给 GridSpec 构造函数创建子图放在网格里(grid)。
  • **fig_kw:把详细的关键字参数传给 figure() 函数。

实例

  1.  
    import matplotlib.pyplot as plt
  2.  
    import numpy as np
  3.  
     
  4.  
    # 创建一些测试数据 -- 图1
  5.  
    x = np.linspace(0, 2*np.pi, 400)
  6.  
    y = np.sin(x**2)
  7.  
     
  8.  
    # 创建一个画像和子图 -- 图2
  9.  
    fig, ax = plt.subplots()
  10.  
    ax.plot(x, y)
  11.  
    ax.set_title('Simple plot')
  12.  
     
  13.  
    # 创建两个子图 -- 图3
  14.  
    f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
  15.  
    ax1.plot(x, y)
  16.  
    ax1.set_title('Sharing Y axis')
  17.  
    ax2.scatter(x, y)
  18.  
     
  19.  
    # 创建四个子图 -- 图4
  20.  
    fig, axs = plt.subplots(2, 2, subplot_kw=dict(projection="polar"))
  21.  
    axs[0, 0].plot(x, y)
  22.  
    axs[1, 1].scatter(x, y)
  23.  
     
  24.  
    # 共享 x 轴
  25.  
    plt.subplots(2, 2, sharex='col')
  26.  
     
  27.  
    # 共享 y 轴
  28.  
    plt.subplots(2, 2, sharey='row')
  29.  
     
  30.  
    # 共享 x 轴和 y 轴
  31.  
    plt.subplots(2, 2, sharex='all', sharey='all')
  32.  
     
  33.  
    # 这个也是共享 x 轴和 y 轴
  34.  
    plt.subplots(2, 2, sharex=True, sharey=True)
  35.  
     
  36.  
    # 创建标识为 10 的图,已经存在的则删除
  37.  
    fig, ax = plt.subplots(num=10, clear=True)
  38.  
     
  39.  
    plt.show()
学新通

部分图表显示结果如下:

图1

学新通

图2

学新通

图3

学新通

图4

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgffjii
系列文章
更多 icon
同类精品
更多 icon
继续加载