In Matplotlib, what does the argument mean in fig.

2019-01-03 03:57发布

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?

5条回答
Bombasti
2楼-- · 2019-01-03 04:06

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

查看更多
Summer. ? 凉城
3楼-- · 2019-01-03 04:17

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.

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-03 04:22

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

EDIT: Some additional information

The following combinations produce asymmetrical arrangements of subplots.

subplot(2,2,[1 3])
subplot(2,2,2)
subplot(2,2,4)

Example 2

You can also use the colon operator to specify multiple locations if they are in sequence.

subplot(2,2,1:2)
subplot(2,2,3)
subplot(2,2,4)

enter image description here

Reference here

查看更多
The star\"
5楼-- · 2019-01-03 04:31

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

查看更多
唯我独甜
6楼-- · 2019-01-03 04:32

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

查看更多
登录 后发表回答