| 1 | package PDL::Demos::PGPLOT_OO; |
| 2 | |
| 3 | # show how to use the new OO PGPLOT interface |
| 4 | |
| 5 | use PDL::Graphics::PGPLOT::Window; |
| 6 | require File::Spec; |
| 7 | use Carp; |
| 8 | |
| 9 | $ENV{PGPLOT_XW_WIDTH}=0.3; |
| 10 | $ENV{PGPLOT_DEV}=$^O =~ /MSWin32/ ? '/GW' : "/XW"; |
| 11 | |
| 12 | sub info {('pgplotOO', 'PGPLOT OO interface')} |
| 13 | sub init {' |
| 14 | use PDL::Graphics::PGPLOT::Window; |
| 15 | '} |
| 16 | |
| 17 | # try and find m51.fits |
| 18 | my @f = qw(PDL Demos m51.fits); |
| 19 | our $m51file = undef; |
| 20 | foreach my $path ( @INC ) { |
| 21 | my $file = File::Spec->catfile( $path, @f ); |
| 22 | if ( -f $file ) { $m51file = $file; last; } |
| 23 | } |
| 24 | confess "Unable to find m51.fits within the perl libraries.\n" |
| 25 | unless defined $m51file; |
| 26 | |
| 27 | my @demo = ( |
| 28 | [comment => q| |
| 29 | The PGPLOT demo showed you how to use the old interface to PGPLOT. |
| 30 | As this is perl, TIMTOWTDI, and this demo shows you how to use the |
| 31 | new, object-orientated PGPLOT interface. For the simple examples |
| 32 | shown here, the new method appears overkill; however, it really |
| 33 | comes into its own when you wish to deal with multiple plots |
| 34 | or windows. |
| 35 | |
| 36 | Enough prattle, on with the show... |
| 37 | |], |
| 38 | |
| 39 | [act => q| |
| 40 | # we start with a different module to the traditional interface |
| 41 | use PDL::Graphics::PGPLOT::Window; |
| 42 | |
| 43 | # create a window "object" |
| 44 | $dev = $ENV{PGPLOT_DEV}; # '/XW' on X, '/GW' on Win32 |
| 45 | $win = PDL::Graphics::PGPLOT::Window->new( { Dev => $dev } ); |
| 46 | |], |
| 47 | |
| 48 | [act => q| |
| 49 | # First we define some variables to use for the rest of the demo. |
| 50 | $x=sequence(10); |
| 51 | $y=2*$x**2; |
| 52 | |
| 53 | # Now a simple plot with points |
| 54 | $win->points( $x, $y ); |
| 55 | |], |
| 56 | |
| 57 | [act => q| |
| 58 | # Here is the same with lines |
| 59 | $win->line( $x, $y ); |
| 60 | |
| 61 | # if you're beginning to think its the same as the old calls, |
| 62 | # just with "$win->" at the beginning then you're not far wrong! |
| 63 | |], |
| 64 | |
| 65 | [act => q| |
| 66 | # A ::Window-only feature: gaps in lines with MISSING option |
| 67 | # (the value is for the $y): |
| 68 | $win->line( $x, $y, {MISSING => 8} ); |
| 69 | |], |
| 70 | |
| 71 | [act => q| |
| 72 | # You can do all the things you did before ... |
| 73 | |
| 74 | $win->points( $x, $y, {Symbol=>4} ); |
| 75 | $win->hold; |
| 76 | $win->line( $x, $y ); |
| 77 | $yerr=sqrt($y); |
| 78 | $win->errb( $x, $y, $yerr ); |
| 79 | |
| 80 | $win->release; |
| 81 | |], |
| 82 | |
| 83 | [act => q| |
| 84 | # and it acts the same way |
| 85 | |
| 86 | $gradient=sequence(40,40); |
| 87 | $win->imag( $gradient ); |
| 88 | $win->hold; |
| 89 | $win->cont( $gradient ); |
| 90 | $win->release; |
| 91 | |
| 92 | # add labels to the plot |
| 93 | $win->label_axes( "An axis", "Another axis", "Title" ); |
| 94 | |], |
| 95 | |
| 96 | [act => q| |
| 97 | # let's try and read the cursor |
| 98 | |
| 99 | $c = czip(zeroes(300)->xlinvals(0,12), zeroes(300)->xlinvals(2,10)); |
| 100 | $sin = sin $c; |
| 101 | $win->line( $sin->im, $sin->re ); |
| 102 | |
| 103 | print "Select a point in the graph (mouse button or key press):\n"; |
| 104 | ( $x, $y, $ch ) = $win->cursor( { Type=>'CrossHair' } ); |
| 105 | |
| 106 | print "\nYou selected: $x + $y i (key = $ch)\n"; |
| 107 | |], |
| 108 | |
| 109 | # should really do something related to the selected points... |
| 110 | |
| 111 | [act => q| |
| 112 | # how about another window? |
| 113 | |
| 114 | $win2 = PDL::Graphics::PGPLOT::Window->new( { Dev => $dev } ); |
| 115 | $win2->env( 0, 4, -2, 0, { Axis => 'logy' } ); |
| 116 | $x = sequence(101) / 25; |
| 117 | $win2->points( $x, $x->sin->abs()->log10 ); |
| 118 | |], |
| 119 | |
| 120 | [act => q| |
| 121 | # switch back to the original window (we don't want to hurt |
| 122 | # its feelings) |
| 123 | |
| 124 | $win->line( $x, { Border => 1 } ); |
| 125 | |], |
| 126 | |
| 127 | [act => q| |
| 128 | # Read in an image ($m51file has been set up by this demo to |
| 129 | # contain the location of the file). |
| 130 | $m51 = rfits($|.__PACKAGE__.q|::m51file); |
| 131 | $win3 = PDL::Graphics::PGPLOT::Window->new(Dev => $dev, Size=> [6,4], |
| 132 | NX=>2, NY=>2, Ch=>2.5, HardCH=>2.5); |
| 133 | $win3->imag($m51,{Title=>"\$win3->imag(\$m51);"} ); |
| 134 | $win3->fits_imag($m51,{Title=>"\$win3->fits_imag(\$m51);"}); |
| 135 | $win3->imag($m51,{J=>1,Title=>"\$win3->imag(\$m51,{J=>1});"}); |
| 136 | $win3->fits_imag($m51,{J=>1,Title=>"\$win3->fits_imag(\$m51,{J=>1});"}); |
| 137 | |
| 138 | # You should see a 6 inch (153 mm) x 4 inch (102 mm) X window with four |
| 139 | # plots in it. All four images should have tick marks on the outside of |
| 140 | # the axes. |
| 141 | |
| 142 | # [ Scaled image of m51; scale [Scaled image of m51 with scale from |
| 143 | # in pixels on both axes ] X=[-1.8, 2.0],Y=[-1.9, 1.9] arcmin, |
| 144 | # with cal. wedge, centered in rect. frame] |
| 145 | |
| 146 | # [ Square image of m51; scale [Square image of m51 with scale as above, |
| 147 | # in pixels on both axes; ``shrink-wrapped''] |
| 148 | # ``shrinkwrapped'' ] |
| 149 | |], |
| 150 | |
| 151 | [act => q| |
| 152 | # free up the windows, after finding their names |
| 153 | |
| 154 | print "You've been watching ", $win->name, ", ", $win2->name, "\n"; |
| 155 | print " and ", $win3->name, "\n"; |
| 156 | |
| 157 | $win->close; undef $win; |
| 158 | $win2->close; undef $win2; |
| 159 | $win3->release; $win3->close; undef $win3; |
| 160 | |
| 161 | print "On X Windows, you need to close the 'PGPLOT Server' window.\n"; |
| 162 | |], |
| 163 | ); |
| 164 | |
| 165 | sub demo { @demo } |
| 166 | |
| 167 | 1; |
| 168 | |
| 169 | =head1 NAME |
| 170 | |
| 171 | PDL::Demos::PGPLOT_OO - demonstrate PDL::Graphics::PGPLOT OO capabilities |
| 172 | |
| 173 | =head1 SYNOPSIS |
| 174 | |
| 175 | pdl> demo pgplotOO |
| 176 | |
| 177 | =cut |