Page 1 of 2

Export graphs directly to PNG, PDF, or other formats?

Posted: Tue Aug 11, 2015 4:35 pm
by macro
I have code that creates a large number of graphs with OPEN PLOT commands like this:

Code: Select all

open plot output/figures/gdp.rgf
    graph(key = upleft, header = "Log GDP", shading = recessions, $
    grid=t==2010:4, $
    subheader='') 1
    # gdp begin_samp end_samp
close plot
Is it possible to export these to (ideally) PDF format automatically, in code, instead of exporting them to the RATS proprietary graphics format or EPS and then running another program to convert them? PDF is my preferred format, but if RATS doesn't support that, I'd settle for a vector graphics format, or (ideally not) PNG, JPG, etc.

I'm thinking along the lines of the "graph export" command in Stata or the graphics commands in MATLAB.

Re: Export graphs directly to PNG, PDF, or other formats?

Posted: Tue Sep 01, 2015 3:10 pm
by macro
Is this possible? There doesn't seem to be a way to do it through the graphical interface in v9.0, and the EPS formats don't appear to support exporting color graphics (although that could be the version of Ghostscript I'm using to view them).

Re: Export graphs directly to PNG, PDF, or other formats?

Posted: Tue Sep 01, 2015 4:21 pm
by TomDoan
I usually use EPSTOPDF which works fine. But I also tested

gswin64c -q -dEPSCrop -sDEVICE=pdfwrite -o outputname inputname

and that seemed to get the colors as well.

Re: Export graphs directly to PNG, PDF, or other formats?

Posted: Mon Sep 21, 2015 1:23 pm
by TomDoan
The Mac OS supports PDF directly (you just "draw" to a PDF file). Windows doesn't. I'm not sure what Matlab does, but it seems to be common to simply rely upon shelling to GhostScript. I have a test version which seems to handle that.

Re: Export graphs directly to PNG, PDF, or other formats?

Posted: Mon Sep 21, 2015 1:28 pm
by macro
That makes sense. With MATLAB, I assume the program has a copy of Ghostscript embedded in it (since that's usually the solution, and they also have Java and Perl embedded in the Windows version). Which test version are you referring to?

Re: Export graphs directly to PNG, PDF, or other formats?

Posted: Thu Oct 01, 2015 9:17 am
by macro
I must be exporting my graphs incorrectly, because code like this

Code: Select all

open plot indicators.eps
    graph 2
    # gdp
    # cons
close plot
does produce a file named indicators.eps, but EPSTOPDF throws an error of "Invalid binary DOS header". It looks like this is because even when I specify a file extension, RATS is still exporting the graph in RGF format. If I change the extension to RGF outside of the code, RATS opens it correctly as an RGF file.

I can then manually export it to EPS, but then EPSTOPDF doesn't pick up the colors. What did I miss?

Is this something related to batch mode? Code like this

Code: Select all

environment gsave="indicators.eps" gformat=portrait
graph 2
# ffed
# fcm10
doesn't display any errors, but doesn't produce an output file anywhere on my system, as far as I can tell.

Re: Export graphs directly to PNG, PDF, or other formats?

Posted: Thu Oct 01, 2015 10:54 am
by TomDoan
open plot ... does only save in RGF

We would recommend the the newer GSAVE instruction. However, the ENV GSAVE seems to work, but you probably would be helped by putting an actual path on the file name.

Re: Export graphs directly to PNG, PDF, or other formats?

Posted: Thu Oct 01, 2015 11:52 am
by macro
I must be missing some nuance of the GSAVE command, because this code

Code: Select all

calendar(q) 1947 1
allocate 2020:4

compute begin_samp = 1969:1
compute end_samp = 2012:4

open data "data.csv"
data(format = cdf, org = columns) begin_samp end_samp $
    gdph cdh ffed lr
close data

gsave(format = portrait, patterns) "C:\Users\macro\Documents\output.eps"
graph 1
# ffed
doesn't display any errors in the output file, but it also doesn't produce the EPS file. I have permissions to write to the output directory, and as far as I can tell, I'm following the syntax for the GSAVE command. Is there still something wrong with this code?

Re: Export graphs directly to PNG, PDF, or other formats?

Posted: Thu Oct 01, 2015 1:17 pm
by TomDoan
It looks fine. Are you sure you can write to the directory and that the file isn't already open somewhere?

Re: Export graphs directly to PNG, PDF, or other formats?

Posted: Thu Oct 01, 2015 1:27 pm
by macro
I have full permissions to every directory I've tried to write to, and the file doesn't exist in any of them, so I don't think another program could have it open. If I run the code from within RATS, it displays the graph and doesn't throw an error, so it doesn't seem to be the code. Is there a default directory that RATS could be writing the files to? I haven't been able to find them on my system, but it seems strange that they're not being written even in the absence of any errors. Usually if I try to write to a file that's already open, RATS will crash or throw an error. Very strange.

Re: Export graphs directly to PNG, PDF, or other formats?

Posted: Thu Oct 01, 2015 1:39 pm
by macro
Here's another oddity. In WinRATS 8.30 Beta, this code

Code: Select all

calendar(q) 1947 1
allocate 2020:4

compute begin_samp = 1969:1
compute end_samp = 2012:4

open data "C:\working\rats\data.csv"
data(format = cdf, org = columns) begin_samp end_samp $
    gdph cdh ffed lr
close data

gsave(format = portrait) "C:\working\rats\output.eps"
graph 1
# ffed
successfully prints to "output.eps" , which EPSTOPDF converts to the attached PDF ("output83beta.pdf"). That graph doesn't look correct, although the graph that is displayed in RATS looks fine ("output83beta.rgf").

However, if I run the code in WinRATS 9.0, the graph still looks fine inside RATS, but no EPS file is produced. I deleted "output.eps" before running the code again in 9.0, so there shouldn't be any file system conflicts.

Re: Export graphs directly to PNG, PDF, or other formats?

Posted: Thu Oct 01, 2015 1:58 pm
by TomDoan
Are you using the version 9 that I sent you?

Re: Export graphs directly to PNG, PDF, or other formats?

Posted: Mon Oct 05, 2015 11:25 am
by macro
I was using the version 9.0 that we received when we upgraded our license. The code works and outputs EPS files successfully when I use the newer v9.0 that you sent me (the most recent Fixed Term version). The only problem I see is that the the labels on the horizontal axis overlap the graph in the EPS file, which doesn't occur in the RGF file:
eps_label_spacing.PNG
eps_label_spacing.PNG (2.46 KiB) Viewed 23612 times
This doesn't come from the code above, but from part of the larger project I'm working on.

Re: Export graphs directly to PNG, PDF, or other formats?

Posted: Mon Oct 05, 2015 2:20 pm
by macro
Here is a complete code example that replicates the spacing problem above:

Code: Select all

calendar(q) 1947 1

compute begin_graph = 2002:4
compute end_graph = 2012:04

open data "errors.csv"
data(format = cdf, org = columns) begin_graph end_graph $
    s1 s2 s3
close data

grparm footer 10
gsave(format = landscape) "bad_spacing.eps"

spgraph(vfield = 3, hfield = 1, header = "Model errors", subheader = "type model", footer = %dateandtime(), $
        ylabels = ||"1", "2", "3"||)
    do i = 1,3
        compute lbl = "s" + %string(i)
        graph(style = spike, min = -5, max = 5, row = i, col = 1) 1
        # %s(lbl) begin_graph end_graph
    end do i
spgraph(done)
This is just dummy data, but it illustrates the problem in that the date labels overlap the x-axis. This error occurs with the version 9 you sent me; it doesn't occur with the version 9 that came with our license, since that version doesn't produce an EPS file at all, as discussed above.

The forum wouldn't let me upload the EPS file, but I can send it to you if you need it.

Re: Export graphs directly to PNG, PDF, or other formats?

Posted: Wed Mar 30, 2016 3:16 am
by cczzwhy
Hello Tom!

I need you to help me exporting the graphs in eps format,I failed with the code

Code: Select all

environment gsave="c:\output.eps" gformat=portrait
,and also failed with clicking the graph to export in eps.

i don`t know how to solve this .I need to add the graphs in latex.

Can you help me?

Thanks in advance.