Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 36 ยท Descriptive and Graphs

Exporting Graphs, Schemes and Styles

Save and export Stata graphs to PNG, PDF, SVG and EMF, use graph save and graph use, choose schemes, set colors, fonts and sizes, and build consistent publication-ready figures.

A graph that only lives in the Graph window is not finished. You need it in a paper, a slide deck or a report, in a size and style that stay the same on every run. This lesson covers saving graphs in Stata's own format, exporting to image and vector files, controlling appearance with schemes, and a routine that makes figures reproducible from a do-file.

Two ways to save: .gph and exports

STATA
twoway scatter price mpg, name(g1, replace)

graph save g1 "output/figures/price_mpg.gph", replace    // Stata's own format (editable)
graph export "output/figures/price_mpg.png", name(g1) width(2000) replace
graph export "output/figures/price_mpg.pdf", name(g1) replace
  • A .gph file keeps the graph as Stata data, so you can reopen it later with graph use "price_mpg.gph", change it in the Graph Editor, or combine it with other graphs. It can only be opened in Stata.
  • graph export writes the graph to a file other programs can read. The extension picks the format.
Format Extension Best for
PNG .png Web pages and slides (raster, set width())
PDF .pdf Papers and LaTeX (vector, sharp at any size)
SVG .svg Web and editing in vector tools
EPS .eps Older print workflows and some journals
EMF .emf (Windows) Word and PowerPoint on Windows
TIFF .tif Print submissions that require TIFF
JPEG .jpg Photos, not recommended for graphs

Vector formats (PDF, SVG, EPS, EMF) stay crisp when scaled, so prefer them for print. For PNG, give a large width(2000) (pixels) so text stays sharp on high-resolution screens. In a do-file, always use name() and replace so the export is predictable and rerunnable. If graph export is called without name(), it exports the graph currently in the Graph window.

The default size is the window size or a fixed 5.5 by 4 inches (depends on the scheme). Set the size at drawing time so every figure matches:

STATA
twoway scatter price mpg, xsize(6) ysize(4)       // inches
graph export "fig.png", width(1800) height(1200) replace

Schemes: the whole look in one setting

A scheme is a bundle of defaults: colors, fonts, background, line widths, legend position, and whether there is a grid. Change the scheme and every graph in the session changes.

STATA
graph query, schemes                  // list available schemes
set scheme s2color                    // the classic default (Stata 16 and earlier look)
set scheme stcolor                    // the default in recent versions
set scheme s1mono                     // black and white, good for print
set scheme s2mono
twoway scatter price mpg, scheme(s1mono)    // scheme for one graph only

Stata ships schemes such as s2color, s2mono, s1color, s1mono, s1rcolor, economist and sj (the Stata Journal style). More schemes come from SSC (ssc install blindschemes, ssc install schemepack). Pick one at the top of your master do-file and your figures look consistent without repeating options. Choose schemes with colorblind-safe palettes (the blindschemes package provides them) when you can.

To make your choice permanent for all sessions: set scheme s1mono, permanently.

Colors, fonts and lines

STATA
twoway (scatter price mpg, mcolor(navy%50) msymbol(O)) ///
       (lfit price mpg, lcolor(maroon) lwidth(medthick)), ///
    graphregion(color(white)) plotregion(lcolor(none)) ///
    title("Price versus mileage", size(medium) color(black))

Common options:

  • Colors by name (navy, maroon, forest_green, gs8 for grey scale 0 to 16), by RGB ("31 119 180") or hex ("#1f77b4"), with opacity (%50). Run help colorstyle for all names.
  • lcolor, lwidth, lpattern(dash) for lines; mcolor, msymbol, msize for markers; fcolor, lcolor for bars (bar(1, color(navy))).
  • graphregion(color(white)) and plotregion(...) set backgrounds. graphregion(fcolor(white) lcolor(white)) avoids a black outline in some exports.
  • , scale(1.2) makes all text larger at once: a quick fix for tiny fonts in slides.
  • Fonts: graph set window fontface "Arial" for the Graph window; for exports, PDF and PNG use the system fonts, and graph set eps fontface Helvetica sets the font for EPS.

The Graph Editor

Double-click a graph (or choose File > Start Graph Editor) and you can click on any element to move it, change its color, add text, arrows or lines, and edit the plot. Recording edits creates a script (an .grec file) that can be replayed on another graph: graph play myedits. Editing by hand is tempting but not reproducible, so use the editor to discover the option and then write the option in your do-file.

A reproducible figure routine

Put these habits into every do-file that makes graphs:

STATA
* 04_figures.do
version 18
clear all
set scheme s1mono

use "data/clean/survey_clean.dta", clear

twoway (histogram income, percent color(gs12) lcolor(gs6)), ///
    title("Income distribution") xtitle("Monthly income (USD)") ///
    ytitle("Percent of households") name(income_hist, replace)
graph export "output/figures/income_hist.pdf", name(income_hist) replace
graph export "output/figures/income_hist.png", name(income_hist) width(2000) replace

graph close _all
  • version 18 fixes the language behavior (use your own version number).
  • The scheme is set once and the graph is named and exported in two formats.
  • graph close _all closes all open graph windows so a batch run does not accumulate them.
flowchart LR
    A["Do-file draws<br/>graph with name()"] --> B["graph save<br/>.gph (editable)"]
    A --> C["graph export<br/>.pdf / .png / .svg"]
    C --> D["Paper, slides, web"]
    B --> E["graph use / combine / editor"]

Keep the do-file as the source of truth; the exports and .gph files are products of it.

Panels, sizes and publication tips

  • Join graphs with graph combine a b, cols(2) ycommon (shared y-axis) and export the combined graph.
  • Keep axis labels with units, readable font sizes and a legend that does not need color alone.
  • Use grayscale and different line patterns (lpattern(dash dot)) for black-and-white print.
  • Avoid chart clutter: remove the grid or box when it adds nothing; avoid 3D effects and pie charts with many slices.
  • Write a caption in the document, not in the graph, so the text stays editable.
  • Check the final export at the printed size before submitting.

Stand-in: a style bundle

A scheme is "defaults you set once". The Python model keeps a style dictionary and applies it to every figure description; overrides win over the scheme, like options on a graph command.

Python Stand-in: scheme defaults and overrides

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Stand-in: pick an export format

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which file type can only be reopened in Stata?

    .gph is Stata's graph format; the others are standard files for other programs.
  2. 2. Which export format stays sharp at any size?

    Vector formats such as PDF, SVG and EPS scale without losing sharpness.
  3. 3. What does a scheme do?

    A scheme bundles the look of graphs; set scheme changes it for the session.
  4. 4. Why is graph export with name() and replace good practice in a do-file?

    A named graph and overwrite behavior make batch runs predictable.

Summary

  • graph save keeps an editable .gph; graph export writes PNG, PDF, SVG, EPS or EMF for other programs.
  • Prefer vector formats for print and a large width() for PNG; set xsize() and ysize() for consistent figures.
  • Schemes set the whole look; set scheme once, and choose a colorblind-safe or monochrome scheme where needed.
  • Use options for colors, lines, markers and scale(); use the Graph Editor to discover options, then code them.
  • Name, export and close graphs in the do-file so every figure can be regenerated.

Next: Linear Regression with regress