Fig.add_subplot(111) 中的参数是什么意思?

发布于 2024-09-16 03:28:23 字数 424 浏览 11 评论 0原文

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

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()

生成:

Exampleplot generated by the returned code

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

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

Sometimes I come across code such as this:

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()

Which produces:

Example plot produced by the included code

I've been reading the documentation like crazy but I can't find an explanation for the 111. sometimes I see a 212.

What does the argument of fig.add_subplot() mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

猫烠⑼条掵仅有一顆心 2024-09-23 03:28:23

我认为下面的图片可以最好地解释这一点:

在此处输入图像描述

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

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()

I think this would be best explained by the following picture:

enter image description here

To initialize the above, one would type:

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()
亽野灬性zι浪 2024-09-23 03:28:23

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

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

These are subplot grid parameters encoded as a single integer. For example, "111" means "1x1 grid, first subplot" and "234" means "2x3 grid, 4th subplot".

Alternative form for add_subplot(111) is add_subplot(1, 1, 1).

長街聽風 2024-09-23 03:28:23

Constantin 的答案很准确,但更多背景信息是从 Matlab 继承的。

图形设置 - 每个图形显示多个图中解释了 Matlab 行为Matlab 文档的部分。

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

The answer from Constantin is spot on but for more background this behavior is inherited from Matlab.

The Matlab behavior is explained in the Figure Setup - Displaying Multiple Plots per Figure section of the Matlab documentation.

subplot(m,n,i) breaks the figure window into an m-by-n matrix of small
subplots and selects the ithe subplot for the current plot. The plots
are numbered along the top row of the figure window, then the second
row, and so forth.

阪姬 2024-09-23 03:28:23

我的解决方案是

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()

 2x2 网格,1 和 3 合并

My solution is

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()

2x2 grid with 1 and 3 merge

甜`诱少女 2024-09-23 03:28:23

输入图像描述这里

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) 表示第二列只有两行,位置参数按行移动。

enter image description here

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)

The first code creates the first subplot in a layout that has 3 rows and 2 columns.

The three graphs in the first column denote the 3 rows. The second plot comes just below the first plot in the same column and so on.

The last two plots have arguments (2, 2) denoting that the second column has only two rows, the position parameters move row wise.

戏蝶舞 2024-09-23 03:28:23

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

  1. add_subplot(nrows, ncols, index, **kwargs)
  2. add_subplot(pos, **kwargs)
  3. add_subplot(ax)
  4. add_subplot() <-- 从 3.1.0 开始

调用 1 和 2:

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

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

两者都会生成 3 行中 (3 x 4 = 12) 个子图的子图排列和 4 列。每次调用中的第三个数字指示要返回哪个轴对象,从左上角的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()

subplots

您可以看到,使用左轴上的调用 1 可以返回任何轴对象,但是使用右轴上的调用 2 > 使用此调用只能返回索引 = 9 的子图 j)、k) 和 l) 无法访问。

即它从文档中说明了这一点

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 的子图网格,并返回网格中的第一个(也是唯一的)轴对象。

The add_subplot() method has several call signatures:

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

Calls 1 and 2:

Calls 1 and 2 achieve the same thing as one another (up to a limit, explained below). Think of them as first specifying the grid layout with their first 2 numbers (2x2, 1x8, 3x4, etc), e.g:

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

Both produce a subplot arrangement of (3 x 4 = 12) subplots in 3 rows and 4 columns. The third number in each call indicates which axis object to return, starting from 1 at the top left, increasing to the right.

This code illustrates the limitations of using call 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()

subplots

You can see with call 1 on the LHS you can return any axis object, however with call 2 on the RHS you can only return up to index = 9 rendering subplots j), k), and l) inaccessible using this call.

I.e it illustrates this point from the documentation:

pos is a three digit integer, where the first digit is the number of rows, the second the number of columns, and the third the index of the subplot. i.e. fig.add_subplot(235) is the same as fig.add_subplot(2, 3, 5). Note that all integers must be less than 10 for this form to work.


Call 3

In rare circumstances, add_subplot may be called with a single argument, a subplot axes instance already created in the present figure but not in the figure's list of axes.


Call 4 (since 3.1.0):

If no positional arguments are passed, defaults to (1, 1, 1).

i.e., reproducing the call fig.add_subplot(111) in the question. This essentially sets up a 1 x 1 grid of subplots and returns the first (and only) axis object in the grid.

初心 2024-09-23 03:28:23

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个

fig.add_subplot(ROW,COLUMN,POSITION)

  • ROW=number of rows
  • COLUMN=number of columns
  • POSITION= position of the graph you are plotting

Examples

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

There are total 2 rows,1 column therefore 2 subgraphs can be plotted. Its location is 1st. There are total 2 rows,1 column therefore 2 subgraphs can be plotted.Its location is 2nd

紫瑟鸿黎 2024-09-23 03:28:23

一些规则:

  1. 除了一种情况外,所有情况下都有 3 个数字和第三个数字
    必须小于或等于先前最大的数字。
  2. 最后一个数字的位置从左上角开始,右上角,然后是左下角,右下角,(注意,如果宽度或高度中间或以上,则可以增加)例如,如果宽度有 4 个位置,高度有 2 个位置,则顶部左、上第二、上第三、上第四和左下、下第二、下第三和下第四(所以最后一个数字将有 8 个值,从左上角开始到右下角结束。

(y)第一个数字,定义如何y 或顶部和底部(垂直)所需的空格示例 1 表示所有页面仅包含 1 张图表,2 表示顶部可以有一张图表,底部可以有一张图表,3 表示顶部可以有一张图表,中间可以有一张图表,底部可以有 1 个图表以此类推

(x) 第二个数字,定义左侧和右侧需要多少个空格,示例 1 表示 1 个图表的页面全宽,2 表示页面可以包含左侧 1 个图表和右侧 1 个图表,3 表示页面可以包含 1 个图表左边,一个在中间,一个在右边,就像上一个一样,但是对于左边和右边,就像画布中的 x

最后定义图表的位置示例:如果我们有 1,2,1,那么 1 表示一个图表的全高,2 表示有 2 个页面宽度图表,一个在左边,一个在右边,那么最后 1 表示从左边开始,如果 2 表示放在第二个位置,右边,如果 1,3,1 那么我们有 3 个位置左,中,右,所以最后一个1将在左边,如果它是1、3、2,那么最后2意味着中心,如果3它将在右边,依此类推,记住第一条规则最后一个数字必须低于前一个,因为这定义了一个位置,

例如2, 1,1 : 2 表示 y 有 2 个位置,表示一个在顶部,一个在底部,第二个 1 表示图表的全宽,因此图表将采用全宽,最后一个将基于第一个数字,因此将从顶部,如果

2,1,2 最后 2 表示第二名,即底部
(例如仅 1 个图表)
y,x,z (1,1,1) 或 111 这意味着整页有 1 个图表

,所以让我们看一些带有图像的示例

示例 1:(1,3,2) 或 132

“在此处输入图像描述"

  1. (1) 表示任何图表都将占据整页高度。
  2. (3)表示图表有左、中、右3个位置
  3. (2)表示图表居中第二位显示

示例 2:(3,1,3):

“在此处输入图像描述"

  1. (3) 表示页面高度分为3处。
  2. (1)表示页面宽度分为1处(全宽)
  3. (3) 表示在第三个可用位置显示图表,因此将位于底部,因为我们只有顶部、中间和底部,因此 3 表示底部

请注意常用的 111 表示 1 个图表在整页中 (1) 表示全高,第二个 (1) 表示全宽,第 1 个位置,请注意,如果我们将其设置为 112 或 113,我们将收到错误,因为顶部和底部仅可用 1 个位置,左侧和左侧各 1 个位置是的,为了增加最后一个数字,您必须有任何先前的数字大于或等于最后一个数字。


最后一点:(224)这里我们在顶部和底部有 2 个位置,在左侧和右侧有 2 个位置,因此在这种情况下,最后一个数字可以比这两个位置都大,因为有 2 个位置从左到右开始,其他 2 个位置将一起使用时是顶部和底部 1 将在左上角,2 将在右上角,3 将在左下角,4 将在右下角最后一个位置,

所以

221在此处输入图像描述


222

“在此处输入图像描述"


223

“在此处输入图像描述"


224在此处输入图像描述

希望我能很好地描述它,如果有任何语法错误,请原谅我

some rules:

  1. there are 3 numbers and the third number in all cases except one case
    must be lower or equal that biggest previous number.
  2. the placemnt of last number start from top left, top right, then bottom left, bottom right, (note can incrased if there middle or more in width or in height) for example if there 4 places in width and 2 places for height so top left, top second, top third, top fourth, and bottom left, bottom second, bottom third and bottom fourth (so last number will have 8 values start from top left and end at bottom right.

(y) the first number, define how spaces needed for y or top and bottom (vertical) example 1 means all page include 1 chart only, 2 means can have one at top and one at bottom, 3 means can have one chart at top, one at middle and 1 and bottom and so on

(x) the second number, define how many spaces needed for left and right, example 1 means full width of page for 1 chart, 2 means page can include one at left and one at right, 3 means can have 1 chart at left and one at middle and one at right like prev but for left and right like x in canvas for example

last define the placement of chart example: if we have 1,2,1 so 1 means full height for one chart, and 2 means there are 2 charts for page width one at left and one at right, then last 1 means start at left, if 2 means put at second placment which right, if 1,3,1 so we have 3 places left, center, right so last 1 will be left, if it 1, 3, 2 so last 2 means center, if 3 it will be at right and so on, remember first rules the last number must be lower than previous as this define one place

for example 2,1,1 : 2 means there are 2 places for y which mean one at top and one at bottom, and second 1 means full width for chart so chart will take full width, last one will be based on first number so it will start from top, and if

2,1,2 the last 2 means second place which means bottom
(for example 1 chart only)
y,x,z (1,1,1) or 111 this means 1 chart in full page

so lets see some examples with image

Example1: (1,3,2) or 132

enter image description here

  1. (1) means any chart will take full page height.
  2. (3) means there are 3 places for charts left, center, right
  3. (2) means display chart in center the second place

Example 2: (3,1,3):

enter image description here

  1. (3) It means page height divided into 3 places.
  2. (1) means page width divided into 1 place (full width)
  3. (3) means display chart in third avail position so will be bottom becuase we have only top, middle, and bottom so 3 means bottom

Note the usally used 111 means 1 chart in full page (1) means full height, second (1) means full width, and 1 first position, note if we make it 112 or 113 we will get error as only aviable 1 place for top and bottom and 1 place for left and right, in order to incerase last number you must have any previous number bigger or equal that the last .


last point: (224) here we have 2 places at top and bottom, and 2 places at left and right so in that case last number can be bigger than both as there 2 places start from left to right, and other 2 places which will be top and bottom when used together 1 will be top left, 2 will be top right, 3 will be bottom left and 4 will be bottom right the last place

so

221: enter image description here


222:

enter image description here


223:

enter image description here


224: enter image description here

hope i could describe it well and forgive me if any grammar mistakes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文