Playing with matplotlib

%pylab will populate the current scope with functions from numpy and matplotlib. inline allows the plots to be displayed directly in the notebook.


In [89]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [91]:



Help on built-in function pop:

pop(...)
    L.pop([index]) -> item -- remove and return item at index (default last).
    Raises IndexError if list is empty or index is out of range.


In [ ]:


In [81]:
X=[1,2,3];
Y=[4,6,5];
plot(X, Y, 'bo-')


Out[81]:
[<matplotlib.lines.Line2D at 0x83a3bd0>]

In [82]:
plot(X, Y,  marker='o', linestyle='--', color='#ff2780', label='legend uses this');
legend(loc='lower left');



In [83]:
# change the fig size
fig = figure(figsize=(10, 5)); 
plot(X, Y)
xlabel('x axis', fontsize=20)
ylabel('y axis')
title('figure title')
xlim([1,4]);

# or fig.set_figheight(5) later


Out[83]:
(1, 4)

In [84]:
#save figure. figsize affects the saved raster figure
fig.set_figwidth(20);
fig.set_figheight(10);
fig.savefig('testplot.png');
fig.savefig('testplot.eps');

In [85]:
# globally changing the font size
font = {'family' : 'monospace',
          'weight' : 'bold',
          'size'   : 15};
rc('font', **font);
rc('lines', linewidth=2);
# plot again
plot(X, Y);
title('some title')
xlabel('my x-axis')


Out[85]:
<matplotlib.text.Text at 0x83d1c10>

Some matrix plot


In [86]:
A = np.random.rand(10,10)
plt.imshow(A)
plt.colorbar()


Out[86]:
<matplotlib.colorbar.Colorbar instance at 0x86f9830>

In [87]:
plt.imshow(A, interpolation='nearest')
plt.colorbar()


Out[87]:
<matplotlib.colorbar.Colorbar instance at 0x8d3db00>

In [87]: