【发布时间】:2014-11-15 04:42:03
【问题描述】:
如何显示不是使用 pyplot/pylab figure() 创建的,而是直接从 matplotlib 的 Figure 类构造的图形?
import matplotlib as mpl
figure = mpl.figure.Figure()
figure.show() # this won't work
【问题讨论】:
标签: python matplotlib plot
如何显示不是使用 pyplot/pylab figure() 创建的,而是直接从 matplotlib 的 Figure 类构造的图形?
import matplotlib as mpl
figure = mpl.figure.Figure()
figure.show() # this won't work
【问题讨论】:
标签: python matplotlib plot
示例代码(基于类的演示)发布在https://stackoverflow.com/a/25769600/3666197
原理通过 a
class SuperShapeFrame( Frame ): # The user interface:
MVC-Controller UI-interactions 和 MVC-Model 输出都被重新处理,Figure 对象的结果输出状态被发送到 MVC-Visual 部分(由 FigureCanvasTkAgg 对象,.grid()-安装到 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
如需完整源代码,请使用上面的链接
【讨论】:
不是使用 pyplot/pylab figure() 创建但直接从 matplotlib 的 Figure 类构建的图形需要图形管理器:
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()
这也可能使用backend_tkagg.new_figure_manager_given_figure() 函数来实现,但该函数在我的python-matplotlib 版本中尚不存在(1.1.1~rc1+git20120423-0ubuntu1)。
【讨论】: