matplotlib再次显示图 [英] matplotlib show figure again

查看:45
本文介绍了matplotlib再次显示图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 matplotlib 时:

When using 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(),如何获得上一个图?

  1. How can I get the previous plot back if I have called figure.show()?

如果我有多个图形,因此从pylab导入*,有没有比 figure.add_suplot(111)更方便的替代方法*;阴谋(..);show()似乎不是我正在寻找的解决方案.

Is there a more convenient alternative to figure.add_suplot(111) if I have multiple figures and thus from pylab import *; plot(..); show() seems not a solution I'm looking for.

我最想要的是

showfunc(stuff) # or
stuff.showfunc()

其中 stuff 是一个对象,其中包含所有布置在一张图片中的情节,而 showfunc 是STATELESS(我是说,每次调用它时,我的行为就像第一次打电话).使用 matplotlib 时可以这样做吗?

where stuff is an object containing all the plots arranged in one picture, and showfunc is STATELESS(I mean, each time I call it, I behaves as if it's first time called). Is this possible when working with matplotlib?

推荐答案

我找不到满意的答案,所以我通过编写一个扩展 matplotlib 的自定义 Figure 类来处理这个问题.figure.Figure 并提供了一个新的 show() 方法,该方法每次调用都会创建一个 gtk.Window 对象.

I can't find a satisfactory answer, so I handle this problem by writing a custom Figure class extending matplotlib.figure.Figure and providing a new show() method, which create a gtk.Window object each time called.

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 的起始文件.还有

Make this file as start file of IPython. And

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

有效!我从来没有接触过 GUI 编程,不知道这会不会有任何副作用.如果您认为应该以这种方式做某事,请自由发表评论.

Works! I never touched GUI programming, and don't know if this would have any side-effect. Comment freely if you think something should be done that way.

这篇关于matplotlib再次显示图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆