您在交互式会话中获得的“标准”窗口是通过figure_manager
与pyplot
.
幸运的是,所有的缩放/平移功能都包含在NavigationToolbar
类家族中。
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
def create_main_frame(self):
# host widget
self.main_frame = QtGui.QWidget()
# set up the mpl side of things
self.fig = Figure((24, 24), tight_layout=True)
self.canvas = FigureCanvas(self.fig)
# attach canvas to host widget
self.canvas.setParent(self.main_frame)
# make axes
self.axes = self.fig.add_subplot(111, adjustable='datalim', aspect='equal')
# make the tool bar
self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame)
# set up layout
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.mpl_toolbar)
vbox.addWidget(self.canvas)
# set the layout to the host widget
self.main_frame.setLayout(vbox)
# make the host widget the central widget in the main frame of the class
# this code is ripped from
self.setCentralWidget(self.main_frame)