[英]matplotlib show figure again
When using matplotlib: 使用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
So my questions are: 所以我的问题是:
How can I get the previous plot back if I have called figure.show()? 如果我调用了figure.show(),我怎么能得到之前的情节呢?
Is there a more convenient alternative to figure.add_suplot(111)
if I have multiple figures and thus from pylab import *; plot(..); show()
如果我有多个数字并因此
from pylab import *; plot(..); show()
是否有一个更方便的替代图figure.add_suplot(111)
from pylab import *; plot(..); show()
from pylab import *; plot(..); show()
from pylab import *; plot(..); show()
seems not a solution I'm looking for. from pylab import *; plot(..); show()
似乎不是我正在寻找的解决方案。
And what I eagerly want is 而我急切想要的是
showfunc(stuff) # or
stuff.showfunc()
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). stuff
是包含排列在一张图片中的所有图的对象, showfunc
是STATELESS(我的意思是,每次我调用它时,我的行为就好像它是第一次调用的那样)。 Is this possible when working with matplotlib
? 使用
matplotlib
时这可能吗?
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. 我找不到一个令人满意的答案,所以我通过编写一个自定义的
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()
Make this file as start file of IPython
. 将此文件作为
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
Works! 作品! I never touched GUI programming, and don't know if this would have any side-effect.
我从未接触到GUI编程,也不知道这是否会产生任何副作用。 Comment freely if you think something should be done that way.
如果你认为应该以这种方式做某事,请自由评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.