matplotlib绘图的基本语法为:
pyplot.图名(x, y, 参数=值),其中,x和y为列表或Numpy数组。数组
import matplotlib.pyplot as plt import numpy as np import math %matplotlib inline ##让绘图显示在Jupyter笔记本中 x01 = np.arange(0,9) ##创建Numpy数组 y01 = x01**2 x02 = np.linspace(-math.pi*2,math.pi*2,100) ##创建Numpy数组 y02 = np.sin(x02)*10 plt.figure(figsize=(6, 4)) ##设置图表的大小,能够省略,使用默认大小 plt.scatter(x01,y01,linewidth=1,label='linear') ##绘制散点图,label为图例 plt.plot(x02,y02,label='scatter') ##绘制折线图,这里会与折线图显示在同一张图表中 plt.title('Square numbers') ##设置图表的标题 plt.xlabel('Value',fontsize=14) ##设置图标x轴的标签 plt.ylabel('Value of numbers',fontsize=14) ##设置图标y轴的标签 plt.axis([-math.pi*2,math.pi*2,-100,100]) ##设置坐标轴的显示区域 ##plt.xlim(-math.pi*2,math.pi*2) ##设置x轴的显示区域 ##plt.ylim(-100,100) ##设置y轴的显示区域 plt.legend() ##显示图例 plt.show() ##显示绘图 ##plt.savefig('f01.svg',format='svg') ##保存绘图
subplots()函数的基本用法是f, ax = plt.subplots(ncols=列数, nrows=行数[, figsize=图片大小, ...]),其中f为所绘制的总图表,ax为所绘制的子图标,能够经过下标调用。svg
x = np.arange(0.01, 10, 0.01) y1 = 2*x - 6 y2 = np.log(x) y3 = np.exp(x) y4 = np.sin(x) f, ax = plt.subplots(ncols=2, nrows=2, figsize=(8, 6)) ax[0, 0].plot(x, y1) ax[0, 0].set_title("Linear") ax[0, 1].plot(x, y2) ax[0, 1].set_title("Log") ax[1, 0].plot(x, y3) ax[1, 0].set_title("Exp") ax[1, 1].plot(x, y4) ax[1, 1].set_title("Sin")
subplot()函数的基本用法为:pyplot.subplot(nrows, ncols, index, 其余参数),而后再使用plt.plot()函数绘图。函数
x = np.arange(0.01, 10, 0.01) y1 = 2*x - 6 y2 = np.log(x) y3 = np.exp(x) y4 = np.sin(x) plt.figure(figsize=(8,6)) plt.subplot(221) ##也能够写做subplot(2,2,1) plt.plot(x, y1,label='Linear') plt.subplot(222) plt.plot(x, y2, label='Log') plt.subplot(223) plt.plot(x, y3, label='Exp') plt.subplot(224) plt.plot(x, y4, label='sin') plt.show()