File: examples.rst

package info (click to toggle)
wxmplot 0.9.58-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,052 kB
  • sloc: python: 10,085; makefile: 88; sh: 2
file content (210 lines) | stat: -rw-r--r-- 6,434 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
.. _ch_examples:

==========================================================
wxmplot Examples
==========================================================

The :ref:`ch_overview` showed a few illustrative  examples using `wxmplot`.
Here we show a few more examples.  These and more are given in the *examples*
directory in the source distribution kit.

.. _wxmplot examples: https://github.com/newville/wxmplot/tree/master/examples

Dynamic examples not shown here
----------------------------------

Several examples that can be found at `wxmplot examples`_ are not shown here
either because they show many plots or are otherwise more complex.  They are
worth trying out.

*demo.py* will show several Line plot examples, including a plot which uses
a timer to simulate a dynamic plot, updating the plot as fast as it can -
typically 20 to 30 times per second, depending on your machine.

*stripchart.py* also shows dynamic, time-based plot.

*scope_mode_function.py* and *scope_mode_generator.py* both
show dynamic plots with data uddated with a user-supplied function that either
returns or yields datasets to update plot traces.

*theme_compare.py* renders the same plot with a selection of different themes.

*image_scroll.py* shows an updating set of images on a single display.
Perhaps surprisingly, this can be faster than updating the line plots.


Scatterplot Example
-------------------------

An example scatterplot can be produced with a script like this:

.. literalinclude:: ../examples/scatterplot.py

and gives a plot (after having selected by "lasso"ing) that looks like this:

.. image:: images/scatterplot.png
   :width: 85 %

Plotting with errorbars
----------------------------

An example plotting with error bars:


.. literalinclude:: ../examples/errorbar.py

gives:

.. image:: images/errorbar.png
   :width: 85 %


Plotting data from a datafile
-----------------------------------------

Reading data with `numpy.loadtext` and plotting:


.. literalinclude:: ../examples/plot_fromdatafile.py

gives:

.. image:: images/datafile_plot.png
   :width: 85 %


Using Left and Right Axes
----------------------------

An example using both right and left axes with different scales can be
created with:

.. literalinclude:: ../examples/leftright.py

and gives a plot that looks like this:

.. image:: images/two_axes.png
   :width: 85 %


Plotting with alpha-fill to show area under a curve
-----------------------------------------------------

It is sometimes desirable to fill the area below a curve, typically to 0.
Using the `alpha` value can be especially helpful for this, so that

.. literalinclude:: ../examples/fill_to_zero.py

will give:

.. image:: images/plot_alphafill_to_zero.png
   :width: 85 %


Plotting with alpha-fill to show uncertainty
-----------------------------------------------------

Another use of a filled band is to fill between two traces.  An important
use of this is to show uncertainties in a function, similar to showing
errorbars above.  If `dy` and `fill=True` are both given, then a band
between `y-dy` and `y+dy` will be filled, as with:


.. literalinclude:: ../examples/fill_uncertainties.py

which gives:

.. image:: images/plot_alphafill_to_dy.png
   :width: 85 %

Of course, you can use that to recast showing a band between any two curves
by assigning the average of the 2 curves to `y` and half the difference to
`dy`, and perhaps setting `linewidth=0` to suppress showing the mean value.

Using `set_data_generator` for user-controlled, dynamic plotting
---------------------------------------------------------------------

There are three examples that use :func:`set_data_generator` to specify how to
update a plot from a user-supplied function.  As seen in these examples, the
function definied can either return data to update the data, or it can use a
Python geneator to yield the data.  In both cases, you first create a plot (it
can be empty), and then set the function for that plot window to call to grab
new data.  The plot window will then periodically call the function you supply,
with a time interval (in milliseconds) given by the `polltime` argument.  With
a simple function, it might look like

.. literalinclude:: ../examples/scope_mode_function.py


This will generate a continuously updating plot adding data as it goes:

.. video:: _static/scope_mode_function.mp4
   :alt: capture of images generated from scope_mode_function.py
   :muted:


As a second example, this time using a generator, you might do something like this:

.. literalinclude:: ../examples/scope_mode_generator.py

which will generate a plot like this:

.. video:: _static/scope_mode_generator.mp4
   :alt: capture of images generated from scope_mode_generator.py
   :muted:

Note that your function should return or yield a list of (x, y) pairs.

As a third example, and by way of comparison with the matplotlib example at
https://matplotlib.org/stable/gallery/animation/strip_chart.html, a similar
result can be generated with the somewhat shorter and less involved code
example

.. literalinclude:: ../examples/scope_mode_mpl_compare.py

Unlike with the matplotlib example, which mixes data generation and management
with plotting code, the :class:`Scope` here only generates the code, and
wxmplot functions handles all the plotting.  This code is both shorter and
better designed than the standard matplotlib example.


Displaying and image of a TIFF file
--------------------------------------

Reading a TIFF file and showing the image:


.. literalinclude:: ../examples/tiff_display.py

gives:

.. image:: images/tifffile_image.png
   :width: 85 %


3-Color Image
-----------------

If the data array has three dimensions, and has a shape of (NY, NX, 3), it
is assumed to be a 3 color map, holding Red, Green, and Blue intensities.
In this case, the Image Frame will show sliders and min/max controls for
each of the three colors.

.. literalinclude:: ../examples/rgb_image.py

giving a plot that would look like this:

.. image:: images/image_3color.png
   :width: 85%

Note that there is also an Image->Toggle Background Color
(Black/White) menu selection that can switch the zero intensity color
between black and white.  The same image with a white background looks
like:


.. image:: images/image_3color_white.png
   :width: 85%

This gives a slightly different view of the same data, with results that
may be more suitable for printed documents and presentations.