This is now recommended over %pylab inline


In [5]:
%matplotlib inline

This introduces matplotlib and pylab (probably among other things) to the namespace. We explicitly import everything else we need.

Because we are all responsible adults here, we ought to stick with the cleaner object-oriented interface pyplot (plt)


In [6]:
from matplotlib import pyplot as plt

In [7]:
import numpy as np
import pandas as pd

Use the following convenience function for plots for publication


In [8]:
def fig_size(fig_width_pt):
    inches_per_pt = 1.0/72.27               # Convert pt to inch
    golden_mean = (np.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
    return (fig_width, fig_height)

For a Latex document with \columnwidth 250 for example, we would use


In [9]:
params = {
    'backend': 'ps',
    'axes.labelsize': 10,
    'text.fontsize': 10,
    'legend.fontsize': 10,
    'xtick.labelsize': 8,
    'ytick.labelsize': 8,
    'text.usetex': True,
    'figure.figsize': fig_size(350.0)
}; params


Out[9]:
{'axes.labelsize': 10,
 'backend': 'ps',
 'figure.figsize': (4.8429500484295005, 2.993107735747381),
 'legend.fontsize': 10,
 'text.fontsize': 10,
 'text.usetex': True,
 'xtick.labelsize': 8,
 'ytick.labelsize': 8}

In [10]:
x = np.linspace(0, 5, 10)
y = x ** 2

With that, we are ready to make some plots


In [11]:
# for the publication-ready plots
with plt.rc_context(params):
    fig, axes = plt.subplots()
    axes.plot(x, y, 'r')
    axes.set_xlabel('x')
    axes.set_ylabel('y')
    axes.set_title('title')
    fig.savefig('plot.eps')



In [12]:
# for regular plots for inline viewing
fig, axes = plt.subplots()
axes.plot(x, y, 'r')
axes.set_xlabel('x')
axes.set_ylabel('y')
axes.set_title('title')


Out[12]:
<matplotlib.text.Text at 0x107782710>

In [13]:
# for xkcd style plots
with plt.xkcd():
    fig, axes = plt.subplots()
    axes.plot(x, y, 'r')
    axes.set_xlabel('x')
    axes.set_ylabel('y')
    axes.set_title('title')


Especially fond of the ability to make plots directly with pandas data structures


In [14]:
df = pd.DataFrame(np.random.randn(50, 4)); df


Out[14]:
0 1 2 3
0 1.167608 -0.458334 -0.918084 0.733271
1 0.278281 1.132983 -0.908310 -0.380805
2 -0.909739 -0.296517 -1.082183 -1.374550
3 -0.893025 1.158158 0.761520 0.613968
4 -0.620827 -1.567075 -1.049536 0.962658
5 0.475419 1.386793 0.798379 0.590144
6 -1.537677 0.638286 -0.161207 1.233663
7 0.070414 0.412677 0.672243 -0.142659
8 -0.456939 0.160147 -1.002376 -0.372419
9 0.276929 -0.635191 0.889454 1.653164
10 0.484616 -1.404420 0.735482 -1.733745
11 0.001873 -0.305948 -0.876147 0.872717
12 -2.011477 -1.002090 0.272683 0.236153
13 0.129138 0.276532 0.935123 -0.209988
14 -0.059428 1.294783 0.754561 -0.433896
15 -0.919852 -0.876178 -2.619658 0.937304
16 1.713422 -0.455328 -0.678657 1.141669
17 0.203004 0.266485 -1.983320 0.030042
18 -0.810396 -0.216571 0.765865 0.197593
19 0.882956 -0.689677 0.734937 0.865756
20 -0.467144 -0.351896 2.883086 -1.367048
21 -0.243787 0.083191 -0.342153 1.252945
22 0.136954 0.647945 -1.153710 0.356150
23 2.237128 0.680304 0.240698 0.516514
24 0.527851 -1.252296 0.347460 -0.748309
25 -2.136866 0.758110 -0.679194 1.667402
26 1.381148 -0.445577 -0.074882 0.868037
27 -0.162995 1.641552 0.785555 2.954206
28 0.299133 0.643042 0.477463 0.964797
29 -0.238233 0.733031 0.782799 0.048659
30 -1.142683 -0.597069 0.426085 0.449329
31 0.795397 -0.384432 0.887271 -0.091803
32 -0.103920 -1.164989 0.282953 1.241319
33 -0.068046 -0.480054 0.799845 0.294001
34 0.553636 0.756095 -0.596505 -1.839652
35 1.207141 0.778127 1.748992 0.099056
36 -0.711920 1.170556 2.021804 -0.197851
37 -0.437082 -1.365135 -0.093418 0.482111
38 -1.577152 0.017964 0.402784 -1.022655
39 1.031536 -1.317241 0.061902 -0.587994
40 0.948377 1.282958 0.859029 0.377171
41 -1.791136 -0.619255 -0.504121 -1.087326
42 -1.008320 -0.426981 -2.073628 -0.581643
43 0.343229 1.258088 -1.503615 0.770873
44 0.307043 0.999707 0.295434 -1.293706
45 0.929601 2.150858 0.467455 -1.238318
46 0.361291 1.175731 1.560616 -0.961978
47 0.017699 -1.135330 1.693420 -2.439152
48 -0.641632 0.779156 -0.566606 -1.981783
49 -1.132586 -0.123396 -1.092064 -0.134838

In [15]:
# for the publication-ready plots
with plt.rc_context(params):
    fig, axes = plt.subplots()
    df.plot(ax=axes, style='o')
    axes.set_xlabel('x')
    axes.set_ylabel('y')
    axes.set_title('a pretty random plot')
#     fig.savefig('plot.eps')



In [ ]: