2

How to display figures that were not created using pyplot/pylab figure(), but constructed directly from matplotlib's Figure class?

import matplotlib as mpl
figure = mpl.figure.Figure()
figure.show() # this won't work
alexei
  • 2,031
  • 1
  • 26
  • 28

2 Answers2

2

Direct Figure / Tkinter co-integration

Sample code ( Class-based demo ) is posted in https://stackoverflow.com/a/25769600/3666197

Principle works via a

class SuperShapeFrame( Frame ):                                         # The user interface:

where both an MVC-Controller UI-interactions and an MVC-Model outputs are re-processed and the resulting output state of a Figure object is sent to MVC-Visual part ( handled by a FigureCanvasTkAgg object, .grid()-mounted into Tkinter )

self.fig        = Figure( ( 6, 6 ), dpi = 100 )                         # matplotlib side
canvas          = FigureCanvasTkAgg( self.fig, master = self )          # canvas
canvas.get_tk_widget().grid( row = 0, column = 0, columnspan = 4 )      # grid()-ed into Tkinter

For the full source, use the link above

Community
  • 1
  • 1
user3666197
  • 1
  • 6
  • 50
  • 92
1

Figures that were not created using pyplot/pylab figure() but constructed directly from matplotlib's Figure class need a figure manager:

import Tkinter as Tk
import matplotlib as mpl
import matplotlib.backends.backend_tkagg as tkagg

figure = mpl.figure.Figure()

window = Tk.Tk()
canvas = tkagg.FigureCanvasTkAgg(figure, master=window)
figManager = tkagg.FigureManagerTkAgg(canvas, 0, window)
figManager.show()

This might also be doable using backend_tkagg.new_figure_manager_given_figure() function, but that function does not yet exist in my version of python-matplotlib (1.1.1~rc1+git20120423-0ubuntu1).

alexei
  • 2,031
  • 1
  • 26
  • 28
  • 1
    Sadly, that function actually still depends on pyplot. I would suggest you upgrade your mpl, we are now on v1.4.0 – tacaswell Sep 20 '14 at 21:40