user32882
user32882

Reputation: 5907

bounds for matplotlib contourf plot not making sense

I'm generating the following contour plot + colorbar in matplotlib:

enter image description here

I extract the relative bounds of the resulting axes using the following loop:

fig = plt.gcf()

for ax in fig.get_axes():
    print(ax.get_position().bounds)

and obtain

(0.125, 0.10999999999999999, 0.62, 0.77)
(0.78375, 0.10999999999999999, 0.11624999999999996, 0.77)

According to a previous question of mine there denote the [left, bottom, width, height] bounds in relative coordinates for each axes. I actually measured the relative bounds and found them to be incorrect. One easy way to spot it is the last values of the height for each axes object. How can they both be 0.77 when the color bar clearly has a greater height than the contourplot?

I would like to have full control of the size of the contour plot and axes with respect to the figure.

Upvotes: 0

Views: 90

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339580

The position of the axes is determined at draw time. That means that before actually drawing the figure the axes' position is the would-be position or the boundary of the space the axes is free to take.

Usually this would be the same position the axes will take in the final plot, in case you let it expand freely. However, here it seems there is some constraint about the aspect in the game.

To obtain the axes position as it will appear in the drawn figure one should draw the figure manually; then ask for its position.

fig = plt.gcf()
fig.canvas.draw()
for ax in fig.get_axes():
    print(ax.get_position().bounds)

Upvotes: 1

Related Questions