Figure.add_subplot () vs pyplot.subplot ()

What is the difference between add_subplot()and subplot()? They both seem to add a subplot if they are not there. I looked at the documentation, but I could not make out the difference. Is it just to make future code more flexible?

For example:

fig = plt.figure()
ax = fig.add_subplot(111)

vs

plt.figure(1)
plt.subplot(111)

from matplotlib tutorials.

+12
source share
2 answers

If you need a link axfor later reference:

ax = fig.add_subplot(111)

gives you one:

plt.subplot(111)

you need to do something like:

ax = plt.gca()

Similarly, if you want to manipulate the figure later:

fig = plt.figure()

gives you a link right away, not:

fig = plt.gcf()

Obtaining explicit links is even more useful if you are working with multiple digit subtasks. For comparison:

figures = [plt.figure() for _ in range(5)]

from:

figures = []
for _ in range(5):
    plt.figure()
    figures.append(plt.gcf())
+8

pyplot.subplot - Figure.add_subplot . pyplot.subplot , . , Figure.add_subplot pyplot.axes.

0

Source: https://habr.com/ru/post/1621519/


All Articles