| 1 | ############################## | ||
| 2 | # Gnuplot_demo package for PDL | ||
| 3 | # | ||
| 4 | # To use this manually: | ||
| 5 | # use PDL::Demos::Screen; | ||
| 6 | # do 'Gnuplot_demo.pm'; | ||
| 7 | # PDL::Demos::Gnuplot_demo::run(); | ||
| 8 | # | ||
| 9 | # Authors: Dima Kogan & Craig DeForest | ||
| 10 | |||
| 11 | package PDL::Demos::Gnuplot_demo; | ||
| 12 | |||
| 13 | use PDL; | ||
| 14 | |||
| 15 | BEGIN { | ||
| 16 | eval 'use PDL::Graphics::Gnuplot;'; | ||
| 17 | if ($@ or !defined($PDL::Graphics::Gnuplot::VERSION)) { | ||
| 18 | eval <<'EOF'; | ||
| 19 | sub run { | ||
| 20 | print qq{ | ||
| 21 | |||
| 22 | PDL::Graphics::Gnuplot is required for this demo, but didn't load. You may have | ||
| 23 | to go get it from CPAN (http://search.cpan.org). You might also need to get the | ||
| 24 | external "gnuplot" app (http://www.gnuplot.info). | ||
| 25 | |||
| 26 | }; | ||
| 27 | } | ||
| 28 | EOF | ||
| 29 | } | ||
| 30 | return 1; | ||
| 31 | } | ||
| 32 | |||
| 33 | use PDL::ImageND; | ||
| 34 | |||
| 35 | use PDL::Demos::Screen; # This is awful but seems to be needed since Screen.pm is where the Routines are located. -CED 2/2013 | ||
| 36 | |||
| 37 | PDL::Demos::Routines->import(); | ||
| 38 | sub comment($); | ||
| 39 | sub act($); | ||
| 40 | sub output; | ||
| 41 | |||
| 42 | |||
| 43 | sub run { | ||
| 44 | local($PDL::debug) = 0; | ||
| 45 | local($PDL::verbose) = 0; | ||
| 46 | |||
| 47 | |||
| 48 | $s = q| | ||
| 49 | # ensure that the module is loaded | ||
| 50 | use PDL::Graphics::Gnuplot; | ||
| 51 | |||
| 52 | # Create a Gnuplot object - the default device displays on most | ||
| 53 | # operating systems. (No plot window yet - just the object). | ||
| 54 | |||
| 55 | $w = gpwin(%%params%%); | ||
| 56 | |||
| 57 | # Create variables to plot | ||
| 58 | $x = xvals(1000); | ||
| 59 | $y = $x/1000 * sin($x/10); | ||
| 60 | |||
| 61 | |; | ||
| 62 | if(!defined($PDL::Graphics::Gnuplot::VERSION)) { | ||
| 63 | die q{ | ||
| 64 | |||
| 65 | ******************************************************************************* | ||
| 66 | This demo requires both the external "gnuplot" application and the module | ||
| 67 | "PDL::Graphics::Gnuplot". You don't seem to have the module installed on your | ||
| 68 | system. You might want to get it from CPAN and try again. | ||
| 69 | ******************************************************************************* | ||
| 70 | |||
| 71 | }; | ||
| 72 | } | ||
| 73 | |||
| 74 | if(!defined($PDL::Graphics::Gnuplot::valid_terms)) { | ||
| 75 | my $ww = new PDL::Graphics::Gnuplot; | ||
| 76 | } | ||
| 77 | |||
| 78 | if($PDL::Graphics::Gnuplot::valid_terms->{wxt}) { | ||
| 79 | $subst = "wxt, size=>[8,6,'in'], title=>'Gnuplot demo window', persist=>0"; | ||
| 80 | } elsif($ENV{DISPLAY}) { | ||
| 81 | $subst = "x11, size=>[8,6,'in'], title=>'Gnuplot demo window', persist=>0"; | ||
| 82 | } else { | ||
| 83 | $subst = ""; | ||
| 84 | } | ||
| 85 | $s=~s/\%\%params\%\%/$subst/; | ||
| 86 | act $s; | ||
| 87 | |||
| 88 | |||
| 89 | $s = q| | ||
| 90 | # ensure that the module is loaded | ||
| 91 | use PDL::Graphics::Gnuplot; | ||
| 92 | |||
| 93 | # Create a Gnuplot object - the default device displays on most | ||
| 94 | # operating systems. (No plot window yet - just the object). | ||
| 95 | $w = gpwin(%%params%%); | ||
| 96 | |||
| 97 | # Create variables to plot | ||
| 98 | $x = xvals(1000)/1000; | ||
| 99 | $y = $x * sin(100 * $x); | ||
| 100 | |||
| 101 | # Generate a line plot. Plot parameters in front. | ||
| 102 | $w->lines({title=>" x * sin(100 x) ",xl=>"Ordinate",yl=>"Abscissa"}, $x, $y ); | ||
| 103 | |; | ||
| 104 | $s =~s/\%\%params\%\%/$subst/; | ||
| 105 | act $s; | ||
| 106 | |||
| 107 | act q| | ||
| 108 | |||
| 109 | # You can set persistent plot parameters with "options". | ||
| 110 | $w->options(title=>"Two lines", xl=>"Ordinate", yl=>"Abscissa"); | ||
| 111 | |||
| 112 | # A two-line plot | ||
| 113 | $y2 = sqrt($x) * cos(100*$x); | ||
| 114 | $w->lines($x,$y,{},$x,$y2); | ||
| 115 | |||
| 116 | |; | ||
| 117 | |||
| 118 | act q| | ||
| 119 | # You can set persistent plot parameters with "options". | ||
| 120 | $w->options(title=>"Two lines", xl=>"Ordinate", yl=>"Abscissa"); | ||
| 121 | |||
| 122 | # A two-line plot. | ||
| 123 | # Groups of data are separated by non-PDL options -- in this | ||
| 124 | # case, the null hash since there are no per-curve options. | ||
| 125 | |||
| 126 | $y2 = sqrt($x) * cos(100*$x); | ||
| 127 | $w->lines($x,$y,{},$x,$y2); | ||
| 128 | |||
| 129 | # A two-line plot with legend. | ||
| 130 | # The legend for each line separates the groups of PDL data. | ||
| 131 | |||
| 132 | $w->options(title=>"Two lines (with legend)", key=>'left'); | ||
| 133 | $w->lines( legend=>"sin",$x,$y, legend=>"cos",$x,$y2 ); | ||
| 134 | |||
| 135 | # | ||
| 136 | |; | ||
| 137 | |||
| 138 | act q| | ||
| 139 | # You can also generate multiline plots with threading. | ||
| 140 | # Here, $x is a 1000-element 1-D PDL, and $yy is a 1000x2 2-D PDL. | ||
| 141 | |||
| 142 | $x = xvals(1000)/1000; | ||
| 143 | ($y,$y2) = ( $x * sin(100 * $x), sqrt($x) * cos(100 * $x) ); | ||
| 144 | $yy = pdl( $y, $y2 ); | ||
| 145 | |||
| 146 | # all options can be abbreviated to the smallest unique string. | ||
| 147 | # Here, "tit" stands for "title", and "le" for "legend". | ||
| 148 | |||
| 149 | $w->lines({tit=>"2-lines threaded"}, le=>["sin", "cos"], $x, $yy); | ||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | |; | ||
| 154 | |||
| 155 | act q| | ||
| 156 | # line plots are far from the only thing you can do! | ||
| 157 | |||
| 158 | # Here is the same plot, with points and some tweaks to the axis labels. | ||
| 159 | $w->options(tit=>"2 sets of points"); | ||
| 160 | |||
| 161 | $l = ["sin", "cos"]; | ||
| 162 | $w->points({xtics=>{rotate=>45,offset=>[0,-1.5],font=>',14'}, | ||
| 163 | xlab=>"Tilted Labels in 14-point text" | ||
| 164 | }, | ||
| 165 | le=>$l, $x, $yy); | ||
| 166 | |||
| 167 | |||
| 168 | |||
| 169 | |; | ||
| 170 | |||
| 171 | act q| | ||
| 172 | # Many types of plot are supported, using the "plot" command | ||
| 173 | # and the "with" curve option. Here, we can mix and match points and lines. | ||
| 174 | |||
| 175 | # You can also set plot options *temporarily* with a hash ref at the start of the | ||
| 176 | # plot call: | ||
| 177 | |||
| 178 | $w->plot( {title=>"Points and lines mixed"}, | ||
| 179 | with=>'points', le=>'sin (points)', $x, $y, | ||
| 180 | with=>'lines', le=>'cos (line)', $x, $y2); | ||
| 181 | |||
| 182 | |||
| 183 | |; | ||
| 184 | |||
| 185 | act q| | ||
| 186 | # Many types of plot are supported, using the "plot" command | ||
| 187 | # and the "with" curve option. Here, we can mix and match points and lines. | ||
| 188 | |||
| 189 | $w->plot( with=>'points', le=>'sin (points)', $x, $y, | ||
| 190 | with=>'lines', le=>'cos (line)', $x, $y2); | ||
| 191 | |||
| 192 | $x = xvals(51)/50; | ||
| 193 | ($y,$y2) = ( $x * sin(20 * $x), sqrt($x) * cos(20 * $x) ); | ||
| 194 | $radii = 0.01 * (0.25 - ($x*2 - 0.5)**2); | ||
| 195 | |||
| 196 | # Here are some other options. | ||
| 197 | $w->plot( {title=>"Circles and Steps"}, | ||
| 198 | le=>"Circles", with=>'circles', $x, $y, $radii, | ||
| 199 | le=>"Steps", with=> 'steps', $x, $y2 | ||
| 200 | ); | ||
| 201 | |; | ||
| 202 | |||
| 203 | act q| | ||
| 204 | # You can plot multiple plots on one pane with "multiplot". | ||
| 205 | |||
| 206 | $x = xvals(51)/50; | ||
| 207 | ($y,$y2) = ( $x * sin(20 * $x), sqrt($x) * cos(20 * $x) ); | ||
| 208 | |||
| 209 | |||
| 210 | $w->multiplot(layout=>[1,2]); | ||
| 211 | |||
| 212 | $w->plot( {title=>"Impulses"}, with=> 'impulses', $x, $y ); | ||
| 213 | $w->plot( {title=>"Filled Curves"}, with => "filledcurves", $x, $y, $y2 ); | ||
| 214 | |||
| 215 | $w->end_multi(); | ||
| 216 | |; | ||
| 217 | |||
| 218 | act q| | ||
| 219 | # 2-D data... | ||
| 220 | |||
| 221 | $rv = rvals(51,51)/2; | ||
| 222 | $im = 25 * cos($rv) / ($rv+1.5); | ||
| 223 | |||
| 224 | $w->multiplot(layout=>[2,2]); | ||
| 225 | $w->options(justify=>1); # set nice aspect ratio | ||
| 226 | $w->plot( {tit=>'Default color map'}, with=>'image', $im ); | ||
| 227 | $w->plot( {tit=>'Grayscale', clut=>'gray'}, with=>'image', $im ); | ||
| 228 | $w->plot( {tit=>'heat map', clut=>'heat1'}, with=>'image', $im ); | ||
| 229 | $w->plot( {tit=>'3d perspective', trid=>1}, with=>'pm3d', $im ); | ||
| 230 | $w->end_multi; | ||
| 231 | |||
| 232 | |; | ||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 | act q| | ||
| 237 | # You can indpendently specify color and position on surface plots, | ||
| 238 | # and can overlay multiple types of plot -- even in 3D. | ||
| 239 | # | ||
| 240 | $rv = rvals(101,101)/5; $im = cos($rv)/($rv+2.5); | ||
| 241 | $grad = sumover $im->range([[-1,0],[1,0]],[$im->dims],'e') * pdl(-1,1); | ||
| 242 | |||
| 243 | $im2 = $im->indexND(ndcoords(26,26)*4); # subsample $im | ||
| 244 | |||
| 245 | $w->reset; | ||
| 246 | $w->options( trid=>1, hidden=>'front', colorbox=>0, clut=>'heat1' ); | ||
| 247 | |||
| 248 | $w->multiplot(layout=>[2,2]); | ||
| 249 | |||
| 250 | $w->plot( {title=>"A colormap-shaded 3-d surface plot"}, with=>'pm3d', $im ); | ||
| 251 | |||
| 252 | $w->plot( {title=>"Perspective 3-d surface plot"}, | ||
| 253 | with=>'pm3d', xvals($im), yvals($im), $im, $grad ); | ||
| 254 | |||
| 255 | $w->plot( {title=>"Perspective grid plot"}, | ||
| 256 | with=>'lines', xvals($im2)*4, yvals($im2)*4, $im2 ); | ||
| 257 | |||
| 258 | $w->plot( {title=>"Combined"}, | ||
| 259 | with=>'pm3d', xvals($im),yvals($im), $im, $grad, | ||
| 260 | with=>'lines', xvals($im2)*4, yvals($im2)*4, $im2 ); | ||
| 261 | |||
| 262 | $w->end_multi; | ||
| 263 | |||
| 264 | |; | ||
| 265 | |||
| 266 | |||
| 267 | use File::Spec; | ||
| 268 | $d = File::Spec->catdir( "PDL", "Demos" ); | ||
| 269 | $m51path = undef; | ||
| 270 | foreach my $path ( @INC ) { | ||
| 271 | my $check = File::Spec->catdir( $path, $d ); | ||
| 272 | if ( -d $check ) { $m51path = $check; last; } | ||
| 273 | } | ||
| 274 | if( defined($m51path) && -e "$m51path/m51.fits") { | ||
| 275 | $m51path = "$m51path/m51.fits"; | ||
| 276 | } else { | ||
| 277 | comment q| | ||
| 278 | |||
| 279 | ****************************************************** | ||
| 280 | You seem to be missing the file 'm51.fits', which | ||
| 281 | should be included in the PDL distribution. Without | ||
| 282 | it, I can't show you the m51 image demos, so I'll | ||
| 283 | quit now. | ||
| 284 | ****************************************************** | ||
| 285 | |; | ||
| 286 | return; | ||
| 287 | } | ||
| 288 | |||
| 289 | $s = q| | ||
| 290 | # Images ... | ||
| 291 | |||
| 292 | $m51 = rfits('%%m51%%'); | ||
| 293 | |||
| 294 | $w->reset; | ||
| 295 | $w->image({j=>1, clut=>'gray', title=>"M51 galaxy"}, with=>'image',$m51 ); | ||
| 296 | |; | ||
| 297 | |||
| 298 | $s=~ s/\%\%m51\%\%/$m51path/; | ||
| 299 | act $s; | ||
| 300 | |||
| 301 | $s = q| | ||
| 302 | |||
| 303 | $m51 = rfits('%%m51%%')->slice('0:-1:4,0:-1:4'); | ||
| 304 | |||
| 305 | $m51s = $m51->convolveND(ones(11,11)/11**2); | ||
| 306 | |||
| 307 | $w->options(clut=>'heat2', trid=>1); | ||
| 308 | |||
| 309 | # 3-D display of M51: various angles (note "columnsfirst" in multiplot) | ||
| 310 | |||
| 311 | $w->multiplot(layout=>[2,2,'columnsfirst']); | ||
| 312 | |||
| 313 | $w->plot({title=>"M51 in 3-D (default view)"}, | ||
| 314 | with=>'pm3d',xvals($m51s), yvals($m51s), $m51s, $m51s ); | ||
| 315 | $w->plot({title=>"M51 in 3-D (ortho view)", view=>'equal xy'}, | ||
| 316 | with=>'pm3d',xvals($m51s), yvals($m51s), $m51s, $m51s ); | ||
| 317 | |||
| 318 | $w->plot({title=>"M51 in 3-D (near-vertical view)", view=>[ 0, 80, 'equal xy' ]}, | ||
| 319 | with=>'pm3d',xvals($m51s), yvals($m51s), $m51s, $m51s ); | ||
| 320 | $w->plot({title=>"M51 in 3-D (nearly along X axis)", view=>[ 85, 5 ]}, | ||
| 321 | with=>'pm3d',xvals($m51s), yvals($m51s), $m51s, $m51s ); | ||
| 322 | |||
| 323 | $w->end_multi; | ||
| 324 | |||
| 325 | |; | ||
| 326 | $s =~ s/\%\%m51\%\%/$m51path/; | ||
| 327 | act $s; | ||
| 328 | } | ||
| 329 | 1; | ||
| 330 | |||
| 331 | |||
| 332 |