Debian Codegraph @

examples/pgplot.pl

raw symbols SCIP
1 =head1 NAME
2
3 std_pgplot - Examples of PGPLOT routines.
4
5 =head1 SYNOPSIS
6
7 std_pgplot.pl
8
9 =head1 DESCRIPTION
10
11 This file is intended to show the use of PGPLOT routines using the
12 object-oriented approach.
13
14 =cut
15
16 use PDLscip-perl cpan PGPLOT 2.36 PDL/;
17 use PDL::Graphics::PGPLOT::Windowscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/;
18
19
20 ##
21 ## Test all PGPLOT routines.
22 ##
23 my $random = grandomscip-perl cpan PGPLOT 2.36 main/grandom().(1000);
24 my ($xc, $yc)=histscip-perl cpan PGPLOT 2.36 main/hist(). $random;
25 my $x=zeroesscip-perl cpan PGPLOT 2.36 main/zeroes().(100)->xlinvalsscip-perl cpan PGPLOT 2.36 main/xlinvals().(-5,5);
26 my $y=expscip-perl cpan PGPLOT 2.36 main/exp().(-$x*$x/2);
27
28
29
30 printscip-perl cpan PGPLOT 2.36 main/print(). "First we will test all functions in PGPLOT.\n";
31 printscip-perl cpan PGPLOT 2.36 main/print(). "We also will show most of the options\n";
32 printscip-perl cpan PGPLOT 2.36 main/print(). "After each plot - please press <CR> to proceed - type q<CR> to quit.\n";
33
34 function_to_doscip-perl cpan PGPLOT 2.36 main/function_to_do(). ('dev(), env(), hold(), release(), bin(), line()');
35
36 #
37 # Create a window object to which we will subsequently plot.
38 #
39 my $w = PDL::Graphics::PGPLOT::Window->newscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/new().({Device => '/xw', Aspect => 1,
40 WindowWidth => 7});
41 # Note how we call the functions via the window object. This particular
42 # call will set the axis colour to Red.
43 $w->envscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/env().(-5, 5, 0, maxscip-perl cpan PGPLOT 2.36 main/max().($yc)*1.1, {Axiscolour => 'Red'});
44
45 # Plot a histogram.
46 $w->binscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/bin().($xc, $yc);
47
48 # Hold the plot. You can subsequently test whether the plot is held
49 # by calling held() - which returns true (1) if the plot is held.
50 $w->holdscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/hold().();
51
52 # This draws a line plot on top of the histogram. Note that you can
53 # refer to options using only the first part of their name - and case
54 # doesn't matter either. It is advised however to use the full name
55 # for options since in some cases you could imagine getting the
56 # wrong result.
57 $w->linescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/line().($x, $y*maxscip-perl cpan PGPLOT 2.36 main/max().($yc),
58 {LineSty => 'Dashed', Color => 'Blue', LineWidth => 5});
59
60 # Release your connection so that the next plotting command will
61 # erase the screen and create a new plot.
62 $w->releasescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/release().();
63
64 # These two are just a convenience functions for this demo..
65 next_plotscip-perl cpan PGPLOT 2.36 main/next_plot().();
66 function_to_doscip-perl cpan PGPLOT 2.36 main/function_to_do(). ('errb() & points()');
67
68 # Create some more data - this time for plots with errorbars.
69 my $xd = pdlscip-perl cpan PGPLOT 2.36 main/pdl().(1,3,7,10);
70 my $yd = pdlscip-perl cpan PGPLOT 2.36 main/pdl().(2, 7,5,7);
71 my $dy = sqrtscip-perl cpan PGPLOT 2.36 main/sqrt().($yd**2/12+0.2**2);
72
73 # This is how you set titles on plots. If you forget to you can
74 # use the label_axes() command (see below) to set them later.
75 $w->envscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/env().(0, 15, 0, 10, {Xtitle => 'X-data', Ytitle=>'Y-data',
76 Title => 'An example of errb and points',
77 Font => 'Italic'});
78
79 # Plot the data as points
80 $w->pointsscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/points().($xd, $yd);
81 # Overplot (implicit) the points with error-bars.
82 $w->errbscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/errb().($xd, $yd, $dy);
83 $w->releasescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/release().();
84
85 next_plotscip-perl cpan PGPLOT 2.36 main/next_plot().();
86 function_to_doscip-perl cpan PGPLOT 2.36 main/function_to_do().('line() poly(), cont(), label_axes() and text()');
87
88 # Create an image.
89 my $im = rvalsscip-perl cpan PGPLOT 2.36 main/rvals().(100, 100);
90
91 # Draw contours - and hold the plot because will clutter it some more.
92 $w->contscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/cont().($im, {NCOn => 4});
93 $w->holdscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/hold().();
94
95 # Note that the colours can be specified also in upper case. They can
96 # also be referred to with numbers in keeping with the PGPLOT tradition.
97 $w->linescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/line().(pdlscip-perl cpan PGPLOT 2.36 main/pdl().(0, 50), pdlscip-perl cpan PGPLOT 2.36 main/pdl().(0, 50), {Color => 'RED'});
98
99 # The corners of a polygon - note that the last should be equal to the
100 # first to get the expected results.
101 my $px = pdlscip-perl cpan PGPLOT 2.36 main/pdl().(20, 40, 40, 20, 20);
102 my $py = pdlscip-perl cpan PGPLOT 2.36 main/pdl().(80, 80, 100, 100, 80);
103
104 # The poly function draws polygons - note that we set the fill using
105 # the numerical notation here - this sets a hatched fill.
106 $w->polyscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/poly().($px, $py, {Fill => 3});
107 # Pay attention to the hatching command as it sets the properties using
108 # an anonymous hash again. I would normally construct this separately
109 # and use a variable for this for readability.
110 $w->polyscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/poly().($px, $py, {Color=>'Red', Fill => 3, Hatch => {Phase => 0.5}});
111
112 # label_axes() is a separate function to set the axis titles. This is
113 # often clearer although less compact than setting it directly in the
114 # env() function.
115 $w->label_axesscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/label_axes().('X-direction', 'Y-direction', 'Title', {Color => 'Yellow'});
116
117 # The text() command puts text on the display and can be displayed using
118 # different justifications, angles and fonts as well as colours.
119 $w->textscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/text().('Towards the centre', 24, 25, {Justification => 0.5, Angle=>45,
120 Font => 'Italic'});
121
122
123 next_plotscip-perl cpan PGPLOT 2.36 main/next_plot().();
124 function_to_doscip-perl cpan PGPLOT 2.36 main/function_to_do().('imag(), ctab(), hi2d and several panels');
125
126 #
127 # We now create a new window for image display since we want several
128 # panels in the plot window. This requires a new window to be created
129 # at present - changes to the code welcome.
130 #
131 $w->closescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/close().();
132 $w = PDL::Graphics::PGPLOT::Window->newscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/new().({Device => '/xw',
133 Aspect => 0.5,
134 NX => 2, NY => 2,
135 CharSize => 2});
136
137
138 # Display the image using a transform between the pixel coordinates
139 # and the display coordinates.
140 $w->imagscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/imag().($im, {Transform => pdlscip-perl cpan PGPLOT 2.36 main/pdl().([0, 0.1, 0, 0, 0, 0.1])});
141
142 # This command will go in the next panel and will display the image using
143 # a square root transfer function and square pixels (PIX => 1)
144 $w->imag1scip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/imag1().($im, {PIX => 1, ITF=>'Sqrt'});
145
146 # You set the colour table using ctab()
147 $w->ctabscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/ctab().('Fire');
148 $w->imagscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/imag().($im);
149
150 # A hold command in a multi-panel situation keeps you in the same panel.
151 $w->holdscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/hold().();
152 # So that you can overplot a contour plot for instance...
153 $w->contscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/cont().($im, {Color => 'Yellow'});
154 $w->releasescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/release().();
155
156 # The hi2d() function draws a 2D histogram of the data and could very
157 # likely be improved by someone with some extra time on their hands.
158 $w->hi2dscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/hi2d().($im->slicescip-perl cpan PGPLOT 2.36 main/slice().('0:-1:10,0:-1:10'));
159
160 next_plotscip-perl cpan PGPLOT 2.36 main/next_plot().();
161 function_to_doscip-perl cpan PGPLOT 2.36 main/function_to_do().('Several plot windows. focus_window(), window_list()');
162
163 #
164 # Multiple windows - the secret unveiled... Well, it is easy actually
165 # at least when you use the OO interface!
166 #
167 $w->closescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/close().();
168 my $w1 = PDL::Graphics::PGPLOT::Window->newscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/new().({Device => '/xw', Aspect => 1,
169 AxisColour => 'Blue',
170 WindowName => 'First',
171 WindowWidth => 6});
172 my $w2 = PDL::Graphics::PGPLOT::Window->newscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/new().({Device => '/xw', Aspect => 0.618,
173 AxisColour => 'Red',
174 WindowWidth => 6});
175 # First draw something in Window 1
176 $w1->linescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/line().($x, $x**2);
177 $w1->holdscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/hold().();
178
179 # Then switch to window 2...
180 my $ii = whichscip-perl cpan PGPLOT 2.36 main/which().($x>=0);
181 $w2->pointsscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/points().($x->indexscip-perl cpan PGPLOT 2.36 main/index().($ii), sqrtscip-perl cpan PGPLOT 2.36 main/sqrt().($x->indexscip-perl cpan PGPLOT 2.36 main/index().($ii)));
182 $w2->holdscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/hold().();
183
184 $w2->linescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/line().($x->indexscip-perl cpan PGPLOT 2.36 main/index().($ii), sqrtscip-perl cpan PGPLOT 2.36 main/sqrt().($x->indexscip-perl cpan PGPLOT 2.36 main/index().($ii)),
185 {Color => 'Red', Linestyle => 'dashed'});
186 $w2->releasescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/release().();
187
188 # Switch back to window 1 - note how easier it is with the OO interface.
189 $w1->pointsscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/points().($x, $x**2+$x->grandomscip-perl cpan PGPLOT 2.36 main/grandom().());
190 $w1->releasescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/release().();
191
192 # In the OO interface there is no built-in way to keep track of different
193 # windows in the way that the non-OO interface does, but on the other hand
194 # you don't really need it.
195
196 # See the std_pgplot.pl file for an example of this.
197
198 next_plotscip-perl cpan PGPLOT 2.36 main/next_plot().();
199 function_to_doscip-perl cpan PGPLOT 2.36 main/function_to_do().('legend(), cursor()');
200
201 # Let's close Window2 and continue our examples in window 1.
202 $w2->closescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/close().();
203
204 # The legend function draws legends on plots which can be for different
205 # symbols, linestyles, widths and mixtures.
206 $w1->legendscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/legend().(['Parabola', 'Scatter points'], -2, 20,
207 {Width => 5, LineStyle => ['Solid', undef], Symbol => [undef, 'Default']});
208
209 #
210 # Now read the cursor - different types of cursor can be chosen
211 printscip-perl cpan PGPLOT 2.36 main/print(). "Select a point using the cursor:\n";
212 my ($xp, $yp)=$w1->cursorscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/cursor().({Type => 'CrossHair'});
213 printscip-perl cpan PGPLOT 2.36 main/print(). "(X, Y)=($xp, $yp)\n";
214
215 next_plotscip-perl cpan PGPLOT 2.36 main/next_plot().();
216 function_to_doscip-perl cpan PGPLOT 2.36 main/function_to_do().('circle(), ellipse(), rectangle() and arrow()');
217
218 # The circle, ellipse and rectangle functions do not take note of the
219 # intrinsic display aspect ratio so if you want a really round circle,
220 # you must make sure that the aspect ratio is 1.
221 $w1->closescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/close().();
222 $w1 = PDL::Graphics::PGPLOT::Window->newscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/new().({Device => '/xs', Aspect => 1,
223 WindowWidth => 6});
224 $w1->envscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/env().(0, 100, 0,100);
225
226 # Draw a circle - at the moment the default fill-style is solid so we need
227 # to specify the outline fill to get a non-filled circle..
228 $w1->circlescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/circle().(50, 50, 10, {Fill => 'Outline'});
229 # The ellipse can be specified with major and minor axis and the rotation
230 # angle for the ellipse, but note that the angle must be specified in radians..
231 $w1->ellipsescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/ellipse().(40, 20, {MajorAxis => 30, MinorAxis=> 10,
232 Theta => 30*3.14/180, Colour => 'Red'});
233 # The angle must be specified in radians for the rectangle too....
234 $w1->rectanglescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/rectangle().(70, 70, {XSide => 10, Angle => 45*3.14/180});
235
236 # And finally draw an arrow...
237 $w1->arrowscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/arrow().(40, 20, 70, 20, {Color => 'Green'});
238
239 next_plotscip-perl cpan PGPLOT 2.36 main/next_plot().();
240 $w1->closescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/close().();
241
242 $w1 = PDL::Graphics::PGPLOT::Window->newscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/new().({Device => '/xs', Aspect => 1,
243 NX => 2, NY => 2});
244 $w1->linescip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/line().($x, $y);
245
246 # The important thing here is to show that you can jump directly to a given
247 # panel and start drawing in this..
248 $w1->binscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/bin().($xc, $yc, {Panel => 3});
249 $w1->envscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/env().(0, 1, 0, 1, {Axis => 'Box'});
250 $w1->textscip-perl cpan PGPLOT 2.36 PDL/Graphics/PGPLOT/Window/text().("That's all folks!", 0.5, 0.5, {Justification => 0.5, CharSize => 5,
251 Color => 'Yellow'});
252
253 next_plotscip-perl cpan PGPLOT 2.36 main/next_plot().();
254
255
256 sub function_to_doscip-perl cpan PGPLOT 2.36 main/function_to_do(). {
257
258 printscip-perl cpan PGPLOT 2.36 main/print(). "\n**************************\n";
259 printscip-perl cpan PGPLOT 2.36 main/print(). "* $_[0]\n";
260 printscip-perl cpan PGPLOT 2.36 main/print(). "**************************\n\n";
261
262 }
263
264
265 sub next_plotscip-perl cpan PGPLOT 2.36 main/next_plot(). {
266 my $message = shiftscip-perl cpan PGPLOT 2.36 main/shift().;
267
268 $message ||='';
269
270 printscip-perl cpan PGPLOT 2.36 main/print(). $message."\n";
271 my $in = <STDIN>;
272
273 if ($in =~ /^q/i) {
274 exitscip-perl cpan PGPLOT 2.36 main/exit().;
275 }
276
277
278 }