| 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 PDL; |
| 17 | use PDL::Graphics::PGPLOT::Window; |
| 18 | |
| 19 | |
| 20 | ## |
| 21 | ## Test all PGPLOT routines. |
| 22 | ## |
| 23 | my $random = grandom(1000); |
| 24 | my ($xc, $yc)=hist $random; |
| 25 | my $x=zeroes(100)->xlinvals(-5,5); |
| 26 | my $y=exp(-$x*$x/2); |
| 27 | |
| 28 | |
| 29 | |
| 30 | print "First we will test all functions in PGPLOT.\n"; |
| 31 | print "We also will show most of the options\n"; |
| 32 | print "After each plot - please press <CR> to proceed - type q<CR> to quit.\n"; |
| 33 | |
| 34 | 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->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->env(-5, 5, 0, max($yc)*1.1, {Axiscolour => 'Red'}); |
| 44 | |
| 45 | # Plot a histogram. |
| 46 | $w->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->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->line($x, $y*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->release(); |
| 63 | |
| 64 | # These two are just a convenience functions for this demo.. |
| 65 | next_plot(); |
| 66 | function_to_do ('errb() & points()'); |
| 67 | |
| 68 | # Create some more data - this time for plots with errorbars. |
| 69 | my $xd = pdl(1,3,7,10); |
| 70 | my $yd = pdl(2, 7,5,7); |
| 71 | my $dy = 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->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->points($xd, $yd); |
| 81 | # Overplot (implicit) the points with error-bars. |
| 82 | $w->errb($xd, $yd, $dy); |
| 83 | $w->release(); |
| 84 | |
| 85 | next_plot(); |
| 86 | function_to_do('line() poly(), cont(), label_axes() and text()'); |
| 87 | |
| 88 | # Create an image. |
| 89 | my $im = rvals(100, 100); |
| 90 | |
| 91 | # Draw contours - and hold the plot because will clutter it some more. |
| 92 | $w->cont($im, {NCOn => 4}); |
| 93 | $w->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->line(pdl(0, 50), 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 = pdl(20, 40, 40, 20, 20); |
| 102 | my $py = 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->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->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_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->text('Towards the centre', 24, 25, {Justification => 0.5, Angle=>45, |
| 120 | Font => 'Italic'}); |
| 121 | |
| 122 | |
| 123 | next_plot(); |
| 124 | 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->close(); |
| 132 | $w = 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->imag($im, {Transform => 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->imag1($im, {PIX => 1, ITF=>'Sqrt'}); |
| 145 | |
| 146 | # You set the colour table using ctab() |
| 147 | $w->ctab('Fire'); |
| 148 | $w->imag($im); |
| 149 | |
| 150 | # A hold command in a multi-panel situation keeps you in the same panel. |
| 151 | $w->hold(); |
| 152 | # So that you can overplot a contour plot for instance... |
| 153 | $w->cont($im, {Color => 'Yellow'}); |
| 154 | $w->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->hi2d($im->slice('0:-1:10,0:-1:10')); |
| 159 | |
| 160 | next_plot(); |
| 161 | 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->close(); |
| 168 | my $w1 = 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->new({Device => '/xw', Aspect => 0.618, |
| 173 | AxisColour => 'Red', |
| 174 | WindowWidth => 6}); |
| 175 | # First draw something in Window 1 |
| 176 | $w1->line($x, $x**2); |
| 177 | $w1->hold(); |
| 178 | |
| 179 | # Then switch to window 2... |
| 180 | my $ii = which($x>=0); |
| 181 | $w2->points($x->index($ii), sqrt($x->index($ii))); |
| 182 | $w2->hold(); |
| 183 | |
| 184 | $w2->line($x->index($ii), sqrt($x->index($ii)), |
| 185 | {Color => 'Red', Linestyle => 'dashed'}); |
| 186 | $w2->release(); |
| 187 | |
| 188 | # Switch back to window 1 - note how easier it is with the OO interface. |
| 189 | $w1->points($x, $x**2+$x->grandom()); |
| 190 | $w1->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_plot(); |
| 199 | function_to_do('legend(), cursor()'); |
| 200 | |
| 201 | # Let's close Window2 and continue our examples in window 1. |
| 202 | $w2->close(); |
| 203 | |
| 204 | # The legend function draws legends on plots which can be for different |
| 205 | # symbols, linestyles, widths and mixtures. |
| 206 | $w1->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 | print "Select a point using the cursor:\n"; |
| 212 | my ($xp, $yp)=$w1->cursor({Type => 'CrossHair'}); |
| 213 | print "(X, Y)=($xp, $yp)\n"; |
| 214 | |
| 215 | next_plot(); |
| 216 | 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->close(); |
| 222 | $w1 = PDL::Graphics::PGPLOT::Window->new({Device => '/xs', Aspect => 1, |
| 223 | WindowWidth => 6}); |
| 224 | $w1->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->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->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->rectangle(70, 70, {XSide => 10, Angle => 45*3.14/180}); |
| 235 | |
| 236 | # And finally draw an arrow... |
| 237 | $w1->arrow(40, 20, 70, 20, {Color => 'Green'}); |
| 238 | |
| 239 | next_plot(); |
| 240 | $w1->close(); |
| 241 | |
| 242 | $w1 = PDL::Graphics::PGPLOT::Window->new({Device => '/xs', Aspect => 1, |
| 243 | NX => 2, NY => 2}); |
| 244 | $w1->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->bin($xc, $yc, {Panel => 3}); |
| 249 | $w1->env(0, 1, 0, 1, {Axis => 'Box'}); |
| 250 | $w1->text("That's all folks!", 0.5, 0.5, {Justification => 0.5, CharSize => 5, |
| 251 | Color => 'Yellow'}); |
| 252 | |
| 253 | next_plot(); |
| 254 | |
| 255 | |
| 256 | sub function_to_do { |
| 257 | |
| 258 | print "\n**************************\n"; |
| 259 | print "* $_[0]\n"; |
| 260 | print "**************************\n\n"; |
| 261 | |
| 262 | } |
| 263 | |
| 264 | |
| 265 | sub next_plot { |
| 266 | my $message = shift; |
| 267 | |
| 268 | $message ||=''; |
| 269 | |
| 270 | print $message."\n"; |
| 271 | my $in = <STDIN>; |
| 272 | |
| 273 | if ($in =~ /^q/i) { |
| 274 | exit; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | } |