Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98a11ca

Browse files
authored
Merge pull request matplotlib#10416 from DietmarSchwertberger/wx-update-examples
DOC: Update some wx examples
2 parents 2d9d684 + d8dd125 commit 98a11ca

File tree

5 files changed

+24
-45
lines changed

5 files changed

+24
-45
lines changed

examples/user_interfaces/embedding_in_wx2_sgskip.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
toolbar - comment out the add_toolbar line for no toolbar
88
"""
99

10-
import matplotlib
11-
matplotlib.use('WXAgg')
1210
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
1311
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
1412
from matplotlib.figure import Figure

examples/user_interfaces/embedding_in_wx3_sgskip.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import os
2929
import gc
3030
import matplotlib
31-
matplotlib.use('WXAgg')
3231
import matplotlib.cm as cm
3332
import matplotlib.cbook as cbook
3433
from matplotlib.backends.backend_wxagg import Toolbar, FigureCanvasWxAgg

examples/user_interfaces/embedding_in_wx4_sgskip.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
An example of how to use wx or wxagg in an application with a custom toolbar.
77
"""
88

9-
import matplotlib
10-
matplotlib.use('WXAgg')
119
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
1210
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg
1311
from matplotlib.backends.backend_wx import _load_bitmap
@@ -74,8 +72,6 @@ def __init__(self):
7472

7573
self.sizer = wx.BoxSizer(wx.VERTICAL)
7674
self.sizer.Add(self.canvas, 1, wx.TOP | wx.LEFT | wx.EXPAND)
77-
# Capture the paint message
78-
self.Bind(wx.EVT_PAINT, self.OnPaint)
7975

8076
self.toolbar = MyNavigationToolbar(self.canvas, True)
8177
self.toolbar.Realize()
@@ -88,10 +84,6 @@ def __init__(self):
8884
self.SetSizer(self.sizer)
8985
self.Fit()
9086

91-
def OnPaint(self, event):
92-
self.canvas.draw()
93-
event.Skip()
94-
9587

9688
class App(wx.App):
9789
def OnInit(self):

examples/user_interfaces/fourier_demo_wx_sgskip.py

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
import numpy as np
99

1010
import wx
11-
import matplotlib
12-
matplotlib.interactive(False)
13-
matplotlib.use('WXAgg')
1411
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
1512
from matplotlib.figure import Figure
1613

@@ -108,55 +105,48 @@ def setKnob(self, value):
108105
class FourierDemoFrame(wx.Frame):
109106
def __init__(self, *args, **kwargs):
110107
wx.Frame.__init__(self, *args, **kwargs)
108+
panel = wx.Panel(self)
111109

112-
self.fourierDemoWindow = FourierDemoWindow(self)
113-
self.frequencySliderGroup = SliderGroup(
114-
self,
115-
label='Frequency f0:',
116-
param=self.fourierDemoWindow.f0)
117-
self.amplitudeSliderGroup = SliderGroup(self, label=' Amplitude a:',
118-
param=self.fourierDemoWindow.A)
110+
# create the GUI elements
111+
self.createCanvas(panel)
112+
self.createSliders(panel)
119113

114+
# place them in a sizer for the Layout
120115
sizer = wx.BoxSizer(wx.VERTICAL)
121-
sizer.Add(self.fourierDemoWindow, 1, wx.EXPAND)
116+
sizer.Add(self.canvas, 1, wx.EXPAND)
122117
sizer.Add(self.frequencySliderGroup.sizer, 0,
123118
wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, border=5)
124119
sizer.Add(self.amplitudeSliderGroup.sizer, 0,
125120
wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, border=5)
126-
self.SetSizer(sizer)
127-
121+
panel.SetSizer(sizer)
128122

129-
class FourierDemoWindow(wx.Window, Knob):
130-
def __init__(self, *args, **kwargs):
131-
wx.Window.__init__(self, *args, **kwargs)
123+
def createCanvas(self, parent):
132124
self.lines = []
133125
self.figure = Figure()
134-
self.canvas = FigureCanvasWxAgg(self, -1, self.figure)
126+
self.canvas = FigureCanvasWxAgg(parent, -1, self.figure)
135127
self.canvas.callbacks.connect('button_press_event', self.mouseDown)
136128
self.canvas.callbacks.connect('motion_notify_event', self.mouseMotion)
137129
self.canvas.callbacks.connect('button_release_event', self.mouseUp)
138130
self.state = ''
139131
self.mouseInfo = (None, None, None, None)
140132
self.f0 = Param(2., minimum=0., maximum=6.)
141133
self.A = Param(1., minimum=0.01, maximum=2.)
142-
self.draw()
134+
self.createPlots()
143135

144136
# Not sure I like having two params attached to the same Knob,
145137
# but that is what we have here... it works but feels kludgy -
146138
# although maybe it's not too bad since the knob changes both params
147139
# at the same time (both f0 and A are affected during a drag)
148140
self.f0.attach(self)
149141
self.A.attach(self)
150-
self.Bind(wx.EVT_SIZE, self.sizeHandler)
151-
152-
self.Bind(wx.EVT_PAINT, self.OnPaint)
153-
154-
def OnPaint(self, event):
155-
self.canvas.draw()
156-
event.Skip()
157142

158-
def sizeHandler(self, *args, **kwargs):
159-
self.canvas.SetSize(self.GetSize())
143+
def createSliders(self, panel):
144+
self.frequencySliderGroup = SliderGroup(
145+
panel,
146+
label='Frequency f0:',
147+
param=self.f0)
148+
self.amplitudeSliderGroup = SliderGroup(panel, label=' Amplitude a:',
149+
param=self.A)
160150

161151
def mouseDown(self, evt):
162152
if self.lines[0].contains(evt)[0]:
@@ -186,7 +176,10 @@ def mouseMotion(self, evt):
186176
def mouseUp(self, evt):
187177
self.state = ''
188178

189-
def draw(self):
179+
def createPlots(self):
180+
# This method creates the subplots, waveforms and labels.
181+
# Later, when the waveforms or sliders are dragged, only the
182+
# waveform data will be updated (not here, but below in setKnob).
190183
if not hasattr(self, 'subplot1'):
191184
self.subplot1, self.subplot2 = self.figure.subplots(2)
192185
x1, y1, x2, y2 = self.compute(self.f0.value, self.A.value)
@@ -222,15 +215,14 @@ def compute(self, f0, A):
222215
(np.exp(-np.pi * (f - f0) ** 2) + np.exp(-np.pi * (f + f0) ** 2))
223216
return f, X, t, x
224217

225-
def repaint(self):
226-
self.canvas.draw()
227-
228218
def setKnob(self, value):
229219
# Note, we ignore value arg here and just go by state of the params
230220
x1, y1, x2, y2 = self.compute(self.f0.value, self.A.value)
221+
# update the data of the two waveforms
231222
self.lines[0].set(xdata=x1, ydata=y1)
232223
self.lines[1].set(xdata=x2, ydata=y2)
233-
self.repaint()
224+
# make the canvas draw its contents again with the new data
225+
self.canvas.draw()
234226

235227

236228
class App(wx.App):

examples/user_interfaces/wxcursor_demo_sgskip.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
Example to draw a cursor and report the data coords in wx.
77
"""
88

9-
import matplotlib
10-
matplotlib.use('WXAgg')
119

1210
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
1311
from matplotlib.backends.backend_wx import NavigationToolbar2Wx, wxc

0 commit comments

Comments
 (0)