Debian Codegraph @

doc/features/items/examples.rst

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