Attractive scientific plots with gnuplot

I use gnuplot for nearly all my graph-drawing for academic publications. On the whole, it's clean and relatively flexible, and that combined with inertia has been enough to keep me from trying interesting alternatives like matplotlib, Plot, ploticus, and R. However, gnuplot's default output is not especially pretty. I often see graphs in papers that look like this...

...or worse, if it's been bitmapped rather than using EPS or PDF. With some tweaking, however, one can produce much more attractive output. I would much rather look at plots like this:

In fact it looks better. Blogger doesn't seem to support any vector image format, but here are the pdf version and the svg version. To produce the PDF version, you need gnuplot 4.4's pdfcairo terminal. Below, you can see the gnuplot files for the above two plots.



Now here's something for which I would pay (some) real money: a gnuplot terminal which outputs directly to Keynote. Then, for example, during a presentation, one could have lines in the plot appear one at a time, explaining each without the distraction of showing irrelevant objects. This should actually be quite doable since Keynote's format is just a zipped XML.

Update 2011.04.09: Mac users of macports may note that the default install of gnuplot for some reason excludes pdfcairo. Abhinav Bhatele writes with instructions for enabling pdfcairo in macports:

$ sudo port edit gnuplot

Add these lines somewhere in the file (I added them before the lua variant):

variant pangocairo description "Enable pdfcairo" {
     depends_lib-append      port:pango
     configure.args-delete   --without-cairo
     configure.args-append   --with-cairo
}

$ sudo port info gnuplot

Just to check that pangocairo variant exists. And then:

$ sudo port uninstall gnuplot
$ sudo port install gnuplot +pangocairo

You'll need to keep in mind that if you do port selfupdate,
the edited version of the portfile might get overwritten.

Update to the update 2011.12.04: Looks like macports now includes the pangocairo variant, but still does not install it by default; so it should work if you run just the last two lines.

Update 2011.04.09: Added SVG version and made it slightly more beautiful.

Update 2013.06.21: Added fontscale 1.0 in PDF version. Also, it seems the SVG output now looks somewhat different in a more recent gnuplot ... will have to fix that sometime.

15 comments:

  1. Very nice. Any thoughts on how to format axis tics? For example, can I change 18000 to 18,000?

    ReplyDelete
  2. I don't know if there's a convenient way to format all tics automatically like that, but you can set each one explicitly. See 'help set xtics' in gnuplot.

    ReplyDelete
  3. I would provide matching funds for a terminal to Powerpoint ;-) I know that's a dirty word for some.

    Thank you for the great post. For me, in Linux, the font comes out way too big. Reducing the size to 6 from 9, matches your result.

    ReplyDelete
  4. Very nice one, the default output of Gnuplot is one of the worst things of the whole program.

    I have used your plot at my site, to highlight some of the things one should do to enhance the plots.

    ReplyDelete
  5. Excellent article - I'll be changing my default plt files for my thesis. My only question is - can you change the fit line?

    It's the only part I've not been able to change.

    ReplyDelete
  6. There is no need for a Keynote terminal. Just plot each curve in a separate file and insert them in contiguous slides. With the proper alignment the desired effect can be easily achieved. I have done that with beamer.

    ReplyDelete
  7. drezha: Post here if you figure it out...

    Marduk: I've done that too, but (1) it is slow and annoying, (2) with plots represented as Keynote objects one can directly manipulate them with all of Keynote's tools: change line colors to match the presentation, make some lines partially transparent, change fonts, etc.

    ReplyDelete
  8. Hey Brighten, just finished borrowing your gnuplot script when I noticed it was you that had written this up...thanks!

    ReplyDelete
  9. Interesting. I agree with your contention that un-enhanced gnuplot plots can be quite sub-optimal for viewing. Thanks for demonstrating some techniques for improvement. Did you know that the use of JPEG format for computer generated images is also sub-optimal, and a lossless bitmap format will result in better image quality in your blog?

    ReplyDelete
  10. From the bottom of my thesis, thank you!

    ReplyDelete
  11. Digging up an old blog, but you should make a mention of the "fontscale" option as this has had me troubled for hours. Basically, when using pdfcairo, the font sizes are halved, i.e. fontscale = 0.5 by default. If you are making a publication quality figure and require the exact figures width/height and font sizes, then this can be quite a bugger!

    Example of it's use in your example: "set terminal pdfcairo font "Gill Sans,9" linewidth 4 rounded fontscale 1.0"

    ReplyDelete
  12. Very nice post - originally found it on gnuplotting.
    I especially like your choice of colors - the pleasing effect of them is quite impressive in my opinion. I wonder if you have more colors that you have picked for niceness? Did you come up with them yourself, or did you find them somewhere else?

    ReplyDelete
  13. lpgaff, great point about fontscale. I've added that. Kaspar, glad you enjoyed it. I chose colors that felt "natural": not jarringly vivid, but with enough distinction across the colors.

    ReplyDelete
  14. You did not mention Gri. Gri produces postscript output. Gri-mode for emacs provides a convenient interface (as does gnuplot-mode to a lesser extent). Good facility for placing plots on a page.

    ReplyDelete