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:
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?
fig.add_subplot(ROW,COLUMN,POSITION)
Examples
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
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.
I think this would be best explained by the following picture:
To initialize the above, one would type:
EDIT: Some additional information
The following combinations produce asymmetrical arrangements of subplots.
You can also use the colon operator to specify multiple locations if they are in sequence.
Reference here
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)
isadd_subplot(1, 1, 1)
.My solution is