问题描述
我正在尝试将MatPlotLib与WXPython一起使用.我从 http://www找到了一个很好的例子. cs.colorado.edu/~kena/classes/5448/s11/presentations/pearse.pdf 因为它显示了如何使用2个面板,并且很好地解释了如何嵌入matplotlib.但我认为它缺少某些部分,因为图形尚未初始化.我该如何解决?
I am trying to use MatPlotLib with WXPython. I find a nice example from http://www.cs.colorado.edu/~kena/classes/5448/s11/presentations/pearse.pdfbecause it show how to use 2 panels and it is well explained how to embed matplotlib. But I think it is missing some part, because the graphic isn't being initialized.How can I fix that?
import wx
import numpy
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
class TestFrame(wx.Frame):
def __init__(self,parent,title):
wx.Frame.__init__(self,parent,title=title,size=(500,500))
self.sp = wx.SplitterWindow(self)
self.p1 = wx.Panel(self.sp, style=wx.SUNKEN_BORDER)
self.p2 = wx.Panel(self.sp, style=wx.SUNKEN_BORDER)
self.sp.SplitVertically(self.p1,self.p2,100)
self.statusbar = self.CreateStatusBar()
self.statusbar.SetStatusText('Oi')
class p1(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent,-1,size=(50,50))
self.figure = matplotlib.figure.Figure()
self.axes = self.figure.add_subplot(111)
t = numpy.arange(0.0,10,1.0)
s = [0,1,0,1,0,2,1,2,1,0]
self.y_max = 1.0
self.axes,plot(t,s)
self.canvas = FigureCanvas(self,-1,self.figure)
app = wx.App(redirect=False)
frame = TestFrame(None, 'Hello World!')
frame.Show()
app.MainLoop()
谢谢.
推荐答案
您使用matplot
创建了类p1
,但没有在TestFrame
中使用它.
You made class p1
with matplot
but you didn't use it in TestFrame
.
我更改了一些名称以使其更清晰
I changed some names to make it more clear
import wx
import numpy
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
class TestFrame(wx.Frame):
def __init__(self,parent,title):
wx.Frame.__init__(self,parent,title=title,size=(500,500))
self.sp = wx.SplitterWindow(self)
self.p1 = wx.Panel(self.sp, style=wx.SUNKEN_BORDER)
self.p2 = MatplotPanel(self.sp)
self.sp.SplitVertically(self.p1,self.p2,100)
self.statusbar = self.CreateStatusBar()
self.statusbar.SetStatusText('Oi')
class MatplotPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent,-1,size=(50,50))
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
t = numpy.arange(0.0,10,1.0)
s = [0,1,0,1,0,2,1,2,1,0]
self.y_max = 1.0
self.axes.plot(t,s)
self.canvas = FigureCanvas(self,-1,self.figure)
app = wx.App(redirect=False)
frame = TestFrame(None, 'Hello World!')
frame.Show()
app.MainLoop()
另请参阅(例如)我对如何结合wxPython,matplotlib和Pyopengl
see also (as example) my answer to how to combine wxPython, matplotlib and Pyopengl
MatplotPanel
和NavigationToolbar
和wx.Button
来更改绘图.
from matplotlib.backends.backend_wxagg import NavigationToolbar2Wx as NavigationToolbar
class MatplotPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent,-1,size=(50,50))
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(self.sizer)
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self, -1, self.figure)
self.toolbar = NavigationToolbar(self.canvas)
self.button = wx.Button(self, -1, "Change plot")
self.button.Bind(wx.EVT_BUTTON, self.changePlot)
self.sizer.Add(self.toolbar, 0, wx.EXPAND)
self.sizer.Add(self.button, 0, wx.EXPAND)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.drawSin()
self.current_draw = 'sin'
# self.drawLines()
def changePlot(self, event):
if self.current_draw == 'sin' :
self.drawLines()
self.current_draw = 'lines'
else:
self.drawSin()
self.current_draw = 'sin'
self.Layout()
def drawLines(self):
x = numpy.arange(0.0,10,1.0)
y = [0,1,0,1,0,2,1,2,1,0]
self.axes.clear()
self.axes.plot(x, y)
def drawSin(self):
x = numpy.arange(0.0,10,0.1)
y = numpy.sin(x)
self.axes.clear()
self.axes.plot(x, y)
这篇关于带有MatPlotLib的WXPython的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!