【问题标题】:How to display a matplotlib figure created directly from Figure class?如何显示直接从 Figure 类创建的 matplotlib 图?
【发布时间】: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


    【解决方案1】:

    Direct Figure / Tkinter 协整

    示例代码(基于类的演示)发布在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
    

    如需完整源代码,请使用上面的链接

    【讨论】:

      【解决方案2】:

      不是使用 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)。

      【讨论】:

      • 可悲的是,该功能实际上仍然依赖于 pyplot。我建议你升级你的 mpl,我们现在是 v1.4.0
      猜你喜欢
      • 2021-01-28
      • 2016-08-29
      • 1970-01-01
      • 2022-01-16
      • 2017-06-20
      • 1970-01-01
      • 2012-01-24
      • 2021-02-15
      • 1970-01-01
      相关资源
      最近更新 更多