大体步骤是:dom
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 5, 10) y = x**2
# 首先,须要一个figure对象 fig = plt.figure()
# 而后,axes对象,在axes上面绘图 axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) axes.plot(x, y, 'r') axes.set_xlabel('x') axes.set_ylabel('y') axes.set_title('title')
若是须要在一张图中绘多个轴:spa
fig, axes = plt.subplots(nrows=1, ncols=2) for ax in axes: ax.plot(x, y, 'r') ax.set_xlabel('x') ax.set_ylabel('y') ax.set_title('title')
一些例子:3d
n = np.array([0,1,2,3,4,5]) x = np.linspace(-0.75, 1., 100) fig, axes = plt.subplots(1, 4, figsize=(12,3)) axes[0].scatter(x, x + 0.25*np.random.randn(len(x))) axes[1].step(n, n**2, lw=2) axes[2].bar(n, n**2, align="center", width=0.5, alpha=0.5) axes[3].fill_between(x, x**2, x**3, color="green", alpha=0.5);
散点图,改变颜色,大小:code
import numpy as np
import matplotlib.pyplot as plt对象
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2 # 0 to 15 point radiuses
color = 2 * np.pi * np.random.rand(N)
plt.scatter(x, y, s=area, c=color, alpha=0.5, cmap=plt.cm.hsv)
plt.show()blog
直方图:it
x = np.random.randn(1000) num_bins = 40 plt.hist(x, num_bins, facecolor='green', alpha=0.5)
极坐标:class
fig = plt.figure() ax = fig.add_axes([0.0, 0.0, .6, .6], polar=True) t = linspace(0, 2 * pi, 100) ax.plot(t, t, color='blue', lw=3);