Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Matplotlib, 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?

like image 284
pleasedontbelong Avatar asked Aug 27 '10 13:08

pleasedontbelong


People also ask

Is one of the arguments of plot () function in matplotlib?

plot() Function. The plot() function in pyplot module of matplotlib library is used to make a 2D hexagonal binning plot of points x, y. Parameters: This method accept the following parameters that are described below: x, y: These parameter are the horizontal and vertical coordinates of the data points.

What is matplotlib fig?

figure. The figure module provides the top-level Artist , the Figure , which contains all the plot elements. The following classes are defined SubplotParams control the default spacing of the subplots Figure. Top level container for all plot elements.

What does Fig ax mean in Python?

subplots method provides a way to plot multiple plots on a single figure. Given the number of rows and columns , it returns a tuple ( fig , ax ), giving a single figure fig with an array of axes ax .

What does %Matplotlib mean in Python?

%matplotlib inline sets the backend of matplotlib to the 'inline' backend: With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.


1 Answers

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() 
like image 119
SaiyanGirl Avatar answered Oct 05 '22 08:10

SaiyanGirl