【问题标题】:In Matplotlib, what does the argument mean in fig.add_subplot(111)?在 Matplotlib 中,fig.add_subplot(111) 中的参数是什么意思?
【发布时间】:2011-04-04 19:55:13
【问题描述】:

有时我会遇到这样的代码:

import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
fig = plt.figure()
fig.add_subplot(111)
plt.scatter(x, y)
plt.show()

产生:

我一直在疯狂地阅读文档,但找不到111 的解释。有时我会看到212

fig.add_subplot()的参数是什么意思?

【问题讨论】:

  • 你好。请问您找到答案了吗? fig.add_subplot(111) 是什么意思?

标签: python matplotlib figure


【解决方案1】:

我认为这最好用下图来解释:

要初始化上述内容,可以键入:

import matplotlib.pyplot as plt
fig = plt.figure()
fig.add_subplot(221)   #top left
fig.add_subplot(222)   #top right
fig.add_subplot(223)   #bottom left
fig.add_subplot(224)   #bottom right 
plt.show()

【讨论】:

  • 我觉得带逗号的版本比带3位数字的版本直观易读得多
  • 这很好,它展示了如何“在网格中定位子图”是像上图示例这样的 2x2 网格或 4x4 图形的网格等。但是有人可以解释一下是什么决定了显示的那些图表。我发现 (2,2,1) 的默认值很小,我希望它可能是那个大小的两倍或更多。
  • 这很有帮助。这张图片如果不是方形 (2x2) 会更好一些,但在其他方面非常有用。
  • @TravisJ 哇,真是巧合!在你写这篇文章的一个小时前,我也在想同样的事情!我很快就会改变它:)
  • 非常有帮助的答案!虽然我认为编辑不准确。 Matplotlib 不支持使用 1:2 或 [1 3] 等语句跨越子图的行为(至少在 1.5.0 中不支持),并且似乎仅限于 Matlab。 (注意 Matplotlib 中还有其他选项可以实现此行为,例如 gridspec
【解决方案2】:

这些是编码为单个整数的子图网格参数。例如,“111”表示“1x1 网格,第一个子图”,“234”表示“2x3 网格,第 4 个子图”。

add_subplot(111) 的替代形式是 add_subplot(1, 1, 1)

【讨论】:

  • 有这方面的详细文档吗?在这种情况下,1x1 网格和 2x3 网格有什么区别?
  • 1x1 网格 = 1 行,1 列。 2x3 网格 = 2 行,3 列。第三个数字从 1 开始并按行递增。有关详细信息,请参阅 subplot() 的文档。
  • 正如其他人解释的那样(两年多前),这是 matlab 的遗产。但为了未来的读者,您应该知道存在更好的替代方案,即subplots() 方法。
  • 旧版 subplot() 的文档是 heresubplots()here
【解决方案3】:

Constantin 的回答是正确的,但对于更多背景知识,这种行为是从 Matlab 继承的。

Matlab 的行为在 Matlab 文档的Figure Setup - Displaying Multiple Plots per Figure 部分进行了解释。

subplot(m,n,i) 将图形窗口分解为一个 m×n 的小矩阵 子图并选择当前图的子图。地块 沿图形窗口的顶行编号,然后是第二个 行,以此类推。

【讨论】:

  • 这是 matplotlib,不是 matlab。
  • matplotlibs 的大部分行为都继承自 Matlab。由于 Matlab 文档更好,我认为它可能有助于解释这个特定函数调用的行为方式。是的,你是对的,这个(问题)是关于 matplotlib 的。在我看来,matplotlib subplot documentation 不太清楚。
  • MATLAB 文档has moved.
【解决方案4】:

我的解决办法是

fig = plt.figure()
fig.add_subplot(1, 2, 1)   #top and bottom left
fig.add_subplot(2, 2, 2)   #top right
fig.add_subplot(2, 2, 4)   #bottom right 
plt.show()

【讨论】:

    【解决方案5】:

    import matplotlib.pyplot as plt
    plt.figure(figsize=(8,8))
    plt.subplot(3,2,1)
    plt.subplot(3,2,3)
    plt.subplot(3,2,5)
    plt.subplot(2,2,2)
    plt.subplot(2,2,4)
    

    第一个代码在具有 3 行和 2 列的布局中创建第一个子图。

    第一列中的三个图表示 3 行。第二个图位于同一列中第一个图的下方,依此类推。

    最后两个图有参数(2, 2),表示第二列只有两行,位置参数逐行移动。

    【讨论】:

      【解决方案6】:

      fig.add_subplot(ROW,COLUMN,POSITION)

      • ROW=行数
      • COLUMN=列数
      • POSITION= 您正在绘制的图形的位置

      示例

      `fig.add_subplot(111)` #There is only one subplot or graph  
      `fig.add_subplot(211)`  *and*  `fig.add_subplot(212)` 
      

      总共有 2 行 1 列,因此可以绘制 2 个子图。它的位置是第一。总共有 2 行 1 列,因此可以绘制 2 个子图。它的位置是第 2 个

      【讨论】:

        【解决方案7】:

        add_subplot() 方法有几个调用签名:

        1. add_subplot(nrows, ncols, index, **kwargs)
        2. add_subplot(pos, **kwargs)
        3. add_subplot(ax)
        4. add_subplot()

        调用 1 和 2:

        调用 1 和 2 彼此实现相同的目的(有一个限制,如下所述)。将它们视为首先使用其前 2 个数字(2x2、1x8、3x4 等)指定网格布局,例如:

        f.add_subplot(3,4,1) 
        # is equivalent to:
        f.add_subplot(341)
        

        两者都产生了 3 行 4 列中 (3 x 4 = 12) 个子图的子图排列。每次调用中的第三个数字表示要返回哪个轴对象,从左上角的1开始,向右递增

        这段代码说明了使用调用 2 的局限性:

        #!/usr/bin/env python3
        import matplotlib.pyplot as plt
        
        def plot_and_text(axis, text):
          '''Simple function to add a straight line
          and text to an axis object'''
          axis.plot([0,1],[0,1])
          axis.text(0.02, 0.9, text)
        
        f = plt.figure()
        f2 = plt.figure()
        
        _max = 12
        for i in range(_max):
          axis = f.add_subplot(3,4,i+1, fc=(0,0,0,i/(_max*2)), xticks=[], yticks=[])
          plot_and_text(axis,chr(i+97) + ') ' + '3,4,' +str(i+1))
        
          # If this check isn't in place, a 
          # ValueError: num must be 1 <= num <= 15, not 0 is raised
          if i < 9:
            axis = f2.add_subplot(341+i, fc=(0,0,0,i/(_max*2)), xticks=[], yticks=[])
            plot_and_text(axis,chr(i+97) + ') ' + str(341+i))
        
        f.tight_layout()
        f2.tight_layout()
        plt.show()
        

        您可以看到,使用 在 LHS 上调用 1 可以返回任何轴对象,但是使用 在 RHS 上调用 2 您最多只能返回 index = 9 的渲染使用此调用无法访问子图 j)、k) 和 l)。

        即它说明了这一点from the documentation:

        pos 是一个三位整数,其中第一位是行数,第二位是列数,第三位是子图的索引。即 fig.add_subplot(235) 与 fig.add_subplot(2, 3, 5) 相同。 请注意,所有整数都必须小于 10 才能使用此表单


        呼叫 3

        在极少数情况下,可以使用单个参数调用 add_subplot,即已在当前图中创建的子图轴实例,但不在图的轴列表中。


        调用 4(自 3.1.0 起):

        如果没有传递位置参数,则默认为 (1, 1, 1)。

        即,在问题中重现调用 fig.add_subplot(111)。这实际上设置了一个 1 x 1 的子图网格并返回网格中的第一个(也是唯一一个)轴对象。

        【讨论】:

          【解决方案8】:

          fig.add_subplot(111) 就像fig.add_subplot(1, 1, 1)111 只是子图网格参数,但编码为单个整数。

          要在 n*m 网格 中选择 第 k 个子图,请执行以下操作:fig.add_subplot(n, m, k)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-10-17
            • 1970-01-01
            • 1970-01-01
            • 2012-07-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多