Python Matplotlib: Gaurav Verma
Python Matplotlib: Gaurav Verma
Gaurav Verma
Plotting - matplotlib
• Pylab combines the pyplot functionality (for plotting) with the numpy
functionality (for mathematics and for working with arrays) in a single
namespace, For example, one can call the sin and cos functions just like
you could in MATLAB, as well as having all the features of pyplot.
>>> plt.subplot(1,2,1)
>>> plt.plot(x, y, ’r--’)
>>> plt.subplot(1,2,2)
>>> plt.plot(y, x, ’g*-’);
Matplotlib.pyplot example
import numpy as np
import matplotlib.pyplot as plt
def f(t):
return np.exp(-t) * np.cos(2*np.pi*t)
plt.figure(1)
plt.subplot(211)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')
plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
Matplotlib.pyplot basic example
fig = plt.figure()
axes1 = fig.add_axes([0.1, 0.1,
0.8, 0.8]) # main axes
axes2 = fig.add_axes([0.2, 0.5,
0.4, 0.3]) # inset axes
# main figure
axes1.plot(x, y, ’r’)
axes1.set_xlabel(’x’)
axes1.set_ylabel(’y’)
axes1.set_title(’title’)
# insert
axes2.plot(y, x, ’g’)
axes2.set_xlabel(’y’)
axes2.set_ylabel(’x’)
axes2.set_title(’insert title’);
Matplotlib.pyplot basic example
>>> ax.set_title("title");
>>> ax.set_xlabel("x")
>>> ax.set_ylabel("y");
>>> ax.legend(["curve1", "curve2", "curve3"]);
fig, ax = plt.subplots()
ax.plot(x, x+1, color="red", alpha=0.5) # half-transparant red
ax.plot(x, x+2, color="#1155dd") # RGB hex code for a bluish color
ax.plot(x, x+3, color="#15cc55") # RGB hex code for a greenish color
Matplotlib.pyplot basic example
Line and marker styles are coded:
fig, ax = plt.subplots(figsize=(12,6))
ax.plot(x, x+1, color="blue", linewidth=0.25)
ax.plot(x, x+2, color="blue", linewidth=0.50)
ax.plot(x, x+3, color="blue", linewidth=1.00)
ax.plot(x, x+4, color="blue", linewidth=2.00)
# possible linestype options ‘-‘, ‘{’, ‘-.’, ‘:’, ‘steps’
ax.plot(x, x+5, color="red", lw=2, linestyle=’-’)
ax.plot(x, x+6, color="red", lw=2, ls=’-.’)
ax.plot(x, x+7, color="red", lw=2, ls=’:’)
# custom dash
line, = ax.plot(x, x+8, color="black", lw=1.50)
line.set_dashes([5, 10, 15, 10]) # format: line length, space length, ...
# possible marker symbols: marker = ’+’, ’o’, ’*’, ’s’, ’,’, ’.’, ’1’, ’2’, ’3’, ’4’, ...
ax.plot(x, x+ 9, color="green", lw=2, ls=’*’, marker=’+’)
ax.plot(x, x+10, color="green", lw=2, ls=’*’, marker=’o’)
ax.plot(x, x+11, color="green", lw=2, ls=’*’, marker=’s’)
ax.plot(x, x+12, color="green", lw=2, ls=’*’, marker=’1’)
# marker size and color
ax.plot(x, x+13, color="purple", lw=1, ls=’-’, marker=’o’, markersize=2)
ax.plot(x, x+14, color="purple", lw=1, ls=’-’, marker=’o’, markersize=4)
ax.plot(x, x+15, color="purple", lw=1, ls=’-’, marker=’o’, markersize=8, markerfacecolor="red")
ax.plot(x, x+16, color="purple", lw=1, ls=’-’, marker=’s’, markersize=8,
markerfacecolor="yellow", markeredgewidth=2, markeredgecolor="blue");
Matplotlib.pyplot basic example
>>> n = array([0,1,2,3,4,5])
>>> fig, axes = plt.subplots(1, 4, figsize=(12,3))
>>> axes[0].scatter(xx, xx + 0.25*randn(len(xx)))
>>> axes[0].set_title("scatter")
>>> axes[1].step(n, n**2, lw=2)
>>> axes[1].set_title("step")
>>> axes[2].bar(n, n**2, align="center", width=0.5, alpha=0.5)
>>> axes[2].set_title("bar")
>>> axes[3].fill_between(x, x**2, x**3, color="green", alpha=0.5)
>>> axes[3].set_title("fill_between")
Matplotlib.pyplot basic example
>>> n = np.random.randn(100000)
>>> fig, axes = plt.subplots(1, 2, figsize=(12,4))
>>> axes[0].hist(n)
>>> axes[0].set_title("Default histogram")
Histograms are also a very
useful visualisation tool
>>> axes[0].set_xlim((min(n), max(n)))
>>> axes[1].hist(n, cumulative=True, bins=50)
>>> axes[1].set_title("Cumulative detailed histogram")
>>> axes[1].set_xlim((min(n), max(n)));
Matplotlib.pyplot example
import numpy as np
import matplotlib.pyplot as plt
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
Error bars.
import numpy as np
import matplotlib.pyplot as plt
3D plots.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x =[1,2,3,4,5,6,7,8,9,10]
y =[5,6,2,3,13,4,1,2,4,8]
z =[2,3,3,3,5,7,9,11,9,10]
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
Matplotlib.pyplot example
3D plots.
from numpy import *
import pylab as p
import mpl_toolkits.mplot3d.axes3d as p3
fig=p.figure()
ax = p3.Axes3D(fig)
ax.plot_wireframe(x,y,z)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
p.show()
Data processing example
End