Skip to content

Commit b1e4eeb

Browse files
author
Pierre Raybaut
committed
Fixed some examples (BodeDemo, CartesianDemo, CurveDemo1, DataDemo, ...)
1 parent 523be04 commit b1e4eeb

File tree

6 files changed

+52
-341
lines changed

6 files changed

+52
-341
lines changed

examples/BodeDemo.py

Lines changed: 3 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
import sys
1212
from PyQt4.Qt import *
13-
#from PyQt4.Qwt5 import *
1413
from qwt import *
15-
import PyQt4.Qwt5.anynumpy as np
14+
import numpy as np
1615

1716

1817
print_xpm = ['32 32 12 1',
@@ -194,30 +193,22 @@ def __init__(self, *args):
194193

195194
self.setDamp(0.01)
196195

197-
# __init__()
198-
199196
def showData(self, frequency, amplitude, phase):
200197
self.curve1.setData(frequency, amplitude)
201198
self.curve2.setData(frequency, phase)
202199

203-
# showData()
204-
205200
def showPeak(self, frequency, amplitude):
206201
self.peakMarker.setValue(frequency, amplitude)
207202
label = self.peakMarker.label()
208203
label.setText('Peak: %4g dB' % amplitude)
209204
self.peakMarker.setLabel(label)
210205

211-
# showPeak()
212-
213206
def show3dB(self, frequency):
214207
self.dB3Marker.setValue(frequency, 0.0)
215208
label = self.dB3Marker.label()
216209
label.setText('-3dB at f = %4g' % frequency)
217210
self.dB3Marker.setLabel(label)
218211

219-
# show3dB()
220-
221212
def setDamp(self, d):
222213
self.damping = d
223214
# Numerical Python: f, g, a and p are NumPy arrays!
@@ -237,10 +228,6 @@ def setDamp(self, d):
237228

238229
self.replot()
239230

240-
# setDamp()
241-
242-
# class BodePlot
243-
244231

245232
class BodeDemo(QMainWindow):
246233

@@ -252,35 +239,6 @@ def __init__(self, *args):
252239

253240
self.setContextMenuPolicy(Qt.NoContextMenu)
254241

255-
# self.zoomers = []
256-
# zoomer = QwtPlotZoomer(
257-
# QwtPlot.xBottom,
258-
# QwtPlot.yLeft,
259-
# QwtPicker.DragSelection,
260-
# QwtPicker.AlwaysOff,
261-
# self.plot.canvas())
262-
# zoomer.setRubberBandPen(QPen(Qt.green))
263-
# self.zoomers.append(zoomer)
264-
#
265-
# zoomer = QwtPlotZoomer(
266-
# QwtPlot.xTop,
267-
# QwtPlot.yRight,
268-
# QwtPicker.PointSelection | QwtPicker.DragSelection,
269-
# QwtPicker.AlwaysOff,
270-
# self.plot.canvas())
271-
# zoomer.setRubberBand(QwtPicker.NoRubberBand)
272-
# self.zoomers.append(zoomer)
273-
274-
# self.picker = QwtPlotPicker(
275-
# QwtPlot.xBottom,
276-
# QwtPlot.yLeft,
277-
# QwtPicker.PointSelection | QwtPicker.DragSelection,
278-
# QwtPlotPicker.CrossRubberBand,
279-
# QwtPicker.AlwaysOn,
280-
# self.plot.canvas())
281-
# self.picker.setRubberBandPen(QPen(Qt.green))
282-
# self.picker.setTrackerPen(QPen(Qt.cyan))
283-
284242
self.setCentralWidget(self.plot)
285243

286244
toolBar = QToolBar(self)
@@ -316,32 +274,11 @@ def __init__(self, *args):
316274
dampLayout.addWidget(QLabel("Damping Factor", dampBox), 0)
317275
dampLayout.addSpacing(10)
318276

319-
# self.cntDamp = QwtCounter(dampBox)
320-
# self.cntDamp.setRange(0.01, 5.0, 0.01)
321-
# self.cntDamp.setValue(0.01)
322-
# dampLayout.addWidget(self.cntDamp, 10)
323-
324277
toolBar.addWidget(dampBox)
325278

326279
self.statusBar()
327280

328-
self.zoom(False)
329281
self.showInfo()
330-
331-
# self.connect(self.cntDamp,
332-
# SIGNAL('valueChanged(double)'),
333-
# self.plot.setDamp)
334-
self.connect(btnZoom,
335-
SIGNAL('toggled(bool)'),
336-
self.zoom)
337-
# self.connect(self.picker,
338-
# SIGNAL('moved(const QPoint &)'),
339-
# self.moved)
340-
# self.connect(self.picker,
341-
# SIGNAL('selected(const QPolygon &)'),
342-
# self.selected)
343-
344-
# __init__()
345282

346283
def print_(self):
347284
printer = QPrinter(QPrinter.HighResolution)
@@ -366,48 +303,22 @@ def print_(self):
366303
renderer.renderTo(self.plot, printer)
367304

368305
def exportDocument(self):
369-
self.plot.exportTo("bode.pdf")
370-
371-
def zoom(self, on):
372-
# self.zoomers[0].setEnabled(on)
373-
# self.zoomers[0].zoom(0)
374-
#
375-
# self.zoomers[1].setEnabled(on)
376-
# self.zoomers[1].zoom(0)
377-
378-
# if on:
379-
# self.picker.setRubberBand(Qwt.QwtPicker.NoRubberBand)
380-
# else:
381-
# self.picker.setRubberBand(Qwt.QwtPicker.CrossRubberBand)
382-
383-
self.showInfo()
384-
385-
# zoom()
306+
renderer = QwtPlotRenderer(self.plot)
307+
renderer.exportTo(self.plot, "bode")
386308

387309
def showInfo(self, text=""):
388-
# if not text:
389-
# if self.picker.rubberBand():
390-
# text = 'Cursor Pos: Press left mouse button in plot region'
391-
# else:
392-
# text = 'Zoom: Press mouse button and drag'
393-
394310
self.statusBar().showMessage(text)
395311

396-
# showInfo()
397-
398312
def moved(self, point):
399313
info = "Freq=%g, Ampl=%g, Phase=%g" % (
400314
self.plot.invTransform(Qwt.QwtPlot.xBottom, point.x()),
401315
self.plot.invTransform(Qwt.QwtPlot.yLeft, point.y()),
402316
self.plot.invTransform(Qwt.QwtPlot.yRight, point.y()))
403317
self.showInfo(info)
404318

405-
# moved()
406-
407319
def selected(self, _):
408320
self.showInfo()
409321

410-
# selected()
411322

412323
def make():
413324
demo = BodeDemo()

examples/CartesianDemo.py

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,33 @@
22

33
import sys
44
from PyQt4 import Qt
5-
#import PyQt4.Qwt5 as Qwt
65
import qwt as Qwt
7-
from PyQt4.Qwt5.anynumpy import *
6+
import numpy as np
87

98

109
class CartesianAxis(Qwt.QwtPlotItem):
1110
"""Supports a coordinate system similar to
12-
http://en.wikipedia.org/wiki/Image:Cartesian-coordinate-system.svg
13-
"""
14-
11+
http://en.wikipedia.org/wiki/Image:Cartesian-coordinate-system.svg"""
1512
def __init__(self, masterAxis, slaveAxis):
1613
"""Valid input values for masterAxis and slaveAxis are QwtPlot.yLeft,
1714
QwtPlot.yRight, QwtPlot.xBottom, and QwtPlot.xTop. When masterAxis is
18-
an x-axis, slaveAxis must be an y-axis; and vice versa.
19-
"""
15+
an x-axis, slaveAxis must be an y-axis; and vice versa."""
2016
Qwt.QwtPlotItem.__init__(self)
2117
self.__axis = masterAxis
22-
try:
23-
# Qwt5
24-
set_axes = self.setAxis
25-
except AttributeError:
26-
# Qwt6
27-
set_axes = self.setAxes
2818
if masterAxis in (Qwt.QwtPlot.yLeft, Qwt.QwtPlot.yRight):
29-
set_axes(slaveAxis, masterAxis)
19+
self.setAxes(slaveAxis, masterAxis)
3020
else:
31-
set_axes(masterAxis, slaveAxis)
21+
self.setAxes(masterAxis, slaveAxis)
3222
self.scaleDraw = Qwt.QwtScaleDraw()
3323
self.scaleDraw.setAlignment((Qwt.QwtScaleDraw.LeftScale,
3424
Qwt.QwtScaleDraw.RightScale,
3525
Qwt.QwtScaleDraw.BottomScale,
3626
Qwt.QwtScaleDraw.TopScale)[masterAxis])
3727

38-
# __init__()
39-
4028
def draw(self, painter, xMap, yMap, rect):
41-
"""Draw an axis on the plot canvas
42-
"""
43-
try:
44-
# Qwt5
45-
xtr = xMap.xTransform
46-
ytr = yMap.xTransform
47-
except AttributeError:
48-
# Qwt6
49-
xtr = xMap.transform
50-
ytr = yMap.transform
29+
"""Draw an axis on the plot canvas"""
30+
xtr = xMap.transform
31+
ytr = yMap.transform
5132
if self.__axis in (Qwt.QwtPlot.yLeft, Qwt.QwtPlot.yRight):
5233
self.scaleDraw.move(round(xtr(0.0)), yMap.p2())
5334
self.scaleDraw.setLength(yMap.p1()-yMap.p2())
@@ -57,23 +38,16 @@ def draw(self, painter, xMap, yMap, rect):
5738
self.scaleDraw.setScaleDiv(self.plot().axisScaleDiv(self.__axis))
5839
self.scaleDraw.draw(painter, self.plot().palette())
5940

60-
# draw()
61-
62-
# class CartesianAxis
63-
6441

6542
class CartesianPlot(Qwt.QwtPlot):
6643
"""Creates a coordinate system similar system
67-
http://en.wikipedia.org/wiki/Image:Cartesian-coordinate-system.svg
68-
"""
69-
44+
http://en.wikipedia.org/wiki/Image:Cartesian-coordinate-system.svg"""
7045
def __init__(self, *args):
7146
Qwt.QwtPlot.__init__(self, *args)
7247
self.setTitle('Cartesian Coordinate System Demo')
7348
# create a plot with a white canvas
7449
self.setCanvasBackground(Qt.Qt.white)
7550
# set plot layout
76-
# self.plotLayout().setMargin(0)
7751
self.plotLayout().setCanvasMargin(0)
7852
self.plotLayout().setAlignCanvasToScales(True)
7953
# attach a grid
@@ -89,9 +63,9 @@ def __init__(self, *args):
8963
yaxis.attach(self)
9064
self.enableAxis(Qwt.QwtPlot.yLeft, False)
9165
# calculate 3 NumPy arrays
92-
x = arange(-2*pi, 2*pi, 0.01)
93-
y = pi*sin(x)
94-
z = 4*pi*cos(x)*cos(x)*sin(x)
66+
x = np.arange(-2*np.pi, 2*np.pi, 0.01)
67+
y = np.pi*np.sin(x)
68+
z = 4*np.pi*np.cos(x)*np.cos(x)*np.sin(x)
9569
# attach a curve
9670
curve = Qwt.QwtPlotCurve('y = pi*sin(x)')
9771
curve.attach(self)
@@ -104,32 +78,15 @@ def __init__(self, *args):
10478
curve.setData(x, z)
10579
self.replot()
10680

107-
# __init__()
108-
109-
# class CartesianPlot
110-
11181

11282
def make():
11383
demo = CartesianPlot()
11484
demo.resize(400, 300)
11585
demo.show()
11686
return demo
11787

118-
# make()
11988

120-
121-
def main(args):
122-
app = Qt.QApplication(args)
89+
if __name__ == '__main__':
90+
app = Qt.QApplication(sys.argv)
12391
demo = make()
12492
sys.exit(app.exec_())
125-
126-
# main()
127-
128-
129-
# Admire
130-
if __name__ == '__main__':
131-
main(sys.argv)
132-
133-
# Local Variables: ***
134-
# mode: python ***
135-
# End: ***

0 commit comments

Comments
 (0)