afalsename has asked for the wisdom of the Perl Monks concerning the following question:

Oh great a wise perl monks please aid a young student in the ways of perl wisdom.

Anyway, what I want to do is export (output) a pgplot to something like an EPS, PDF or Jpeg. If anyone could tell me how to do this with my code, that would be wonderful. Or even better would be to send a link where I could learn. However please realize that I am a complete noob and perl programing and thus far the websites I've found have been unintelligible to me.

Relavent code:

srand; use PDL; use PDL::Graphics::PGPLOT; use PGPLOT; use Exporter; $id=dev($^O =~ /MSWin32/ ? '/GW' : '/XSERVE'); $ENV{PGPLOT_XW_WIDTH}=0.5;
...
hold(bin($distributionx, $distributiony)); hold(line($distributionx,$predictiony,{COLOR=>RED})); line($distributionx,1000*$diffy,{COLOR=>purple}); #this is what I'd like to export, #all in one graph

Replies are listed 'Best First'.
Re: Export Plots
by Corion (Patriarch) on Sep 23, 2010 at 15:16 UTC
Re: Export Plots
by syphilis (Archbishop) on Sep 23, 2010 at 23:30 UTC
    First thing to do is to check which drivers you have at your disposal:
    # From PGPLOT-2.20 test suite use PGPLOT; pgqndt($ndrivers); print "Testing pgqdt() - $ndrivers drivers found...\n"; for $n (1..$ndrivers) { pgqdt($n,$type,$tlen,$descr,$dlen,$inter); print "$n: $type $tlen $descr $dlen $inter\n"; }
    For some examples, give the PGPLOT test suite a run, and take a look at the code (in test1.p, test2.p, ..., test12.p) that creates the 12 images.
    Just download the source, extract it some location, cd to the PGPLOT-2.20 directory, and run perl test.pl

    There's also a test script in the PDL source distro (t/pgplot.t) that you could take a look at. It uses m51.fits (also in the source distro) to create some images. You'll need to set the DISPLAY environment variable to a true value (eg 1) to run that script. If you're on Windows, you'll probably want to set the PGPLOT_DEV environment variable to /PNG (assuming you're happy enough to have the graphs/images written to png files).

    I don't know if GD is a viable alternative to PGPLOT for you. You might find the coding a little simpler with GD, and it's certainly easier to get help with GD (as it's much more widely used than PGPLOT).

    Cheers,
    Rob
Re: Export Plots
by ambrus (Abbot) on Sep 24, 2010 at 13:01 UTC

    See Plot a spiral with gnuplot.

    To export the plot in a pdf, try gnuplot commands like this before the plot:

    set terminal postscript set output "foobar.ps"
    Note the following. If you try this interactively and want to redraw the plot with changed parameters, you might have to issue the set output statement again so that gnuplot reopens the output file, otherwise gnuplot might just append the second plot to the file and you'll get two pages. Note also that if your input contains lots of data points then the postscript file will contain data for all of them, so you might get a very large file that's slow to render even if not much will be visible of it when you eventually view it. To avoid this, either reduce the number of your data points, or output to a raster format (such as PNG) with gnuplot instead, or render the postscript to a raster format with a postscript engine (such as ghostscript) and distribute that image.

    Update 2011-10-19: for a code example, see Re: Draw chart.