如何通过matplotlib API使用show()创建地物?

如何通过matplotlib API使用show()创建地物?,matplotlib,backend,Matplotlib,Backend,我需要创建一个figure对象,该对象可以在屏幕上显示(当使用一些交互式后端时)或显示,但我需要避免使用pylab/pyplot API,因为它设置了默认后端并会搞乱其他事情。我将图形创建为 import matplotlib.figure import matplotlib.backends.backend_qt4agg # or agg for headless backends figure=matplotlig.figure.Figure() canvas=matplotlib.back

我需要创建一个figure对象,该对象可以在屏幕上显示(当使用一些交互式后端时)或显示,但我需要避免使用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)
但我还是错过了一些东西。警察说


那么我该怎么做呢?

交互式会话中的“标准”窗口是通过
图形管理器
类家族运行的,这些类与
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)

交互式会话中的“标准”窗口是通过与
pyplot
紧密相连的
figure\u manager
类系列运行的

幸运的是,所有的缩放/平移功能都包含在
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)

您需要将画布粘贴到小部件窗口中。看到是的,但是我想使用一些matplotlib的内部函数来完成所有繁重的工作(带有缩放按钮的标准窗口等),只需绕过pylab API。您需要将画布粘贴到小部件窗口中。看到是的,但我想使用一些matplotlib的内部函数来完成所有繁重的工作(带有缩放按钮的标准窗口等),只是绕过pylab API。