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

For questions and discussion related to graphs, reports, and other output, including issues related to presenting or publishing results.
macro
Posts: 70
Joined: Thu Jul 16, 2015 3:01 pm

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

Unread post 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.
macro
Posts: 70
Joined: Thu Jul 16, 2015 3:01 pm

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

Unread post 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).
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

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

Unread post 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.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

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

Unread post 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.
macro
Posts: 70
Joined: Thu Jul 16, 2015 3:01 pm

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

Unread post 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?
macro
Posts: 70
Joined: Thu Jul 16, 2015 3:01 pm

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

Unread post 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.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

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

Unread post 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.
macro
Posts: 70
Joined: Thu Jul 16, 2015 3:01 pm

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

Unread post 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?
Attachments
data.csv
(5.67 KiB) Downloaded 948 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

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

Unread post by TomDoan »

It looks fine. Are you sure you can write to the directory and that the file isn't already open somewhere?
macro
Posts: 70
Joined: Thu Jul 16, 2015 3:01 pm

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

Unread post 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.
macro
Posts: 70
Joined: Thu Jul 16, 2015 3:01 pm

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

Unread post 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.
Attachments
output83beta.rgf
(1.29 KiB) Downloaded 1061 times
output83beta.pdf
(3.08 KiB) Downloaded 845 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

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

Unread post by TomDoan »

Are you using the version 9 that I sent you?
macro
Posts: 70
Joined: Thu Jul 16, 2015 3:01 pm

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

Unread post 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 23611 times
This doesn't come from the code above, but from part of the larger project I'm working on.
macro
Posts: 70
Joined: Thu Jul 16, 2015 3:01 pm

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

Unread post 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.
Attachments
bad_spacing.pdf
(4.29 KiB) Downloaded 866 times
errors.csv
(1.15 KiB) Downloaded 1092 times
cczzwhy
Posts: 48
Joined: Tue Jun 16, 2015 3:47 am

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

Unread post 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.
Post Reply