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