Matplotlib Plots

Matplotlib

How to generate publishable quality images using python pylab

I use python-pylab, a python interface to matplotlib to generate most of my plots.

This page describes how to produce publishable quality images using python pylab and Matplotlib. Most of the ideas have been taken from: http://www.scipy.org/Cookbook/Matplotlib/LaTeX_Examples

I have written a plot_settings.py module to switch between various modes of plotting and I have separated out all the plot settings into this script.

#!/usr/bin/python

# Module: plot_settings.py
# Author: Varun Hiremath <vh63@cornell.edu>
# Created: Thu,  2 Apr 2009 05:06:31 -0400

import pylab, math

# Symbols
symbols = ['-','--','-.',':','.',',','o','^','v','<','>','s','+','x','D','d','1','2','3','4','h','H','p']
# Symbols + line
lps = [k+'-' for k in [',','.','o','^','v','<','>','s','+','x','D','d','1','2','3','4','h','H','p']]
# Colors
colors= ['b','g','r','c','m','y','k','w']

def get_figsize(fig_width_pt):
    inches_per_pt = 1.0/72.0                # Convert pt to inch
    golden_mean = (math.sqrt(5)-1.0)/2.0    # Aesthetic ratio
    fig_width = fig_width_pt*inches_per_pt  # width in inches
    fig_height = fig_width*golden_mean      # height in inches
    fig_size =  [fig_width,fig_height]      # exact figsize
    return fig_size

# Publishable quality image settings for 2-column papers
params0 = {'backend': 'eps',
          'axes.labelsize': 6,
          'text.fontsize': 6,
          'xtick.labelsize': 6,
          'ytick.labelsize': 6,
          'legend.pad': 0.1,    # empty space around the legend box
          'legend.fontsize': 5,
          'lines.markersize': 3,
          'font.size': 6,
          'text.usetex': True,
          'figure.figsize': get_figsize(250)}

# Medium sized images
params1 = {'backend': 'eps',
          'axes.labelsize': 8,
          'text.fontsize': 8,
          'xtick.labelsize': 8,
          'ytick.labelsize': 8,
          'legend.pad': 0.1,     # empty space around the legend box
          'legend.fontsize': 8,
          'lines.markersize': 3,
          'font.size': 8,
          'text.usetex': True,
          'figure.figsize': get_figsize(500)}

# Large images (default)
params2 = {'backend': 'eps',
          'axes.labelsize': 10,
          'text.fontsize': 10,
          'xtick.labelsize': 10,
          'ytick.labelsize': 10,
          'legend.pad': 0.2,     # empty space around the legend box
          'legend.fontsize': 10,
           'lines.markersize': 3,
          'font.size': 10,
          'text.usetex': True,
          'figure.figsize': get_figsize(800)}

def set_mode(mode):
    if mode == "publish":
        pylab.rcParams.update(params0)
    elif mode == "medium":
        pylab.rcParams.update(params1)
    else:
        pylab.rcParams.update(params2)

def set_figsize(fig_width_pt):
    pylab.rcParams['figure.figsize'] = get_figsize(fig_width_pt)

pylab.rcParams.update(params2)

A sample script sample_plots.py is included below demonstrating the use of plot_settings.py module to plot medium sized and publishable quality images. The data file used is available here error.op.

#!/usr/bin/python

# Author: Varun Hiremath <vh63@cornell.edu>
# Created: Thu,  2 Apr 2009 05:21:30 -0400

import scipy, pylab
from scipy import io
import plot_settings as ps

error = io.read_array("error.op")
nrs = error[:,0]

# Publishable quality image
ps.set_mode("publish")
pylab.figure(1)
pylab.axes([0.125,0.2,0.95-0.125,0.95-0.3])

for i in range(6, len(error[0])):
    pylab.plot(nrs, error[:,i], ps.lps[i], label="$\Phi_{"+str(i-5)+"}$")

pylab.legend(numpoints=1)
pylab.xlabel("$n_{rs}$")
pylab.ylabel("error ($\epsilon$)")
pylab.title("Plot of error")
pylab.savefig("error_publish.eps")
pylab.savefig("error_publish.png")

# Medium size image
ps.set_mode("medium")
pylab.figure(2)
# pylab.axes([0.125,0.2,0.95-0.125,0.95-0.3])

for i in range(6, len(error[0])):
    pylab.plot(nrs, error[:,i], ps.lps[i], label="$\Phi_{"+str(i-5)+"}$")

pylab.legend(numpoints=1)
pylab.xlabel("$n_{rs}$")
pylab.ylabel("error ($\epsilon$)")
pylab.title("Plot of error")
pylab.savefig("error_medium.eps")
pylab.savefig("error_medium.png")
Sample image 

Sample images produced (png quality).