PlotViewer
X/Y data and functions can be plotted using the PlotViewer window.
For the tutorial we assume the following X/Y dataset:
| X | Y |
| 1.0 | 0.0 |
| 1.5 | 0.23 |
| 2.0 | 0.45 |
| 3.2 | 0.79 |
| 4.0 | 0.61 |
| 5.1 | 0.52 |
| 7.0 | 0.33 |
| 8.0 | 0.41 |
First a PlotViewer has to be opened:
viewer=gui.CreatePlotViewer()
Then you need to create and fill a PlotData object:
data=gui.PlotData() data.AddXY(1.0,0.0) data.AddXY(1.5,0.23) data.AddXY(2.0,0.45) data.AddXY(3.2,0.79) data.AddXY(4.0,0.61) data.AddXY(5.1,0.52) data.AddXY(7.0,0.33) data.AddXY(8.0,0.41)
Now the data can be added to the viewer. For this example we use circles with a radius of 5 to display the data points:
viewer.AddData(data).SetSymbolSize(5).SetMode(viewer.POINTS)
The PlotViewer can also plot functions. To plot a funcion a class has to be derived from PlotFunction and its Func member function has to be overridden. If init is overriden in the derived class, it has to call the base class init. Following example creates a class which plots a sinc function:
from math import sin class sinc(gui.PlotFunction): def __init__(self,width=1): gui.PlotFunction.__init__(self) self.width_=width def Func(self,x): if x==0: return 1.0 else: return sin(self.width_*x)/(self.width_*x)
To plot the sinc you need to create an instance of the sinc class and add it to the PlotViewer:
sincinst=sinc() viewer.AddData(sincinst).SetMode(viewer.LINES)
To display a different range within the plot window the minimum and maximum x and y to be displayed can be adjusted:
viewer.SetMinimumX(-10.0) viewer.SetMaximumX(10.0) viewer.SetMinimumY(-0.5) viewer.SetMaximumY(1.5)
The resulting plot looks like this:
The full script for this tutorial can be found here:
Back to Tutorials
Attachments
- PlotViewer.png (28.2 kB) - added by andreas on 08/09/07 13:03:29.
- tutorial_plotviewer.py (0.7 kB) - added by andreas on 02/11/08 11:35:10.

