1Prima(3) User Contributed Perl Documentation Prima(3)
2
3
4
6 PDL::Demos::Prima - PDL demo for PDL::Graphics::Prima
7
9 You can enjoy this demo in any number of ways. First, you can invoke
10 the demo from the command line by saying
11
12 perl -MPDL::Demos::Prima
13
14 Second, you can invoke the demo from with the pdl shell by saying
15
16 pdl> demo prima
17
18 Finally, all of the content is in the pod documentation, so you can
19 simply read this, though it won't be quite so interactive. :-)
20
21 perldoc PDL::Demos::Prima
22 podview PDL::Demos::Prima
23
25 The documentation in this module is meant to give a short, hands-on
26 introduction to PDL::Graphics::Prima, a plotting library written on top
27 of the Prima GUI toolkit.
28
29 use PDL::Graphics::Prima::Simple
30 To get started, you will want to use PDL::Graphics::Prima::Simple. This
31 module provides a set of friendly wrappers for simple, first-cut data
32 visualization. PDL::Graphics::Prima, the underlying library, is a
33 general-purpose 2D plotting library built as a widget in the Prima GUI
34 toolkit, but we don't need the full functionality for the purposes of
35 this demo.
36
37 use PDL::Graphics::Prima::Simple;
38 my $x = sequence(100)/10;
39 line_plot($x, $x->sin);
40
41 More than just lines!
42 In addition to numerous ways to plot x/y data, you can also plot
43 distributions and images. The best run-down of the simple plotting
44 routines can be found in the Synopsis for PDL::Graphics::Prima::Simple.
45
46 $distribution = grandom(100);
47 hist_plot($distribution);
48
49 $x = sequence(100)/10;
50 cross_plot($x, $x->sin);
51
52 $image = rvals(100, 100);
53 matrix_plot($image);
54
55 Mouse Interaction
56 Plots allow for mouse interaction, herein referred to as twiddling. You
57 can resize the window, zoom with the scroll wheel, or click and drag
58 the canvas around. There is also a right-click zoom-rectangle, and a
59 right-click context menu.
60
61 hist_plot(grandom(100));
62
63 # Run this, then try using your mouse
64
65 In your Perl scripts, and in the PDL shell for some operating systems
66 and some versions of Term::ReadLine, twiddling will cause your script
67 to pause when you create a new plot. To resume your script or return
68 execution to the shell, either close the window or press 'q'.
69
70 # If your PDL shell supports simultaneous
71 # input and plot interaction, running this
72 # should display both plots simultaneously:
73
74 $x = sequence(100)/10;
75 cross_plot($x, $x->sin);
76 line_plot($x, $x->cos);
77
78 Multiple plots without blocking
79 The blocking behavior just discussed is due to what is called
80 autotwiddling. To turn this off, simply send a boolean false value to
81 auto_twiddle. Then, be sure to invoke twiddling when you're done
82 creating your plots.
83
84 auto_twiddle(0);
85 hist_plot(grandom(100));
86 matrix_plot(rvals(100, 100));
87 twiddle();
88
89 Once turned off, autotwiddling will remain off until you turn it back
90 on.
91
92 # autotwiddling still off
93 hist_plot(grandom(100));
94 matrix_plot(rvals(100, 100));
95 twiddle();
96
97 Adding a title and axis labels
98 Functions like hist_plot, cross_plot, and matrix_plot actually create
99 and return plot objects which you can subsequently modify. For example,
100 adding a title and axis labels are pretty easy. For titles, you call
101 the title method on the plot object. For axis labels, you call the
102 label method on the axis objects.
103
104 # Make sure autotwiddling is off in your script
105 auto_twiddle(0);
106
107 # Build the plot
108 my $x = sequence(100)/10;
109 my $plot = line_plot($x, $x->sin);
110
111 # Add the title and labels
112 $plot->title('Harmonic Oscillator');
113 $plot->x->label('Time [s]');
114 $plot->y->label('Displacement [cm]');
115
116 # Manually twiddle once everything is finished
117 twiddle();
118
119 Saving to a file
120 PDL::Graphics::Prima::Simple excels at user interaction, but you can
121 save your plots to a file using save_to_file or save_to_postscript
122 methods, or by right-clicking and selecting the appropriate menu
123 option.
124
125 auto_twiddle(0);
126 $x = sequence(100)/10;
127 line_plot($x, $x->sin)->save_to_postscript;
128
129 # You can supply a filename to the method if you like.
130 # Also available is save_to_file, which saves to raster
131 # file formats. Expect save_to_postscript to be merged
132 # into save_to_file in the future.
133
134 Adding additional data to the plot
135 Once you have created a plot, you can add additional data to it. You
136 achieve this by adding a new DataSet with the data you want displayed.
137
138 auto_twiddle(0);
139 my $plot = hist_plot(grandom(100));
140
141 # Add a Gaussian curve that "fits" the data
142 use PDL::Constants qw(PI);
143 my $fit_xs = zeroes(100)->xlinvals(-2, 2);
144 my $fit_ys = exp(-$fit_xs**2 / 2) / sqrt(2*PI);
145 $plot->dataSets->{fit_curve} = ds::Pair($fit_xs, $fit_ys);
146
147 twiddle();
148
149 The default plot type for pairwise data is Diamonds. You can choose a
150 different pairwise plot type, or even mix and match multiple pairwise
151 plot types.
152
153 auto_twiddle(0);
154 my $plot = hist_plot(grandom(100));
155
156 # Add a Gaussian curve that "fits" the data
157 use PDL::Constants qw(PI);
158 my $fit_xs = zeroes(200)->xlinvals(-5, 5);
159 my $fit_ys = exp(-$fit_xs**2 / 2) / sqrt(2*PI);
160 $plot->dataSets->{fit_curve} = ds::Pair($fit_xs, $fit_ys,
161 # Use lines
162 plotTypes => [
163 ppair::Lines(
164 # with a thickness of three pixels
165 lineWidth => 3,
166 # And the color red
167 color => cl::LightRed,
168 ),
169 ppair::Diamonds,
170 ],
171 );
172
173 twiddle();
174
175 The plot command
176 If you want to specify everything in one command, you can use the plot
177 function. This lets you put everything together that we've already
178 discussed, including multiple DataSets in a single command, title
179 specification, and x and y axis options.
180
181 # Generate some data:
182 my $xs = sequence(100)/10 + 0.1;
183 my $ys = $xs->sin + $xs->grandom / 10;
184 my $y_err = $ys->grandom/10;
185
186 # Plot the data and the fit
187 plot(
188 -data => ds::Pair($xs, $ys,
189 plotTypes => [
190 ppair::Triangles(filled => 1),
191 ppair::ErrorBars(y_err => $y_err),
192 ],
193 ),
194 -fit => ds::Func(\&PDL::sin,
195 lineWidth => 3,
196 color => cl::LightRed,
197 ),
198 -note => ds::Note(
199 pnote::Text('Incoming Signal',
200 x => 0.2,
201 y => sin(0.2) . '-3em',
202 ),
203 ),
204 title => 'Noisey Sine Wave',
205 x => {
206 label => 'Time [s]',
207 scaling => sc::Log,
208 },
209 y => { label => 'Measurement [Amp]' },
210 );
211
212 Enjoy PDL::Graphics::Prima!
213 I hope you've enjoyed the tour, and I hope you find
214 PDL::Graphics::Prima to be a useful plotting tool!
215
216 # Thanks!
217
219 David Mertens "dcmertens.perl@gmail.com"
220
222 Copyright (c) 2013, David Mertens. All righs reserved.
223
224 This module is free software; you can redistribute it and/or modify it
225 under the same terms as Perl itself. See perlartistic.
226
227
228
229perl v5.30.2 2020-04-02 Prima(3)