gpt4 book ai didi

python - Matplotlib:如何显示已关闭的图形

转载 作者:太空狗 更新时间:2023-10-30 00:31:44 27 4
gpt4 key购买 nike

我有一个函数返回用 pyplot 创建的 Figure。此函数在返回之前关闭图窗。如果我没有关闭它,只需使用 plt.show() 即可轻松显示它,但让我们假设我无法做到这一点。

我可以轻松地将返回的 Figure 保存到一个文件中,但是我找不到显示它的方法(即:有一个显示该图的弹出窗口)。

from matplotlib import pyplot as plt                                                


def new_figure():
fig = plt.figure()
plt.plot([0, 1], [2, 3])
plt.close(fig)
return fig


fig = new_figure()
fig.savefig('output.svg')
fig.show()

我怎样才能显示这个数字?

最佳答案

当在 figure 实例上调用 plt.close 时,实际销毁的是使用的图形界面(FigureManager)在屏幕上显示图形(请参阅 JoeKington 在 Matplotlib: re-open a closed figure? 发表的评论)。所以 figure 实例仍然存在并且没有被销毁。为了再次在屏幕上显示图形,我们必须以某种方式重建一个接口(interface),以替换调用 plt.close(fig) 时被破坏的接口(interface)。

这可以通过使用 plt.figure() 创建一个新图形,“窃取”它的管理器,并使用它来显示我们想要在屏幕上显示的图形来完成。或者,可以手动重建界面以使用 GUI 工具包显示图形。我提供了一个例子 PySide使用 Qt4Agg 后端。此外,这里有一个很好的示例展示了如何使用 Tkinter (TkAgg) 完成此操作:http://matplotlib.org/examples/user_interfaces/embedding_in_tk.html (我也测试了这种方法并且它有效)。

虚拟图形方法:

此解决方案基于 how to close a show() window but keep the figure alive?Obtaining the figure manager via the OO interface in Matplotlib .用于构建在屏幕上显示图形的图形界面的 GUI 工具包取决于 backend这是 matplotlib 使用的。如果使用的后端是 TkAggTkInter 将在 Python 2.7 中给出一些可以忽略的警告(参见 post on python bug tracker)。

import matplotlib.pyplot as plt

def new_figure():

fig = plt.figure()
plt.plot([0, 1], [2, 3])
plt.close(fig)
return fig

def show_figure(fig):

# create a dummy figure and use its
# manager to display "fig"

dummy = plt.figure()
new_manager = dummy.canvas.manager
new_manager.canvas.figure = fig
fig.set_canvas(new_manager.canvas)

if __name__ == '__main__':

fig = new_figure()
show_figure(fig)

plt.show()

Pyside 方法:

这包括使用新 Canvas 和工具栏重建 GUI,以在屏幕上显示 fig 实例。

重要说明:如果从 Spyder 运行,下面的代码必须在新的专用 Python 控制台(按 F6)中执行,因为 Spyder 也是一个启动它自己的 QApplication 的 Qt 应用程序(参见 PySide Qt script doesn't launch from Spyder but works from shell )。

import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT
import matplotlib.pyplot as plt

from PySide import QtGui
import sys

def new_figure():

fig = plt.figure()
plt.plot([0, 1], [2, 3])
plt.close(fig)
return fig

class myFigCanvas(QtGui.QWidget):

def __init__(self, fig, parent=None):
super(myFigCanvas, self).__init__(parent)

#---- create new canvas and toolbar --

canvas = FigureCanvasQTAgg(fig)
toolbar = NavigationToolbar2QT(canvas, self)

#---- setup layout of GUI ----

grid = QtGui.QGridLayout()
grid.addWidget(canvas, 0, 0)
grid.addWidget(toolbar, 1, 0)

self.setLayout(grid)

if __name__ == '__main__':

app = QtGui.QApplication(sys.argv)

fig = new_figure()
new_canvas = myFigCanvas(fig)
new_canvas.show()

sys.exit(app.exec_())

结果是:

enter image description here

关于python - Matplotlib:如何显示已关闭的图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31729948/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com