在WxPython面板中嵌入matplotlib图 [英] Embedding a matplotlib figure inside a WxPython panel
本文介绍了在WxPython面板中嵌入matplotlib图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在WxPython面板中嵌入 matplotlib图形对象?
How do I embed a matplotlib figure object inside a WxPython panel?
我在Google上四处搜寻,并看到了涉及交互式图形和其他额外内容的复杂示例.有人可以举一个简单的例子吗?
I googled around and saw complicated examples involving interactive graphics and other extra stuff. Can anybody help with a minimal example?
推荐答案
这是带有matplotlib画布的面板的最小示例:
This is a minimal example for a Panel with a matplotlib canvas:
from numpy import arange, sin, pi
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure
import wx
class CanvasPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self, -1, self.figure)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.SetSizer(self.sizer)
self.Fit()
def draw(self):
t = arange(0.0, 3.0, 0.01)
s = sin(2 * pi * t)
self.axes.plot(t, s)
if __name__ == "__main__":
app = wx.PySimpleApp()
fr = wx.Frame(None, title='test')
panel = CanvasPanel(fr)
panel.draw()
fr.Show()
app.MainLoop()
这篇关于在WxPython面板中嵌入matplotlib图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文