Last active
May 20, 2019 21:39
-
-
Save cwebber314/5024548 to your computer and use it in GitHub Desktop.
Embed a matplotlib widget in wxpython. Control the figure using wxpython GUI elements.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
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.slider = wx.Slider(self, -1, value=1, minValue=0, maxValue=11) | |
self.slider.Bind(wx.EVT_SLIDER, self.OnSlider) | |
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, proportion=1, | |
flag=wx.LEFT | wx.TOP | wx.GROW) | |
self.sizer.Add(self.slider, proportion=0, | |
flag=wx.LEFT | wx.TOP | wx.GROW) | |
self.SetSizer(self.sizer) | |
self.Fit() | |
def OnSlider(self, event): | |
self.draw() | |
def draw(self): | |
t = arange(0.0, 3.0, 0.01) | |
h = self.slider.GetValue() | |
s = h * sin(2*pi*t) | |
self.axes.clear() | |
self.axes.plot(t, s) | |
self.canvas.draw() | |
if __name__ == "__main__": | |
app = wx.PySimpleApp() | |
frame = wx.Frame(None, title='test') | |
panel = CanvasPanel(frame) | |
panel.draw() | |
frame.Show() | |
app.MainLoop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment