| 1 | package PDL::Demos::PGPLOT; |
| 2 | use PDL::Graphics::PGPLOT; |
| 3 | |
| 4 | sub info {('pgplot', 'PGPLOT graphics output')} |
| 5 | |
| 6 | sub init {' |
| 7 | $ENV{PGPLOT_XW_WIDTH}=0.3; |
| 8 | $ENV{PGPLOT_DEV}=$^O =~ /MSWin32/ ? "/GW" : "/XW"; |
| 9 | use PDL::Graphics::PGPLOT; |
| 10 | '} |
| 11 | my @demo = ( |
| 12 | [comment => q| |
| 13 | Welcome to this tour of the PDL's PGPLOT interface. |
| 14 | |
| 15 | This tour will introduce the PDL's PGPLOT plotting module and show |
| 16 | what this powerful package can provide in terms of plotting. It is |
| 17 | not designed to give a full tour of PGPLOT, you are advised to see |
| 18 | the routines provided with pgperl for that. |
| 19 | |
| 20 | The PDL::Graphics::PGPLOT module provides a high-level interface |
| 21 | to PGPLOT. However if you want even better control of your plots |
| 22 | you might want to include the PGPLOT module specifically: |
| 23 | |
| 24 | use PGPLOT; |
| 25 | |
| 26 | One aspect of PGPLOT that requires mention is the use of devices: |
| 27 | Normally PGPLOT will inquire you about what device you want to use, |
| 28 | with the prompt: |
| 29 | |
| 30 | Graphics device/type (? to see list, default /NULL): |
| 31 | |], |
| 32 | |
| 33 | [act => q| |
| 34 | # ensure the module is loaded |
| 35 | use PDL::Graphics::PGPLOT; |
| 36 | # The size of the window can be specified |
| 37 | $ENV{PGPLOT_XW_WIDTH}=0.3; |
| 38 | # You can set your device explicitly |
| 39 | $id=dev($ENV{PGPLOT_DEV}); # '/XW' on X, '/GW' on Win32 |
| 40 | |], |
| 41 | |
| 42 | [act => q| |
| 43 | # First we define some variables to use for the rest of the demo. |
| 44 | $x=sequence(10); |
| 45 | $y=2*$x**2; |
| 46 | |
| 47 | # Now a simple plot with points |
| 48 | points $x, $y; |
| 49 | |], |
| 50 | |
| 51 | [act => q| |
| 52 | # Here is the same with lines |
| 53 | line $x, $y; |
| 54 | |], |
| 55 | |
| 56 | [act => q| |
| 57 | # If you want to overlay one plot you can use the command |
| 58 | # 'hold' to put the graphics on hold and 'release' to |
| 59 | # revert the effect |
| 60 | |
| 61 | points $x, $y, {SYMBOL=>4}; # The last argument sets symboltype |
| 62 | hold; |
| 63 | # Now draw lines between the points |
| 64 | line $x, $y; |
| 65 | # Plot errorbars over the points |
| 66 | $yerr=sqrt($y); |
| 67 | errb $x, $y, $yerr; |
| 68 | |
| 69 | # To revert to old behaviour, use release |
| 70 | release; |
| 71 | |], |
| 72 | |
| 73 | [act => q| |
| 74 | bin $x, $y; |
| 75 | |
| 76 | # This plots a binned histogram of the data and as you can |
| 77 | # see it made a new plot. |
| 78 | |], |
| 79 | |
| 80 | [act => q| |
| 81 | # 2D data can also easily be accommodated: |
| 82 | |
| 83 | # First make a simple image |
| 84 | $gradient=sequence(40,40); |
| 85 | |
| 86 | # Then display it. |
| 87 | imag $gradient; |
| 88 | |
| 89 | # And overlay a contour plot over it: |
| 90 | hold; |
| 91 | cont $gradient; |
| 92 | release; |
| 93 | |], |
| 94 | |
| 95 | [act => q| |
| 96 | # PDL::Graphics::PGPLOT contains several colour tables, |
| 97 | # a more extensive collection can be found in |
| 98 | # PDL::Graphics::LUT |
| 99 | # |
| 100 | # (note: the call to lut_names() can take a few seconds to execute) |
| 101 | # |
| 102 | use PDL::Graphics::LUT; |
| 103 | @names = lut_names(); |
| 104 | print "Available tables: [ ", @names, " ]\n"; |
| 105 | |
| 106 | # use the first table |
| 107 | ctab( lut_data($names[0]) ); |
| 108 | use PGPLOT; |
| 109 | pglabel "", "", "Colour table: $names[0]"; |
| 110 | |], |
| 111 | |
| 112 | [act => q| |
| 113 | # To change plot specifics you can either use the specific PGPLOT |
| 114 | # commands - recommended if you need lots of control over your |
| 115 | # plot. |
| 116 | # |
| 117 | # Or you can use the new option specifications: |
| 118 | |
| 119 | # To plot our first graph again with blue color, dashed line |
| 120 | # and a thickness of 10 we can do: |
| 121 | |
| 122 | line $x, $y, {COLOR=>5, LINESTYLE=>'dashed', LINEWIDTH=>10}; |
| 123 | |], |
| 124 | |
| 125 | [act => q| |
| 126 | |
| 127 | # Now for a more complicated example. |
| 128 | # First create some data |
| 129 | $x=sequence(360)*3.1415/180.; |
| 130 | $y=sin($x)*transpose(cos($x)); |
| 131 | |
| 132 | # Make an ndarray with the wanted contours |
| 133 | $contours=pdl [0.1,0.5,1.0]; |
| 134 | # And an array (reference to an array) with labels |
| 135 | $labels=['A', 'B', 'C']; |
| 136 | # Create a contour map of the data - note that we can set the colour of |
| 137 | # the labels. |
| 138 | cont($y, {CONTOURS=>$contours, linest=>'DASHED', |
| 139 | LINEWIDTH=>3, COLOR=>2, LABELCOL=>4}); |
| 140 | hold; |
| 141 | |
| 142 | pgqlw($linewidth); |
| 143 | |
| 144 | points $x->slice('0:-1:4')*180./3.1415; |
| 145 | release; |
| 146 | |], |
| 147 | |
| 148 | [act => q| |
| 149 | # |
| 150 | # More examples of changing the plot defaults |
| 151 | # |
| 152 | $x = 1+sequence(10); |
| 153 | $y = $x*2; |
| 154 | $bord_opt = { TYPE => 'RELATIVE', VALUE => 0.1 }; |
| 155 | line log10($x), $y, { AXIS => 'LOGX', BORDER => $bord_opt }; |
| 156 | |], |
| 157 | |
| 158 | [act => q| |
| 159 | # |
| 160 | # We can also create vector maps of data |
| 161 | # This requires one array for the horizontal component and |
| 162 | # one for the vertical component |
| 163 | # |
| 164 | $horizontal=sequence(10,10); |
| 165 | $vertical=transpose($horizontal)+random(10,10)*$horizontal/10.; |
| 166 | |
| 167 | $arrow={ARROW=> {FS=>1, ANGLE=>25, VENT=>0.7, SIZE=>3}}; |
| 168 | vect $horizontal, $vertical, {ARROW=>$arrow, COLOR=>RED}; |
| 169 | hold; |
| 170 | cont $vertical-$horizontal, {COLOR=>YELLOW}; |
| 171 | release; |
| 172 | |], |
| 173 | |
| 174 | [act => q| |
| 175 | # |
| 176 | # To draw [filled] polygons, the command poly is handy: |
| 177 | # |
| 178 | |
| 179 | $x=sequence(10)/5; |
| 180 | poly $x, $x**2, {FILL=>HATCHED, COLOR=>BLUE}; |
| 181 | |], |
| 182 | |
| 183 | [act => q| |
| 184 | # |
| 185 | # the latest feature of PDL are complex numbers |
| 186 | # so let's play with a simple example |
| 187 | # |
| 188 | |
| 189 | |
| 190 | $z50 = zeroes(50); |
| 191 | $c = czip($z50->xlinvals(0,7), $z50->xlinvals(2,4)); |
| 192 | line sin($c)->im; hold; # the imaginary part |
| 193 | line sin($c)->re; # real |
| 194 | line abs sin $c; release; # and the modulus |
| 195 | |], |
| 196 | |
| 197 | [act => q| |
| 198 | # |
| 199 | # more complex numbers |
| 200 | # |
| 201 | |
| 202 | $c = czip(zeroes(300)->xlinvals(0,12), zeroes(300)->xlinvals(2,10)); |
| 203 | $sin = sin $c; |
| 204 | line $sin->im, $sin->re; # look at the result in the complex plane |
| 205 | |], |
| 206 | |
| 207 | [act => q| |
| 208 | #close the window--we're done! |
| 209 | close_window($id); |
| 210 | print "On X Windows, you need to close the 'PGPLOT Server' window.\n"; |
| 211 | |], |
| 212 | ); |
| 213 | |
| 214 | sub demo { @demo } |
| 215 | |
| 216 | 1; |
| 217 | |
| 218 | =head1 NAME |
| 219 | |
| 220 | PDL::Demos::PGPLOT - demonstrate PDL::Graphics::PGPLOT capabilities |
| 221 | |
| 222 | =head1 SYNOPSIS |
| 223 | |
| 224 | pdl> demo pgplot |
| 225 | |
| 226 | =cut |