Diego Herranz
Diego Herranz

Reputation: 2937

Can't resize matplotlib window

I have a plot using matplotlib which updates every second. It's intended only for slow monitoring so I followed a simple approach of clearing and drawing again and again which is not optimum but I wanted simplicity.

my_fig = plt.figure()
ax1 = plt.subplot(111)
plt.show(block=False)

while True:

    data = read_data_and_process(...)

    ax1.plot_date(data[0], data[1], '-')        
    my_fig.autofmt_xdate()

    plt.draw()
    time.sleep(1)
    ax1.cla()

It works but if I resize the window, the plot doesn't change its size. If I plot the data without updating, I can resize the window and the plot resizes accordingly:

my_fig = plt.figure()
ax1 = plt.subplot(111)       

data = read_data_and_process(...)        
ax1.plot_date(data[0], data[1], '-')        
my_fig.autofmt_xdate()        
plt.show(block=True)

How can I do to be able to resize the window on the first example while updating the data?

Thanks!

Upvotes: 1

Views: 1778

Answers (1)

djhoese
djhoese

Reputation: 3667

When I try this code I am unable to even grab the window to resize it because the matplotlib backend's event loop is stuck. I would suggest looking at the Animation API for matplotlib (see examples).

However, here is a quick hack to get your example to work by forcing the Qt backend.

import matplotlib
matplotlib.use('QT4Agg')
from matplotlib import pyplot as plt
from PyQt4 import QtCore,QtGui
import time

# The rest of your code as normal

    # then at the bottom of the while loop
    plt.draw()
    QtGui.qApp.processEvents()
    time.sleep(1)
    ax1.cla()

Bottom line is this shouldn't be used in "production" code, but as a quick hack sure it works.

Upvotes: 1

Related Questions