前言
所有的图都是基于函数y = x^2 ,但图像不是重点,如何作图才是重点。 希望你能耐心看到最后。
1.

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1, 1, 100)
print(type(x))
y = x ** 2
plt.figure(1)
plt.subplot(2, 2, 1)
plt.plot(x, y)
plt.show()
np.linspace(-1, 1, 100): 获取[-1,1]之间的100个点,以numpy.ndarray形式返回 figure(1): 创建一张画布,并且编号为1 plt.subplot(2, 2, 1): 将画布分为2×2,第一个是行,第二个是列,且在第一个位置作图。

2.
与上面一样,这次将一个散点图放在4 位置。 
x = np.linspace(-1, 1, 100)
print(type(x))
y = x ** 2
plt.figure(1)
plt.subplot(2, 2, 1)
plt.plot(x, y)
plt.subplot(2, 2, 4)
plt.scatter(x, y)
plt.show()
plt.subplot(2, 2, 4): 将画布分为2×2,在第四个位置作图。 plt.scatter(x, y): 以x为横轴坐标、y为纵轴坐标,绘制散点图。
- 粉色的是画在第四个位置的图

3.
有了上面的2点,这里理解4张子图就很轻松。 
x = np.linspace(-1, 1, 100)
print(type(x))
y = x ** 2
plt.figure(1)
plt.subplot(2, 2, 1)
plt.plot(x, y, label='1')
plt.legend(loc=2)
plt.subplot(2, 2, 2)
plt.plot(x, y, label='2')
plt.legend(loc=2)
plt.subplot(2, 2, 3)
plt.plot(x, y, label='3')
plt.legend(loc=2)
plt.subplot(2, 2, 4)
plt.plot(x, y, label='4')
plt.legend(loc=2)
plt.show()
plt.plot(x, y, label='4'): 绘制折线图,图例为4。 plt.legend(loc=2): 图例的位置设为2。
图例的位置
loc=1 在子图的右上角loc=2 在子图的左上角loc=3 在子图的左下角loc=4 在子图的右下角 
x = np.linspace(-1, 1, 100)
print(type(x))
y = x ** 2
plt.figure(1)
plt.subplot(2, 2, 1)
plt.plot(x, y, label='1')
plt.legend(loc=1)
plt.subplot(2, 2, 2)
plt.plot(x, y, label='2')
plt.legend(loc=2)
plt.subplot(2, 2, 3)
plt.plot(x, y, label='3')
plt.legend(loc=3)
plt.subplot(2, 2, 4)
plt.plot(x, y, label='4')
plt.legend(loc=4)
plt.show()
4.

x = np.linspace(-1, 1, 100)
print(type(x))
y = x ** 2
plt.figure(1)
plt.subplot(2, 2, 1)
plt.plot(x, y)
plt.subplot(2, 2, 2)
plt.plot(x, y)
plt.subplot(2, 1, 2)
plt.plot(x, y)
plt.show()
plt.subplot(2, 2, 1): 把画布分为2x2,且在1位置。 plt.subplot(2, 2, 2): 把画布分为2x2,且在2位置。 plt.subplot(2, 1, 2): 把画布分为2x1,且在2位置。
- 或许第三个图是大家最迷惑的地方,画个图大家就明白了:

5.
如果看懂了4.,那么接下来的应该也能理解 
x = np.linspace(-1, 1, 100)
print(type(x))
y = x ** 2
plt.figure(1)
plt.subplot(2, 2, 1)
plt.plot(x, y)
plt.subplot(2, 2, 3)
plt.plot(x, y)
plt.subplot(1, 2, 2)
plt.plot(x, y)
plt.show()
plt.subplot(2, 2, 1): 把画布分为2x2,且在第一个位置 plt.subplot(2, 2, 3): 把画布分为2x2,且在第三个位置 plt.subplot(1, 2, 2): 把画布分为1x2,且在第二个位置
- 前两个图我再啰嗦一下:
 - 重点来到第三个图:

6.
我们最后来画一下4x4的图巩固一下吧。 px: 表示第x张图 
虽然很多,但是别怕,我都会讲的。
x = np.linspace(-1, 1, 100)
print(type(x))
y = x ** 2
plt.figure(1)
plt.subplot(4, 4, 1)
plt.plot(x, y, label='p1')
plt.legend(loc=2)
plt.subplot(4, 4, 2)
plt.plot(x, y, label='p2')
plt.legend(loc=2)
plt.subplot(4, 4, 3)
plt.plot(x, y, label='p3')
plt.legend(loc=2)
plt.subplot(4, 4, 4)
plt.plot(x, y, label='p4')
plt.legend(loc=2)
plt.subplot(4, 1, 2)
plt.plot(x, y, label='p5')
plt.legend(loc=2)
plt.subplot(2, 2, 3)
plt.plot(x, y, label='p6')
plt.legend(loc=2)
plt.subplot(4, 2, 6)
plt.plot(x, y, label='p7')
plt.legend(loc=2)
plt.subplot(4, 4, 15)
plt.plot(x, y, label='p8')
plt.legend(loc=2)
plt.subplot(4, 4, 16)
plt.plot(x, y, label='p9')
plt.legend(loc=2)
plt.show()
6.1: p1~p4, p8,p9
我们可以发现,这张画布的第一行都是一样的图,倒数两个也是同样的图(因为用的是同样的分割方法4x4)。 对于4x4的画布,这4个子图就相当于1x1的图。 plt.subplot(4, 4, x): 将画布分为4x4,并且在第x 个位置 
6.2:p5

6.3:p6

6.4:p7

|