Race condition when getting the height and width of a matplotlib plot

Viewed 46

I'm not sure if I'm missing something, or if this race condition is a bug in matplotlib. I am printing the height and width of an axes object several times, and there is a weird discrepancy between Test 3 and Test 4 below, which are both after the plt.show() call, but Test 3 is immediately after the plt.show(), and Test 4 is after a plt.pause(1). These (I assume?) should give the same result, but don't.

Windows 10 / Python 3.7.6 / Matplotlib 3.1.3

import matplotlib.pyplot as plt

f1,ax1 = plt.subplots()

ax1.set_xlim(-1,1)
ax1.set_ylim(-1,1)

ax1.set_aspect('equal',adjustable='box')

h = ax1.bbox.height
w = ax1.bbox.width

####Test 1
print('%i,%i' % (h,w))


ax1.scatter([0,0.5],[0,0],marker='s',edgecolor='k',alpha=0.5)

h = ax1.bbox.height
w = ax1.bbox.width

####Test 2
print('%i,%i' % (h,w))


f1.show()

h = ax1.bbox.height
w = ax1.bbox.width

####Test 3
print('%i,%i' % (h,w)) #=> 369, 496

plt.pause(1) #Highlighting race condition

h = ax1.bbox.height
w = ax1.bbox.width

####Test 4
print('%i,%i' % (h,w)) #=> 368, 368
0 Answers
Related