PNG vs PDF in Gnuplot: pngcairo or pdfcairo?

Short answer: use pngcairo when the figure will be seen on a screen at a known size (web pages, slides, chat), and pdfcairo when it will be printed, zoomed or included in a LaTeX document. Both are Cairo terminals with the same drawing quality and the same font syntax — they differ in what kind of file comes out: pixels versus vectors.

set terminal pngcairo size 800,600 enhanced font "Sans,12"
set output "results.png"
set title "Measurement Results"
set xlabel "Time (s)"
set ylabel "Signal (mV)"
set grid
plot sin(x) with lines linewidth 2 title "sin(x)", \
     cos(x) with lines linewidth 2 title "cos(x)"
set output

set terminal pdfcairo size 12.5cm,9cm enhanced font "Sans,12"
set output "results.pdf"
replot
set output

The script exports the same plot twice. The first block is the familiar pattern: choose the terminal, name the output, plot. It then switches terminal to pdfcairo, names a PDF file, and calls replot to redraw the identical plot into the new format — the standard gnuplot idiom for multi-format export. Notice the two different size dialects: pngcairo measures its canvas in pixels (size 800,600), while pdfcairo measures in physical units, inches by default or centimetres when you write cm after the numbers, because a PDF page has a real-world size.

Side by side

Aspectpngcairopdfcairo
Output typeRaster image (a grid of pixels)Vector document (lines and text stay lines and text)
Size optionPixels: size 800,600Physical: size 5in,3in (default) or size 12.5cm,9cm
Scaling laterBlurs or pixelates when enlargedScales cleanly to any print size
FontsRendered into the pixels at export timeKept as text in the PDF, so it stays sharp and searchable
Best forWeb pages, slides, READMEs, quick previewsPapers, reports, theses, anything printed

The font clause is identical on both: font "Sans,12" names a family and a size, and either terminal default can be overridden per element (for example set title "..." font "Sans,14"). Both terminals also support the enhanced text mode named in the terminal line: it enables superscripts and subscripts inside labels, so set ylabel "Density (10^6 counts)" and "H_2O" render with raised and lowered parts, plus simple font changes within a string. If a label looks literally like 10^6 with a visible caret, the terminal was started with noenhanced or does not support the mode.

Which one should you pick?

  • Web, slides, messaging: pngcairo, sized generously — export at roughly twice the display size so the figure stays crisp on high-resolution screens.
  • Print and PDF documents: pdfcairo. A vector figure is also usually smaller than a high-resolution PNG of the same plot, and the journal or your future self can zoom without limit.
  • Unsure: export both with the replot pattern above and archive the PDF as the master copy; a PNG can always be regenerated from the script, but pixels can never be turned back into vectors.

Common mistakes

  • PDF comes out a different physical size than expected: pdfcairo defaults to 5×3 inches. Always state the size you want explicitly, in inches or with an explicit cm unit, rather than relying on the default.
  • PNG looks fine on screen but fuzzy in print: 800 pixels across a full page width is not enough. Either raise the pixel size (and the font size with it, or use fontscale) or switch that export to pdfcairo — for print, vector beats more pixels.
  • Zero-byte or truncated file: the output was never closed. End each export block with a bare set output, as in the script, so gnuplot flushes and finishes the file.

Related tutorials: Your First Gnuplot Plot, SVG Output in Gnuplot, Fonts in Gnuplot. See also the full Gnuplot Tutorials index.

Try it online: The plot commands above run in the online terminal as-is; the cairo terminals need a local gnuplot install for the actual file export. Paste the script into the Gnuplot Online terminal and run it in your browser — no install needed. To run gnuplot locally instead, see the installation guide and the download page.

Go deeper: For a systematic walk through gnuplot from first plots to publication-ready figures, see Gnuplot in Action by Philipp K. Janert — Janert is a gnuplot developer, and the book is the most complete tutorial reference for the program. Affiliate link: see our Affiliate Disclosure.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *