Why the initial figure dimensions are wrong in this matplotlib script?
05:02 18 Nov 2025

This Python 3.13 script with Matplotlib 3.10.3 run on 1200x1600 display:

import matplotlib.pyplot as plt, numpy as np

fig, ax = plt.subplots()
plt.tight_layout(pad=0.01)
fig.canvas.manager.full_screen_toggle()
img = np.random.randint(5, size=(4, 3))

while True:
  ax.imshow(img)
  print(fig.get_size_inches(), *ax.get_xlim())
  plt.waitforbuttonpress()

initially outputs wrong dimensions

[6.4 4.8] 0.0 1.0

but after a click anywhere on the figure

enter image description here

it outputs physically correct values:

[12.   15.58] -0.5 2.5

What is a way to get correct dimensions before the first click, i.e. at the first call to print(fig.get_size_inches(), *ax.get_xlim())?

python matplotlib