I would like to create an application that embeds a Matplotlib animation within a PyQT4 GUI. I am trying to figure out the basics of how a FigureCanvasQTAgg object works, and am having trouble changing the axis limits once it is created. In the program below, a very simple figure is generated using the FigureCanvasQTAgg object, and I would like the press of the button to change the limits of the x-axis of the resulting plot. (Note that the following code is a simplified version of the code at the end of this post.)
import sys
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class Window(QtGui.QDialog):
def __init__(self):
super(Window, self).__init__()
self.fig = Figure()
self.canvas = FigureCanvas(self.fig)
self.ax = self.fig.add_subplot(111) # create an axis
self.ax.hold(False) # discards the old graph
self.ax.set_xlim([0, 100])
self.ax.set_ylim([-10, 10])
self.ax.set_xlabel('Random x label')
self.button = QtGui.QPushButton('Adjust axes')
self.button.clicked.connect(self.axis_adjust)
# set the layout
layout = QtGui.QVBoxLayout()
layout.addWidget(self.canvas)
layout.addWidget(self.button)
self.setLayout(layout)
self.canvas.draw()
def axis_adjust(self):
self.ax.set_xlim([0, 200])
self.ax.set_xlabel('New label')
app = QtGui.QApplication(sys.argv)
ex = Window()
sys.exit(app.exec_())
Unfortunately, when I run my program and click the "Adjust axes" button, it has absolutely no effect. One thing I fundamentally do not understand is what the line self.canvas.draw() does. From a Matplotlib tutorial, I got the impression that this makes the figure actually appear within the GUI window...but this is not the case, because when I comment out the line self.canvas.draw(), the figure still appears within the GUI window. It is actually the line self.setLayout(layout) that makes the figure appear in the GUI window, which does not make sense from what I have read. It seems my lack of understanding of this functionality is at the root of my problem...