Fix Python – In Matplotlib, what does the argument mean in fig.add_subplot(111)?

Question

Asked By – pleasedontbelong

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?

Now we will see solution for issue: In Matplotlib, what does the argument mean in fig.add_subplot(111)?


Answer

These are subplot grid parameters encoded as a single integer. For example, “111” means “1×1 grid, first subplot” and “234” means “2×3 grid, 4th subplot”.

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

This question is answered By – Constantin

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0