Resources

Saving multiple graphs as pdfs

0

Hi,

I am creating several graphs at once, and want to save them all under automatic file names. I have code like:

title(myTitle[i] );

where i is a string vector containing the names I want on the graphs as they are created. This works fine, but

plotsave(mytitle[i].pdf, dim);

does not… is there an easy way to do this? Basically, I want to be able to automate my file so if I change i once at the start, I get the program to run through a whole set of i specific tasks including saving a graph.

 

Thanks

Tags: asked February 17, 2014

4 Answers

0

The first input to the plotSave command is a string which specifies the name of the file you would like to create. Let’s try a couple of scenarios. The first is that you have a string array named myTitles. For each iteration of a loop you would like to save a new graph with the same name as the i'th of myTitles. Here is a basic example:

//some simple data for fully
//functioning example
x = seqa(0.01, 0.01, 30);
y = cos(x);

//String array with names of files
string myTitles = { "myfirstplot.pdf", "mysecondplot.pdf", "mythirdplot.pdf" };
dims_cm = { 30, 18 };

for i(1, rows(myTitles), 1);
   //draw graph
   plotXY(x, y);

   //index out each filename
   plotSave(myTitles[i], dims_cm);
endfor; 

The code above will create three identical XY plots and save one with the names myfirstplot.pdf, mysecondplot.pdf and mythirdplot.pdf.

Next we will show the case in which we want to save a series of graphs which all have the same name, but a different number at the end. We will use the string combine operator, $+ for this:

//some simple data for fully
//functioning example
x = rndn(30, 1);
y = rndn(30, 1);

//String array with names of files
mytitle = "myscatter";
dims_cm = { 30, 18 };

for i(1, rows(myTitles), 1);
   //draw graph
   plotScatter(x, y);

   //Combine "myscatter" with "i" as a string with ".pdf"
   fileName = mytitle$+ntos(i)$+".pdf";

   //index out each filename
   plotSave(fileName, dims_cm);
endfor; 

NOTE: ntos is a new convenience function in GAUSS 14. If you are using and older version, it will probably be simplest to use:

fileName = mytitle$+cvtos(ftocv(i, 1, 0))$+".pdf";

in place of the similar line that uses ntos in the main example.

aptech
342
0

Thanks, that worked. I think my problem was that I was using a combination of old graphing commands and new ones, and the plot would not save. My code was:

tt = seqa(1989,1,rows(bins));

library pgraph;
dim = { 30, 18 };
i=1;

let string myTitle = {"AustraliaF", "Canada", "France", "Germany","Hong Kong", "Indonesia","Italy", "Japan", "Malaysia","Netherlands", "Norway", "Singapore", "S Korea","Spain", "Sweden","Switzerland", "Thailand", "UK","USA"};
do while i le 19;
   _pcolor=15; @use white as color for all lines@
   _plegstr="\20250% Quantile 0033% Quantile 0066% Quantile ";
   _plwidth={2 2 2}; @width of lines, 0 is normal@
   _pltype={3 6 6};
   _plegctl=1;
   title(myTitle[i] );

   xy(tt,fmid2[.,i]~flowr2[.,i]~fuppr2[.,i]);
   plotsave(mytitle[i], dim);
   sleep(0.3);
   i=i+1;
endo;

 

This resulted in code that worked but no graphs being saved — is this because of the old commands like _pcolor? I am not familiar with new graphing commands (but I do have Gauss 14), so I am trying to format my graph the same way I had earlier…

 

 

0

To clarify, I get the graph, but then I get an error that says

 "unable to save file with extension"

Since my plotSave command appears to be the same as in your first example, I’m a bit confused..

0

There are two important distinctions between my example and yours. The first difference (and the reason you are getting the error) is that the elements of your string array do not have a file extension. You can fix that by using the GAUSS string combine operator inside of the plotSave line like this:

plotsave(mytitle[i]$+".pdf", dim);

The error that you are seeing is supposed to print out:

Unable to save file extension with: 

However, since you did not supply a file extension, the file extension GAUSS found is an empty string.

The second important difference is that the example I provided uses the new plotting functions (i.e. plotXY in this case rather than xy). You can save a file created by the deprecated PQG library with the function graphprt. Here is a line (or two) to replace the plotSave line in your code:

graphp_str = "-cf "$+mytitle[i]$+".eps"$+"-c=1" 
graphprt(graphp_str)

This will save the files as EPS files, which is the format that PDF uses internally.

aptech
342