Matplotlib Basic

Matplotlib is an excellent 2D and 3D graphics library for generating scientific figures.


In [2]:
# This line configures matplotlib to show figures embedded in the notebook, instead of opening a new window for each figure.
%matplotlib inline

2D Figures


In [4]:
from pylab import *
import numpy as np
I = np.identity(3)

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

In [8]:
figure()
plot(x, y, '--r')
xlabel('x')
ylabel('y')
title('title')
show()



In [9]:
figure()
x = np.random.random(50)
y = x * 10 + np.random.random(50)
scatter(x, y)
show()



In [10]:
figure()
hist(np.random.randn(10000))
show()


Image


In [11]:
import matplotlib.image as mpimg
img=mpimg.imread('nao_camera.png')
img


Out[11]:
array([[[ 0.94901961,  0.89411765,  0.93725491],
        [ 0.94901961,  0.89411765,  0.93725491],
        [ 0.94117647,  0.89803922,  0.93725491],
        ..., 
        [ 0.93333334,  0.93333334,  1.        ],
        [ 0.94117647,  0.93333334,  0.99607843],
        [ 0.94117647,  0.93333334,  0.99607843]],

       [[ 0.95294118,  0.89803922,  0.94117647],
        [ 0.94901961,  0.89411765,  0.93725491],
        [ 0.94117647,  0.90196079,  0.94117647],
        ..., 
        [ 0.9254902 ,  0.93333334,  0.99215686],
        [ 0.93725491,  0.92941177,  0.99215686],
        [ 0.93725491,  0.92941177,  0.99215686]],

       [[ 0.9254902 ,  0.89803922,  0.9254902 ],
        [ 0.92941177,  0.90196079,  0.92941177],
        [ 0.9254902 ,  0.90588236,  0.92941177],
        ..., 
        [ 0.9254902 ,  0.93333334,  1.        ],
        [ 0.92941177,  0.92941177,  1.        ],
        [ 0.93333334,  0.93333334,  1.        ]],

       ..., 
       [[ 0.15294118,  0.44313726,  0.29803923],
        [ 0.14901961,  0.43921569,  0.29411766],
        [ 0.16470589,  0.42745098,  0.30980393],
        ..., 
        [ 0.30588236,  0.50588238,  0.40784314],
        [ 0.30588236,  0.49803922,  0.40392157],
        [ 0.30588236,  0.49803922,  0.40392157]],

       [[ 0.15686275,  0.43921569,  0.3137255 ],
        [ 0.14901961,  0.43137255,  0.30588236],
        [ 0.18039216,  0.43529412,  0.31764707],
        ..., 
        [ 0.32156864,  0.50196081,  0.42745098],
        [ 0.31764707,  0.49019608,  0.41176471],
        [ 0.3137255 ,  0.48627451,  0.40784314]],

       [[ 0.21568628,  0.43529412,  0.28627452],
        [ 0.21568628,  0.43529412,  0.28627452],
        [ 0.20392157,  0.43529412,  0.3019608 ],
        ..., 
        [ 0.3137255 ,  0.49019608,  0.43921569],
        [ 0.34117648,  0.48627451,  0.42352942],
        [ 0.33725491,  0.48235294,  0.41960785]]], dtype=float32)

In [12]:
imgplot = imshow(img)



In [13]:
g_img = img[:, :, 1]
imgplot = imshow(g_img)
imgplot.set_cmap('hot')
cb = plt.colorbar()


3D Figures


In [14]:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D

In [15]:
x = linspace(0, 5, 100)
y = x ** 2
z = np.sin(x)

In [16]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
p = ax.plot(x, y, z)