再次显示matplotlib图形

8 投票
1 回答
3854 浏览
提问于 2025-04-18 00:24

在使用matplotlib的时候:

from matplotlib import pyplot as plt

figure = plt.figure()

ax = figure.add_subplot(111)
ax.plot(x,y)

figure.show()  # figure is shown in GUI

# How can I view the figure again after I closed the GUI window?

figure.show()  # Exception in Tkinter callback... TclError: this isn't a Tk application
figure.show()  # nothing happened

所以我有几个问题:

  1. 如果我已经调用了figure.show(),我该如何找回之前的图表?

  2. 如果我有多个图表,是否有比figure.add_subplot(111)更方便的替代方法?因为使用from pylab import *; plot(..); show()似乎不是我想要的解决方案。

我非常想要的是

showfunc(stuff) # or
stuff.showfunc()

其中stuff是一个包含所有图表并排在一张图里的对象,而showfunc是无状态的(我的意思是,每次调用它时,它的表现就像第一次被调用一样)。在使用matplotlib时,这可能实现吗?

1 个回答

4

我找不到一个满意的答案,所以我通过写一个自定义的 Figure 类来解决这个问题,这个类是从 matplotlib.figure.Figure 继承来的,并且提供了一个新的 show() 方法,每次调用时都会创建一个 gtk.Window 对象。

import gtk
import sys
import os
import threading

from matplotlib.figure import Figure as MPLFigure
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
from matplotlib.backends.backend_gtkagg import NavigationToolbar2GTKAgg as NaviToolbar


class ThreadFigure(threading.Thread):
    def __init__(self, figure, count):
        threading.Thread.__init__(self)
        self.figure =   figure
        self.count  =   count
    def run(self):
        window  =   gtk.Window()
        # window.connect('destroy', gtk.main_quit)

        window.set_default_size(640, 480)
        window.set_icon_from_file(...)  # provide an icon if you care about the looks

        window.set_title('MPL Figure #{}'.format(self.count))
        window.set_wmclass('MPL Figure', 'MPL Figure')

        vbox    =   gtk.VBox()
        window.add(vbox)

        canvas  =   FigureCanvas(self.figure)
        vbox.pack_start(canvas)

        toolbar =   NaviToolbar(canvas, window)
        vbox.pack_start(toolbar, expand = False, fill = False)

        window.show_all()
        # gtk.main() ... should not be called, otherwise BLOCKING


class Figure(MPLFigure):
    display_count = 0
    def show(self):
        Figure.display_count += 1 
        thrfig = ThreadFigure(self, Figure.display_count)
        thrfig.start()

把这个文件设置为 IPython 的启动文件。然后

figure = Figure()
ax = figure.add_subplot(211)
... (same story as using standard `matplotlib.pyplot` )
figure.show()

# window closed accidentally or intentionally...

figure.show()
# as if `.show()` is never called

这样就可以了!我从来没有接触过图形用户界面编程,也不知道这样做会不会有什么副作用。如果你觉得有什么地方应该改进,请随意评论。

撰写回答