1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
Examples
--------
Curve example
^^^^^^^^^^^^^
Create a basic curve plotting widget:
* before creating any widget, a `QApplication` must be instantiated (that
is a `Qt` internal requirement)::
>>> import guidata
>>> app = guidata.qapplication()
* that is mostly equivalent to the following (the only difference is that
the :mod:`plotpy` helper function also installs the `Qt` translation
corresponding to the system locale)::
>>> from qtpy.QtWidgets import QApplication
>>> app = QApplication([])
* now that a `QApplication` object exists, we may create the plotting
widget using :py:class:`.PlotWidget` and :py:class:`.PlotOptions`::
>>> from plotpy.plot import PlotWidget, PlotOptions
>>> options = PlotOptions(title="Example", xlabel="X", ylabel="Y", type="curve")
>>> plot = PlotWidget(options=options)
* ...or using the :py:class:`.PlotBuilder` (see :py:attr:`plotpy.builder.make`)::
>>> from plotpy.builder import make
>>> plot = make.widget(title="Example", xlabel="X", ylabel="Y", type="curve")
Create a curve item:
* from the associated plot item class (e.g. `ErrorBarCurveItem` to create
a curve with error bars): the item properties are then assigned by creating
the appropriate style parameters object (e.g. :py:class:`.styles.ErrorBarParam`)::
>>> from plotpy.items import CurveItem
>>> from plotpy.styles import CurveParam
>>> param = CurveParam()
>>> param.label = 'My curve'
>>> curve = CurveItem(param)
>>> curve.set_data(x, y)
* or using the :py:class:`.PlotBuilder` (see :py:attr:`plotpy.builder.make`)::
>>> from plotpy.builder import make
>>> curve = make.curve(x, y, title='My curve')
Attach the curve to the plotting widget::
>>> plot.add_item(curve)
Display the plotting widget::
>>> plot.show()
>>> app.exec()
Image example
^^^^^^^^^^^^^
Create a basic image plotting widget (see also `Curve example`_),
using :py:class:`.PlotWidget` and :py:class:`.PlotOptions`::
>>> import guidata
>>> app = guidata.qapplication()
>>> from plotpy.plot import PlotWidget, PlotOptions
>>> plot = PlotWidget(options=PlotOptions(title="Example", type="image"))
...or using the :py:class:`.PlotBuilder` (see :py:attr:`plotpy.builder.make`)::
>>> from plotpy.builder import make
>>> plot = make.widget(title="Example", type="image")
Generate random data for testing purpose::
>>> import numpy as np
>>> data = np.random.rand(100, 100)
Create a simple image item:
* from the associated plot item class (e.g. `XYImageItem` to create
an image with non-linear X/Y axes): the item properties are then
assigned by creating the appropriate style parameters object
(e.g. :py:class:`.styles.ImageParam`)::
>>> from plotpy.items import ImageItem
>>> from plotpy.styles import ImageParam
>>> param = ImageParam()
>>> param.label = 'My image'
>>> image = ImageItem(param)
>>> image.set_data(data)
* or using the :py:class:`.PlotBuilder` (see :py:attr:`plotpy.builder.make`)::
>>> from plotpy.builder import make
>>> image = make.image(data, title='My image')
Final steps (see also `Curve example`_)::
>>> plot.add_item(image)
>>> plot.show()
>>> app.exec()
Shape example
^^^^^^^^^^^^^
A shape may be created:
* from the associated plot item class (e.g. `RectangleShape` to create a
rectangle): the item properties are then assigned by creating the
appropriate style parameters object (:py:class:`.styles.ShapeParam`)::
>>> from plotpy.items import RectangleShape
>>> from plotpy.styles import ShapeParam
>>> param = ShapeParam()
>>> param.title = 'My rectangle'
>>> rect_item = RectangleShape(0., 2., 4., 0., param)
* or using the :py:class:`.PlotBuilder` (see :py:attr:`plotpy.builder.make`)::
>>> from plotpy.builder import make
>>> rect_item = make.rectangle(0., 2., 4., 0., title='My rectangle')
Annotation example
^^^^^^^^^^^^^^^^^^
An annotated shape may be created:
* from the associated plot item class (e.g. `AnnotatedCircle` to
create an annotated circle): the item properties are then assigned
by creating the appropriate style parameters object
(:py:class:`.styles.AnnotationParam`)::
>>> from plotpy.items import AnnotatedCircle
>>> from plotpy.styles import AnnotationParam
>>> param = AnnotationParam()
>>> param.title = 'My circle'
>>> circle_item = AnnotatedCircle(0., 2., 4., 0., param)
* or using the :py:class:`.PlotBuilder` (see :py:attr:`plotpy.builder.make`)::
>>> from plotpy.builder import make
>>> circle_item = make.annotated_circle(0., 2., 4., 0., title='My circle')
|