By Jochen Voss, on
I like to use the matplotlib Python library to create figures for use in my articles. Unfortunately, the matplotlib default settings seem to be optimised for screen-viewing, so creating pictures for inclusion in a scientific paper needs some fiddling. Here is an example script which sets up things for such a figure.
#! /usr/bin/env python import matplotlib matplotlib.use("PS") from pylab import * rc("font", family="serif") rc("font", size=10) width = 4.5 height = 1.4 rc("figure.subplot", left=(22/72.27)/width) rc("figure.subplot", right=(width-10/72.27)/width) rc("figure.subplot", bottom=(14/72.27)/height) rc("figure.subplot", top=(height-7/72.27)/height) figure(figsize=(width, height)) x = loadtxt("x.dat") plot(x[:,0], x[:,1], "k-") savefig("figure.eps")
The script performs the following steps:
matplotlib.use
statement tells matplotlib to use one
of the non-interactive backends. Without this statement, the script would
need a connections to an X-windows server to run.
rc
statement set the font family and size for the
labels on the axes to match the font used in the article's text
(matplotlib uses a 12pt sans-serif font by default).
rc("figure.subplot", ...)
settings and the
figsize
argument to the figure
call set the
image geometry. Since the text on journal pages typically is about 5in
wide, a total figure width of 4.5in is often a good value. The margins set
by the rc
statements have to be given as fractions of the
image width/height and must be big enough to contain the axis labels. A
bit of experimentation helps to find good values; here we use a 14pt
margin at the bottom (10pt font height plus some space), 22pt left margin
(depends on the number of digits in the y-axis labels), 7pt for the
top margin (space for half of the top-most y-axis label, and then
some more) and 10pt to the right (enough space for half the right-most
x-axis label).
"k"
in the plot command sets the
plot color to black. Matplotlib default is blue, but some journals charge
you for use of colour in your articles (and also it is nice if you can
print things on a b/w printer).
savefig
command creates an encapsulated PostScript file. This format is good for inclusion in TeX documents. The file can be converted to PDF using the epstopdf command as needed.
This is an excerpt from Jochen's blog.
Newer entry: New Book Chapter
Older entry: CO2 emissions for different modes of transportation (updated)
Copyright © 2009 Jochen Voss. All content on this website (including text, pictures, and any other original works), unless otherwise noted, is licensed under the CC BY-SA 4.0 license.