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]:
In [8]:
lines = plt.plot([1,2,3])
plt.setp(lines)
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]:
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()
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]:
In [23]:
plt.hist(data, normed=True)
Out[23]: