| 1 | package PDL::Demos::Gnuplot; | ||
| 2 | use File::Spec; | ||
| 3 | |||
| 4 | my $subst = ""; | ||
| 5 | if($PDL::Graphics::Gnuplot::valid_terms->{wxt}) { | ||
| 6 | $subst = "wxt, size=>[8,6,'in'], title=>'Gnuplot demo window', persist=>0"; | ||
| 7 | } elsif($ENV{DISPLAY}) { | ||
| 8 | $subst = "x11, size=>[8,6,'in'], title=>'Gnuplot demo window', persist=>0"; | ||
| 9 | } | ||
| 10 | my @d = qw(PDL Demos); | ||
| 11 | our $m51path = undef; | ||
| 12 | foreach my $path ( @INC ) { | ||
| 13 | my $check = File::Spec->catfile( $path, @d, "m51.fits" ); | ||
| 14 | if ( -f $check ) { $m51path = $check; last; } | ||
| 15 | } | ||
| 16 | |||
| 17 | sub init {sprintf <<'EOF', $subst} | ||
| 18 | use PDL::ImageND; | ||
| 19 | my $s = q| | ||
| 20 | # ensure that the module is loaded | ||
| 21 | use PDL::Graphics::Gnuplot; | ||
| 22 | # Create a Gnuplot object - the default device displays on most | ||
| 23 | # operating systems. (No plot window yet - just the object). | ||
| 24 | $w = gpwin(%s); | ||
| 25 | # Create variables to plot | ||
| 26 | $x = xvals(1000); | ||
| 27 | $y = $x/1000 * sin($x/10); | ||
| 28 | |; | ||
| 29 | eval $s; | ||
| 30 | EOF | ||
| 31 | sub info {('gnuplot', 'Gnuplot graphics')} | ||
| 32 | |||
| 33 | my @demo = ( | ||
| 34 | [act => sprintf <<'EOF'], | ||
| 35 | # ensure that the module is loaded | ||
| 36 | use PDL::Graphics::Gnuplot; | ||
| 37 | |||
| 38 | # Create a Gnuplot object - the default device displays on most | ||
| 39 | # operating systems. (No plot window yet - just the object). | ||
| 40 | $w = gpwin(%s); | ||
| 41 | |||
| 42 | # Create variables to plot | ||
| 43 | $x = xvals(1000)/1000; | ||
| 44 | $y = $x * sin(100 * $x); | ||
| 45 | |||
| 46 | # Generate a line plot. Plot parameters in front. | ||
| 47 | $w->lines({title=>" x * sin(100 x) ",xl=>"Ordinate",yl=>"Abscissa"}, $x, $y ); | ||
| 48 | EOF | ||
| 49 | |||
| 50 | [act => q| | ||
| 51 | |||
| 52 | # You can set persistent plot parameters with "options". | ||
| 53 | $w->options(title=>"Two lines", xl=>"Ordinate", yl=>"Abscissa"); | ||
| 54 | |||
| 55 | # A two-line plot | ||
| 56 | $y2 = sqrt($x) * cos(100*$x); | ||
| 57 | $w->lines($x,$y,{},$x,$y2); | ||
| 58 | |||
| 59 | |], | ||
| 60 | |||
| 61 | [act => q| | ||
| 62 | # You can set persistent plot parameters with "options". | ||
| 63 | $w->options(title=>"Two lines", xl=>"Ordinate", yl=>"Abscissa"); | ||
| 64 | |||
| 65 | # A two-line plot. | ||
| 66 | # Groups of data are separated by non-PDL options -- in this | ||
| 67 | # case, the null hash since there are no per-curve options. | ||
| 68 | |||
| 69 | $y2 = sqrt($x) * cos(100*$x); | ||
| 70 | $w->lines($x,$y,{},$x,$y2); | ||
| 71 | |||
| 72 | # A two-line plot with legend. | ||
| 73 | # The legend for each line separates the groups of PDL data. | ||
| 74 | |||
| 75 | $w->options(title=>"Two lines (with legend)", key=>'left'); | ||
| 76 | $w->lines( legend=>"sin",$x,$y, legend=>"cos",$x,$y2 ); | ||
| 77 | |||
| 78 | # | ||
| 79 | |], | ||
| 80 | |||
| 81 | [act => q| | ||
| 82 | # You can also generate multiline plots with broadcasting. | ||
| 83 | # Here, $x is a 1000-element 1-D PDL, and $yy is a 1000x2 2-D PDL. | ||
| 84 | |||
| 85 | $x = xvals(1000)/1000; | ||
| 86 | ($y,$y2) = ( $x * sin(100 * $x), sqrt($x) * cos(100 * $x) ); | ||
| 87 | $yy = pdl( $y, $y2 ); | ||
| 88 | |||
| 89 | # all options can be abbreviated to the smallest unique string. | ||
| 90 | # Here, "tit" stands for "title", and "le" for "legend". | ||
| 91 | |||
| 92 | $w->lines({tit=>"2-lines broadcasted"}, le=>["sin", "cos"], $x, $yy); | ||
| 93 | |], | ||
| 94 | |||
| 95 | [act => q| | ||
| 96 | # line plots are far from the only thing you can do! | ||
| 97 | |||
| 98 | # Here is the same plot, with points and some tweaks to the axis labels. | ||
| 99 | $w->options(tit=>"2 sets of points"); | ||
| 100 | |||
| 101 | $l = ["sin", "cos"]; | ||
| 102 | $w->points({xtics=>{rotate=>45,offset=>[0,-1.5],font=>',14'}, | ||
| 103 | xlab=>"Tilted Labels in 14-point text" | ||
| 104 | }, | ||
| 105 | le=>$l, $x, $yy); | ||
| 106 | |], | ||
| 107 | |||
| 108 | [act => q| | ||
| 109 | # Many types of plot are supported, using the "plot" command | ||
| 110 | # and the "with" curve option. Here, we can mix and match points and lines. | ||
| 111 | |||
| 112 | # You can also set plot options *temporarily* with a hash ref at the start of the | ||
| 113 | # plot call: | ||
| 114 | |||
| 115 | $w->plot( {title=>"Points and lines mixed"}, | ||
| 116 | with=>'points', le=>'sin (points)', $x, $y, | ||
| 117 | with=>'lines', le=>'cos (line)', $x, $y2); | ||
| 118 | |], | ||
| 119 | |||
| 120 | [act => q| | ||
| 121 | # Many types of plot are supported, using the "plot" command | ||
| 122 | # and the "with" curve option. Here, we can mix and match points and lines. | ||
| 123 | |||
| 124 | $w->plot( with=>'points', le=>'sin (points)', $x, $y, | ||
| 125 | with=>'lines', le=>'cos (line)', $x, $y2); | ||
| 126 | |||
| 127 | $x = xvals(51)/50; | ||
| 128 | ($y,$y2) = ( $x * sin(20 * $x), sqrt($x) * cos(20 * $x) ); | ||
| 129 | $radii = 0.01 * (0.25 - ($x*2 - 0.5)**2); | ||
| 130 | |||
| 131 | # Here are some other options. | ||
| 132 | $w->plot( {title=>"Circles and Steps"}, | ||
| 133 | le=>"Circles", with=>'circles', $x, $y, $radii, | ||
| 134 | le=>"Steps", with=> 'steps', $x, $y2 | ||
| 135 | ); | ||
| 136 | |], | ||
| 137 | |||
| 138 | [act => q| | ||
| 139 | # You can plot multiple plots on one pane with "multiplot". | ||
| 140 | |||
| 141 | $x = xvals(51)/50; | ||
| 142 | ($y,$y2) = ( $x * sin(20 * $x), sqrt($x) * cos(20 * $x) ); | ||
| 143 | |||
| 144 | |||
| 145 | $w->multiplot(layout=>[1,2]); | ||
| 146 | |||
| 147 | $w->plot( {title=>"Impulses"}, with=> 'impulses', $x, $y ); | ||
| 148 | $w->plot( {title=>"Filled Curves"}, with => "filledcurves", $x, $y, $y2 ); | ||
| 149 | |||
| 150 | $w->end_multi(); | ||
| 151 | |], | ||
| 152 | |||
| 153 | [act => q| | ||
| 154 | # 2-D data... | ||
| 155 | |||
| 156 | $rv = rvals(51,51)/2; | ||
| 157 | $im = 25 * cos($rv) / ($rv+1.5); | ||
| 158 | |||
| 159 | $w->multiplot(layout=>[2,2]); | ||
| 160 | $w->options(justify=>1); # set nice aspect ratio | ||
| 161 | $w->plot( {tit=>'Default color map'}, with=>'image', $im ); | ||
| 162 | $w->plot( {tit=>'Grayscale', clut=>'gray'}, with=>'image', $im ); | ||
| 163 | $w->plot( {tit=>'heat map', clut=>'heat1'}, with=>'image', $im ); | ||
| 164 | $w->plot( {tit=>'3d perspective', trid=>1}, with=>'pm3d', $im ); | ||
| 165 | $w->end_multi; | ||
| 166 | |||
| 167 | |], | ||
| 168 | |||
| 169 | [act => q| | ||
| 170 | # You can indpendently specify color and position on surface plots, | ||
| 171 | # and can overlay multiple types of plot -- even in 3D. | ||
| 172 | # | ||
| 173 | $rv = rvals(101,101)/5; $im = cos($rv)/($rv+2.5); | ||
| 174 | $grad = sumover $im->range([[-1,0],[1,0]],[$im->dims],'e') * pdl(-1,1); | ||
| 175 | |||
| 176 | $im2 = $im->indexND(ndcoords(26,26)*4); # subsample $im | ||
| 177 | |||
| 178 | $w->reset; | ||
| 179 | $w->options( trid=>1, hidden=>'front', colorbox=>0, clut=>'heat1' ); | ||
| 180 | |||
| 181 | $w->multiplot(layout=>[2,2]); | ||
| 182 | |||
| 183 | $w->plot( {title=>"A colormap-shaded 3-d surface plot"}, with=>'pm3d', $im ); | ||
| 184 | |||
| 185 | $w->plot( {title=>"Perspective 3-d surface plot"}, | ||
| 186 | with=>'pm3d', xvals($im), yvals($im), $im, $grad ); | ||
| 187 | |||
| 188 | $w->plot( {title=>"Perspective grid plot"}, | ||
| 189 | with=>'lines', xvals($im2)*4, yvals($im2)*4, $im2 ); | ||
| 190 | |||
| 191 | $w->plot( {title=>"Combined"}, | ||
| 192 | with=>'pm3d', xvals($im),yvals($im), $im, $grad, | ||
| 193 | with=>'lines', xvals($im2)*4, yvals($im2)*4, $im2 ); | ||
| 194 | |||
| 195 | $w->end_multi; | ||
| 196 | |], | ||
| 197 | |||
| 198 | !(defined($m51path) && -e $m51path) ? | ||
| 199 | [comment => q| | ||
| 200 | ****************************************************** | ||
| 201 | You seem to be missing the file 'm51.fits', which | ||
| 202 | should be included in the PDL distribution. Without | ||
| 203 | it, I can't show you the m51 image demos, so I'll | ||
| 204 | quit now. | ||
| 205 | ****************************************************** | ||
| 206 | |] : ( | ||
| 207 | |||
| 208 | [act => q| | ||
| 209 | # Images ... | ||
| 210 | |||
| 211 | $m51 = rfits $|.__PACKAGE__.q|::m51path; | ||
| 212 | |||
| 213 | $w->reset; | ||
| 214 | # note this uses the 'fits' plot type, which adjusts the coordinates | ||
| 215 | $w->plot({j=>1, clut=>'gray', title=>"M51 galaxy"}, with=>'fits',$m51 ); | ||
| 216 | |], | ||
| 217 | |||
| 218 | [act => q| | ||
| 219 | $m51 = rfits($|.__PACKAGE__.q|::m51path)->slice('0:-1:4,0:-1:4'); | ||
| 220 | |||
| 221 | $m51s = $m51->convolveND(ones(11,11)/11**2); | ||
| 222 | |||
| 223 | $w->options(clut=>'heat2', trid=>1); | ||
| 224 | |||
| 225 | # 3-D display of M51: various angles (note "columnsfirst" in multiplot) | ||
| 226 | |||
| 227 | $w->multiplot(layout=>[2,2,'columnsfirst']); | ||
| 228 | |||
| 229 | $w->plot({title=>"M51 in 3-D (default view)"}, | ||
| 230 | with=>'pm3d',xvals($m51s), yvals($m51s), $m51s, $m51s ); | ||
| 231 | $w->plot({title=>"M51 in 3-D (ortho view)", view=>'equal xy'}, | ||
| 232 | with=>'pm3d',xvals($m51s), yvals($m51s), $m51s, $m51s ); | ||
| 233 | |||
| 234 | $w->plot({title=>"M51 in 3-D (near-vertical view)", view=>[ 0, 80, 'equal xy' ]}, | ||
| 235 | with=>'pm3d',xvals($m51s), yvals($m51s), $m51s, $m51s ); | ||
| 236 | $w->plot({title=>"M51 in 3-D (nearly along X axis)", view=>[ 85, 5 ]}, | ||
| 237 | with=>'pm3d',xvals($m51s), yvals($m51s), $m51s, $m51s ); | ||
| 238 | |||
| 239 | $w->end_multi; | ||
| 240 | |], | ||
| 241 | )); | ||
| 242 | |||
| 243 | sub demo { @demo } | ||
| 244 | |||
| 245 | sub done {'$w->close;'} | ||
| 246 | |||
| 247 | 1; | ||
| 248 | |||
| 249 | =head1 NAME | ||
| 250 | |||
| 251 | PDL::Demos::Gnuplot - demonstrate PDL::Graphics::Gnuplot capabilities | ||
| 252 | |||
| 253 | =head1 SYNOPSIS | ||
| 254 | |||
| 255 | pdl> demo gnuplot | ||
| 256 | |||
| 257 | =cut |