What are the differences between add_axes and add_subplot?

In the previous answer, I was recommended to use add_subplotinstead add_axesto show the axes correctly, but when searching for documentation I could not understand when and why I should use one of these functions.

Can someone explain the differences?

+31
source share
1 answer

General grounds

Both, add_axesand add_subplotadd axis to the figure. They both return an object (subclass a) matplotlib.axes.Axes.

However, the mechanism used to add axes is significantly different.

add_axes

add_axes - add_axes(rect), rect - [x0, y0, width, height], coodinates (x0,y0) . , . .

fig = plt.figure()
ax = fig.add_axes([0,0,1,1])

, .

add_subplot

add_subplot . , , . - 3- ,

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

(1) 2 3 . add_subplot(111) ( 1 1). ( matplotlib add_subplot() ' - .)

, matplotlib . add_subplot(111) , [0.125,0.11,0.775,0.77] , (). , .. pyplot.subplots_adjust(...) pyplot.tight_layout().

add_subplot . , , add_axes .

import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (5,3)

fig = plt.figure()
fig.add_subplot(241)
fig.add_subplot(242)
ax = fig.add_subplot(223)
ax.set_title("subplots")

fig.add_axes([0.77,.3,.2,.6])
ax2 =fig.add_axes([0.67,.5,.2,.3])
fig.add_axes([0.6,.1,.35,.3])
ax2.set_title("random axes")

plt.tight_layout()
plt.show()

enter image description here

- plt.subplots().

fig, ax = plt.subplots()

, ,

fig, axes = plt.subplots(nrows=3, ncols=4)

fig.add_axes([0,0,1,1]), . , , , , , . fig.add_subplot, , , , pyplot.subplots_adjust(...) pyplot.tight_layout().

+70

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


All Articles