I am trying to determine the size of the current matplotlib figure window, such that I can reposition it correctly on the screen. This must be done before entering the event loop ( i.e. before calling plt.show()
). Here is an example:
import matplotlib
import matplotlib.pyplot as plt
def print_info(window):
print("screen width: {}".format(window.winfo_screenwidth()))
print("window width: {}".format(window.winfo_width()))
return
matplotlib.use('TkAgg')
fig, axes = plt.subplots()
axes.plot([1, 2, 3], [1, 4, 9], 'ro', label='Test')
axes.set_title('Test curve')
# plt.draw() # <-- this has no effect
# fig.canvas.draw_idle() # <-- this has no effect
window = plt.get_current_fig_manager().window
# window.update() # <-- this has no effect
fig.canvas.mpl_connect('key_press_event', lambda event: print_info(window))
#plt.pause(0.000001) # only entering the tk/pyplot event loop forces update
print_info(window)
plt.show()
The output is:
screen width: 1920
window width: 1
If I uncomment the plt.pause(...)
call, it works fine (but I get a warning):
/home/hakon/.pyenv/versions/3.6.1/lib/python3.6/site-packages/matplotlib/backend_bases.py:2453: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented
warnings.warn(str, mplDeprecation)
screen width: 1920
window width: 640
Questions:
- How can I avoid calling
plt.pause()
to get the correct window width? - If the only option I have is to call
plt.pause()
, what is then the reason for the warning?