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
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())? My goal is to get the number of display pixels per unit square of img, so if there is an alternative to ax.get_position().width*fig.get_size_inches()[0]*fig.dpi/(xMax-xMin), which doesn't suffer from this problem, I would like to know.
An ugly solution is to insert these lines before the loop:
ax.imshow(img)
plt.pause(0.1)
Is there a better option?

fig.canvas.draw(), which is supposed to force layout computation without showing the figure? See also matplotlib's brief documentation.ax.imshow(img)and it didn't work.