1

我需要创建一个图形对象,它可以show()在屏幕上编辑(当使用一些交互式后端时)或savefig()编辑,但我需要避免使用 pylab/pyplot API,因为它设置了默认后端并搞砸了其他事情。我将图形创建为

import matplotlib.figure
import matplotlib.backends.backend_qt4agg # or agg for headless backends
figure=matplotlig.figure.Figure()
canvas=matplotlib.backends.backend_qt4agg.FigureCanvasQTAgg(figure)

但我仍然缺少一些东西。Figure.show的文档说

If the figure was not created using figure(), it will lack a FigureManagerBase, and will raise an AttributeError.

我该怎么做呢?

4

1 回答 1

1

您在交互式会话中获得的“标准”窗口是通过figure_managerpyplot.

幸运的是,所有的缩放/平移功能都包含在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)
于 2013-10-17T15:56:40.960 回答