| 1 | package PDL::Demos::PGPLOT_OO_demo; | ||
| 2 | |||
| 3 | # show how to use the new OO PGPLOT interface | ||
| 4 | |||
| 5 | use PDL; | ||
| 6 | use PDL::Graphics::PGPLOT::Window; | ||
| 7 | |||
| 8 | PDL::Demos::Routines->import(); | ||
| 9 | sub comment($); | ||
| 10 | sub act($); | ||
| 11 | sub output; | ||
| 12 | |||
| 13 | sub run { | ||
| 14 | |||
| 15 | $ENV{PGPLOT_XW_WIDTH}=0.3; | ||
| 16 | $ENV{PGPLOT_DEV}=$^O =~ /MSWin32/ ? '/GW' : "/XSERVE"; | ||
| 17 | |||
| 18 | comment q| | ||
| 19 | The PGPLOT demo showed you how to use the old interface to PGPLOT. | ||
| 20 | As this is perl, TIMTOWTDI, and this demo shows you how to use the | ||
| 21 | new, object-orientated PGPLOT interface. For the simple examples | ||
| 22 | shown here, the new method appears overkill; however, it really | ||
| 23 | comes into its own when you wish to deal with multiple plots | ||
| 24 | or windows. | ||
| 25 | |||
| 26 | Enough prattle, on with the show... | ||
| 27 | |||
| 28 | |; | ||
| 29 | |||
| 30 | act q| | ||
| 31 | # we start with a different module to the traditional interface | ||
| 32 | use PDL::Graphics::PGPLOT::Window; | ||
| 33 | |||
| 34 | # create a window "object" | ||
| 35 | $dev = $^O =~ /MSWin32/ ? '/GW' : '/XSERVE'; | ||
| 36 | $win = PDL::Graphics::PGPLOT::Window->new( { Dev => $dev } ); | ||
| 37 | |||
| 38 | |; | ||
| 39 | |||
| 40 | act q| | ||
| 41 | # First we define some variables to use for the rest of the demo. | ||
| 42 | $x=sequence(10); | ||
| 43 | $y=2*$x**2; | ||
| 44 | |||
| 45 | # Now a simple plot with points | ||
| 46 | $win->points( $x, $y ); | ||
| 47 | |||
| 48 | |; | ||
| 49 | |||
| 50 | act q| | ||
| 51 | # Here is the same with lines | ||
| 52 | $win->line( $x, $y ); | ||
| 53 | |||
| 54 | # if you're beginning to think its the same as the old calls, | ||
| 55 | # just with "$win->" at the beginning then you're not far wrong! | ||
| 56 | |||
| 57 | |; | ||
| 58 | |||
| 59 | act q| | ||
| 60 | # You can do all the things you did before ... | ||
| 61 | |||
| 62 | $win->points( $x, $y, {Symbol=>4} ); | ||
| 63 | $win->hold; | ||
| 64 | $win->line( $x, $y ); | ||
| 65 | $yerr=sqrt($y); | ||
| 66 | $win->errb( $x, $y, $yerr ); | ||
| 67 | |||
| 68 | $win->release; | ||
| 69 | |||
| 70 | |; | ||
| 71 | |||
| 72 | act q| | ||
| 73 | # and it acts the same way | ||
| 74 | |||
| 75 | $gradient=sequence(40,40); | ||
| 76 | $win->imag( $gradient ); | ||
| 77 | $win->hold; | ||
| 78 | $win->cont( $gradient ); | ||
| 79 | $win->release; | ||
| 80 | |||
| 81 | # add labels to the plot | ||
| 82 | $win->label_axes( "An axis", "Another axis", "Title" ); | ||
| 83 | |; | ||
| 84 | |||
| 85 | |||
| 86 | act q| | ||
| 87 | # let's try and read the cursor | ||
| 88 | |||
| 89 | use PDL::Complex; | ||
| 90 | $c = zeroes(300)->xlinvals(0,12)+i*zeroes(300)->xlinvals(2,10); | ||
| 91 | $sin = sin $c; | ||
| 92 | $win->line( $sin->im, $sin->re ); | ||
| 93 | |||
| 94 | print "Select a point in the graph (mouse button or key press):\n"; | ||
| 95 | ( $x, $y, $ch ) = $win->cursor( { Type=>'CrossHair' } ); | ||
| 96 | |||
| 97 | print "\nYou selected: $x + $y i (key = $ch)\n"; | ||
| 98 | |||
| 99 | |; | ||
| 100 | |||
| 101 | # should really do something related to the selected points... | ||
| 102 | |||
| 103 | act q| | ||
| 104 | # how about another window? | ||
| 105 | |||
| 106 | $win2 = PDL::Graphics::PGPLOT::Window->new( { Dev => $dev } ); | ||
| 107 | $win2->env( 0, 4, -2, 0, { Axis => 'logy' } ); | ||
| 108 | $x = sequence(101) / 25; | ||
| 109 | $win2->points( $x, $x->sin->abs()->log10 ); | ||
| 110 | |||
| 111 | |; | ||
| 112 | |||
| 113 | act q| | ||
| 114 | # switch back to the original window (we don't want to hurt | ||
| 115 | # its feelings) | ||
| 116 | |||
| 117 | $win->line( $x, { Border => 1 } ); | ||
| 118 | |||
| 119 | |; | ||
| 120 | |||
| 121 | act q| | ||
| 122 | # free up the windows, after finding their names | ||
| 123 | |||
| 124 | print "You've been watching " . $win->name(); | ||
| 125 | print " and " . $win2->name() . "\n"; | ||
| 126 | |||
| 127 | $win->close(); | ||
| 128 | $win2->close(); | ||
| 129 | |||
| 130 | |; | ||
| 131 | |||
| 132 | } | ||
| 133 | |||
| 134 | 1; |