import matplotlib.pyplot as plt | |
import random | |
""" | |
subplot函数: | |
直接指定划分方式和位置,它可以将一个绘图区域划分为N个子图,每个subplot函数只能绘制一个子图 | |
plt.subplot(*args, **kwargs) | |
注意事项: | |
每绘制一个子图表都要调用一次subplot函数 | |
绘图区域的位置编号 | |
""" | |
# 利用subplot函数来绘制子图 | |
# 绘制第一个子图 | |
plt.subplot(2, 2, 1) # 第一个绘图区 -- 两行两列,第一个位置 | |
plt.plot([1, 2, 3, 4, 5], [random.randint(1, 10) for i in range(5)]) | |
# 绘制第二个子图 | |
plt.subplot(222) # 第二个绘图区,简写形式 -- 两行两列,第二个位置 | |
plt.plot([1, 2, 3, 4, 5], [random.randint(1, 10) for i in range(5)], 'ro') | |
# 绘制第三个子图 | |
plt.subplot(2, 1, 2) # 第三个绘图区 -- 两行一列,在第二行 | |
x = [1, 2, 3, 4, 5] | |
y = [random.randint(10, 50) for i in range(5)] | |
plt.bar(x, y) | |
plt.show() | |