前言
matplotlib包含了很多不同的模块,其中pyplot是常用的绘图模块
基本使用方法
绘制正弦曲线,并设置标题、坐标轴名称、坐标轴范围
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False
x = np.arange(-1 * np.pi, 1 * np.pi, 0.01)
y = np.sin(x)
plt.plot(x, y)
plt.title(u'正弦曲线', fontdict={'size': 20})
plt.xlabel(u'弧度', fontdict={'size': 16})
plt.ylabel(u'正弦值', fontdict={'size': 16})
plt.axis([-1.5 * np.pi, 1.5 * np.pi, -1.5, 1.5])
plt.show()
同一坐标系中绘制多种曲线并通过样式、宽度、颜色加以区分
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False
x = np.linspace(-4, 4, 200)
f1 = np.power(10, x)
f2 = np.power(np.e, x)
f3 = np.power(2, x)
plt.plot(x, f1, 'r', ls='-', linewidth=2, label='$10^x$')
plt.plot(x, f2, 'g', ls='--', linewidth=2, label='$e^x$')
plt.plot(x, f3, 'b', ls=':', linewidth=2, label='$e^x$')
plt.text(1, 7.5, r'$10^x$', fontsize=16)
plt.text(2.2, 7.5, r'$e^x$', fontsize=16)
plt.text(3.2, 7.5, r'$2^x$', fontsize=16)
plt.title(u'幂函数曲线', fontdict={'size': 20})
plt.xlabel(u'x', fontdict={'size': 16})
plt.ylabel(u'y', fontdict={'size': 16})
plt.legend(loc='upper left')
plt.axis([-4, 4, 0, 8])
plt.show()
绘制多轴图,即将多幅子图绘制在同一画板
import matplotlib.pyplot as plt
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False
plt.subplot(221) # 两行两列第1个位置
plt.axis([-1, 2, -1, 2])
plt.axhline(y=0.5, color='b')
plt.axhline(y=0.5, xmin=0.25, xmax=0.75, color='r')
plt.subplot(222) # 两行两列第2个位置
plt.axis([-1, 2, -1, 2])
plt.axvline(x=0, ymin=0, linewidth=4, color='b')
plt.axvline(x=1, ymin=0.5, ymax=0.75, color='r')
plt.subplot(212) # 两行一列第2个位置
plt.axis([-1, 2, -1, 2])
plt.axvspan(1.25, 1.55, facecolor='g', alpha=1)
plt.axhspan(0.25, 0.75, facecolor='b', alpha=0.2)
plt.show()
直方图的绘制
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False
data = np.random.normal(0, 2, 666)
plt.hist(data)
bins = np.arange(-10, 10, 1)
plt.hist(data, bins)
plt.show()
绘制散点图
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False
x = np.random.rand(50)
y = np.random.rand(50)
area = np.pi * (15 * np.random.rand(50)) ** 2
color = 2 * np.pi * np.random.rand(50)
plt.scatter(x, y, s=area, c=color, alpha=0.5)
plt.show()
绘制盒状图
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False
data=np.random.randn(1000)
fig, (ax2) = plt.subplots(1, figsize=(8, 4))
ax2.boxplot(data)
plt.show()
绘制2D图
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False
y, x = np.ogrid[-2:2:200j, -3:3:300j]
z = x*np.exp(-x**2 -y**2)
extent = [np.min(x), np.max(x), np.min(y), np.max(y)]
plt.subplot(121)
cs = plt.contour(z, 10, extent=extent)
plt.clabel(cs)
plt.subplot(122)
plt.contourf(x.reshape(-1), y.reshape(-1), z, 20)
plt.show()
绘制3D图
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
import mpl_toolkits.mplot3d
mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False
x, y = np.mgrid[-2:2:50j, -2:2:50j]
z = x * np.exp(-x ** 2 - y ** 2)
ax = plt.subplot(111, projection='3d')
ax.plot_surface(x, y, z, rstride=2, cstride=1, cmap=plt.cm.coolwarm)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()