pleasedontbelong
pleasedontbelong

Reputation: 20102

What does the argument mean in fig.add_subplot(111)?

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?

Upvotes: 602

Views: 553800

Answers (8)

Mahmoud Magdy
Mahmoud Magdy

Reputation: 931

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

Upvotes: 3

compuphys
compuphys

Reputation: 1377

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.

Upvotes: 21

Pritz
Pritz

Reputation: 472

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.

Upvotes: 34

SaiyanGirl
SaiyanGirl

Reputation: 17024

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

Upvotes: 637

Rameez Ahmad Dar
Rameez Ahmad Dar

Reputation: 871

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

Upvotes: 18

yoonghm
yoonghm

Reputation: 4625

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

Upvotes: 40

DaveTM
DaveTM

Reputation: 756

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.

Upvotes: 55

Constantin
Constantin

Reputation: 28164

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

Upvotes: 627

Related Questions