|
PyQwt Command Line Interpreter Examples
The programming style imposed by PyQt (and other GUI toolkit wrappers) is to
create an application instance with some widgets and to enter into an event
loop that locks you from the Python command line interpreter or an enhanced
interpreter like IPython.
PyQwt includes the iqt module that allows to:
- create and control (Py)Qt widgets from the command line interpreter.
- interact with those widgets through (Py)Qt's GUI.
Note: on Windows, the iqt module requires at least PyQwt-5.0.1 or
PyQwt-4.2.3.
A step by step example follows:
-
Start python (or ipython)
and type:
>>> import numpy as np
to import numpy while giving it a short name.
-
The module PyQt4.Qwt5.iqt makes it
possible to combine the read-eval-print loop of the Python
interpreter with the event loop of Qt. Type:
>>> import PyQt4.Qwt5.iqt
to create an application instance and to simulate entering an event loop
which does not inhibit the Python interpreter to read its input.
-
The module qwt.qplt provides an interpreter
friendly interface to the QwtPlot class. Type:
>>> from PyQt4.Qwt5.qplt import *
to load the classes Plot,
IPlot, Curve,
Axis, Pen and
Symbol.
-
Type:
>>> x = np.arange(-2*np.pi, 2*np.pi, 0.01)
to create an array of x-values, ranging from -2*pi to 2*pi with a step size
of 0.01.
-
Type:
>>> p = Plot(Curve(x, np.cos(x), Pen(Magenta, 2), "cos(x)"),
... Curve(x, np.exp(x), Pen(Red), "exp(x)", Y2),
... Axis(Right, Log),
... "PyQwt using Qwt-%s -- http://qwt.sf.net" % QWT_VERSION_STR)
to show a plot with two curves, a linear x-axis, a linear y-axis, a
logarithmic y-axis and a title.
A Plot widget has hidden features:
-
toggle the visibility of each curve by clicking on its legend item.
-
zooming and unzooming uses an infinite deep stack:
- left-click&drag&release to zoom
- right-click to unzoom to the previous zoom selection
- middle-click to unzoom to the initial state
-
There is also an IPlot widget that adds a few
interactive buttons to Plot to facilitate printing
to a printer or an (E)PS file and to clone the plot into Grace.
Grace is an interactive plotting program producing hardcopy output that
is better suited for LaTeX documents and scientific journals.
Type:
>>> p = IPlot(Curve(x, np.cos(x), Pen(Magenta, 2), "cos(x)"),
... Curve(x, np.exp(x), Pen(Red), "exp(x)", Y2),
... Axis(Right, Log),
... "PyQwt using Qwt-%s -- http://qwt.sf.net" % QWT_VERSION_STR)
to show it.
-
Type:
>>> x = x[0:-1:10]
to create a new array of x-values by selecting every 10th element of the
old array.
-
Type:
>>> p.plot(Curve(x, np.cos(x-np.pi/4), Symbol(Circle, Yellow), "circle"),
... Curve(x, np.cos(x+np.pi/4), Pen(Blue), Symbol(Square, Cyan), "square"))
to add two more curves to the plot.
See the
PyQt4.Qwt5.qplt documentation
for detailed information.
|