In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()



In [2]:
plt.plot([1,2,3,4], [1,4,9,16])
plt.show()



In [3]:
plt.plot([1,2,3,4], [1,4,9,16], 'b-')
plt.show()



In [4]:
plt.plot([1,2,3,4], [1,4,9,16], 'ro')
plt.axis([0, 6, 0, 20]) # [xmin, xmax, ymin, ymax]
plt.show()



In [5]:
# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)

# red dashes, blue squares, and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()



In [6]:
x = np.arange(0., 5., 0.2)
y = x+1

line, = plt.plot(x, y, '-', linewidth=2.0) # using tuple unpacking to get first element in list
line.set_antialiased(False) # turn of antialiasing
plt.show()



In [7]:
x1 = np.arange(0., 5., 0.2)
y1 = x1+1

x2 = np.arange(0., 10., 0.2)
y2 = x2+5

# set properities on a list of lines
lines = plt.plot(x1, y1, x2, y2)

# use keyword args
plt.setp(lines, color='r', linewidth=2.0)

# use MATLAB-style string-value pairs
plt.setp(lines, 'color', 'r', 'linewidth', 2.0)


Out[7]:
[None, None, None, None]

In [8]:
lines = plt.plot([1,2,3])
plt.setp(lines)


  agg_filter: unknown
  alpha: float (0.0 transparent through 1.0 opaque)         
  animated: [True | False]         
  antialiased or aa: [True | False]         
  axes: an :class:`~matplotlib.axes.Axes` instance         
  clip_box: a :class:`matplotlib.transforms.Bbox` instance         
  clip_on: [True | False]         
  clip_path: [ (:class:`~matplotlib.path.Path`,         :class:`~matplotlib.transforms.Transform`) |         :class:`~matplotlib.patches.Patch` | None ]         
  color or c: any matplotlib color         
  contains: a callable function         
  dash_capstyle: ['butt' | 'round' | 'projecting']         
  dash_joinstyle: ['miter' | 'round' | 'bevel']         
  dashes: sequence of on/off ink in points         
  drawstyle: ['default' | 'steps' | 'steps-pre' | 'steps-mid' |                   'steps-post']         
  figure: a :class:`matplotlib.figure.Figure` instance         
  fillstyle: ['full' | 'left' | 'right' | 'bottom' | 'top' | 'none']         
  gid: an id string         
  label: string or anything printable with '%s' conversion.         
  linestyle or ls: [``'-'`` | ``'--'`` | ``'-.'`` | ``':'`` | ``'None'`` |                   ``' '`` | ``''``]         and any drawstyle in combination with a linestyle, e.g., ``'steps--'``.         
  linewidth or lw: float value in points         
  lod: [True | False]         
  marker: unknown
  markeredgecolor or mec: any matplotlib color         
  markeredgewidth or mew: float value in points         
  markerfacecolor or mfc: any matplotlib color         
  markerfacecoloralt or mfcalt: any matplotlib color         
  markersize or ms: float         
  markevery: unknown
  path_effects: unknown
  picker: float distance in points or callable pick function         ``fn(artist, event)``         
  pickradius: float distance in points         
  rasterized: [True | False | None]         
  sketch_params: unknown
  snap: unknown
  solid_capstyle: ['butt' | 'round' |  'projecting']         
  solid_joinstyle: ['miter' | 'round' | 'bevel']         
  transform: a :class:`matplotlib.transforms.Transform` instance         
  url: a url string         
  visible: [True | False]         
  xdata: 1D array         
  ydata: 1D array         
  zorder: any number         

In [9]:
import numpy as np
import matplotlib.pyplot as plt

def f(t):
    return np.exp(-t) * np.cos(2*np.pi*t)

t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)

plt.figure(1) # figure() is optional here because figure(1) will be created by default
plt.subplot(211) # subplot(111) would be created by default if not specified otherwise
# subplot(numrows, numcols, fignum), where fignum ranges from 1 to numrows*numcols
# commas between arguments are optional if numrows*numcols < 10
# thus, subplot(211) = subplot(2,1,1)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')

plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')

plt.show()

plt.gca() # get current axis, an instance of matplotlib.axes.Axes

plt.gcf() # current figure, an instance of matplotlib.figure.Figure


Out[9]:

In [10]:
# it's possible to cerate an arbitrary number of subplots and axes
# to place an axes manually (i.e., not on a rectangular grid), use the axes() command
# use axes() to specify the location as axes([left, bottom, width, height]), 
# where all values are in fractional (0 to 1) coordinates

In [11]:
# it's possible to create multiple figures by using multiple figure() calls with an increasing
# figure number
# each figure can contain as many axes and subplots as specified
import matplotlib.pyplot as plt
plt.figure(1) # first figure
plt.subplot(211) # first subplot in first figure
plt.plot([1,2,3])

plt.subplot(212) # second subplot in first figure
plt.plot([4,5,6])

plt.figure(2) # second figure
plt.plot([4,5,6])

plt.figure(1)
plt.subplot(211)
plt.title('easy as 1,2,3')

#plt.clf() # clear current figure
#plt.cla() # clear current axis

# this statefulness is all a thin stateful wrapper around an object-oriented API

#plt.close() # memory for a figure is not completely released until the figure is closed with close()
# even deleting references or closing windows is not enough


Out[11]:
<matplotlib.text.Text at 0x10f16d410>

In [12]:
import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

#print X.shape
#print Y.shape
#print Z.shape
#print X
#print Y
#print Z
plt.contour(X, Y, Z)

plt.show()



In [12]:


In [13]:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
cset = ax.contour(X, Y, Z, cmap=cm.coolwarm)
ax.clabel(cset, fontsize=9, inline=1)

plt.show()



In [14]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)
cset = ax.contour(X, Y, Z, cmap=cm.coolwarm)


plt.show()



In [15]:
X, Y = np.meshgrid([1, 3, 6, 2], [3, 4, 5, 6])
print X.shape
print Y.shape
print X
print Y

Z = (X - Y) * 10
print Z.shape
print Z

plt.contour(X, Y, Z)

plt.show()


(4, 4)
(4, 4)
[[1 3 6 2]
 [1 3 6 2]
 [1 3 6 2]
 [1 3 6 2]]
[[3 3 3 3]
 [4 4 4 4]
 [5 5 5 5]
 [6 6 6 6]]
(4, 4)
[[-20   0  30 -10]
 [-30 -10  20 -20]
 [-40 -20  10 -30]
 [-50 -30   0 -40]]

In [16]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [1, 3, 6, 2]
y = [3, 4, 5, 6]
ax.scatter(x,y)
X, Y = np.meshgrid(x,y)
Z = np.sqrt((X**2 + Y**2))
ax.contour(X, Y, Z, cmap=cm.coolwarm)


plt.show()



In [17]:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np

im = np.arange(100)
im.shape = 10, 10

fig = plt.figure(1, (4., 4.))
grid = ImageGrid(fig, 111, # similar to subplot(111)
                nrows_ncols = (2, 2), # creates 2x2 grid of axes
                axes_pad=0.1, # pad between axes in inch.
                )

for i in range(4):
    grid[i].imshow(im) # The AxesGrid object work as a list of axes.

plt.show()



In [18]:
data = np.random.randn(500)  # array of 500 random numbers
plt.hist(data)
plt.ylabel("Counts")
plt.title("The Gaussian Distribution")


Out[18]:
<matplotlib.text.Text at 0x10f601650>

In [23]:
plt.hist(data, normed=True)


Out[23]:
(array([ 0.02901289,  0.06092707,  0.17697863,  0.31333921,  0.41778561,
         0.2379057 ,  0.15957089,  0.04061805,  0.01160516,  0.00290129]),
 array([-2.97534705, -2.28599828, -1.59664951, -0.90730074, -0.21795196,
         0.47139681,  1.16074558,  1.85009436,  2.53944313,  3.2287919 ,
         3.91814067]),
 <a list of 10 Patch objects>)