Matplotlib will show the number again

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

So my questions are:

  • How can I return to the previous chart if I called figure.show ()?

  • Is there a more convenient alternative figure.add_suplot(111)if I have several shapes, and therefore from pylab import *; plot(..); show()seems not the solution I'm looking for.

And I want to look

showfunc(stuff) # or
stuff.showfunc()

where stuffis an object containing all the graphs located in one picture, and showfuncis STATELESS (I mean, every time I call it, I behave as if it was the first time I called it). Is this possible when working with matplotlib?

+4
source share
1 answer

, , 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

! GUI , . , , - .

+3

Source: https://habr.com/ru/post/1534011/


All Articles