matplotlib.pyplot一图多张表
1. plt.subplot(x, y, n)和plt.subplots(x, y)
【plt.subplot(x, y, n)】其中 x 代表生成表的行数,y 代表生成表的列数,n 代表第几张表
比如plt.subplot(3, 4, n)生成12张表
import matplotlib.pyplot as plt
# 循环创建12张表
for i in range(1, 13):
plt.subplot(3, 4, i)
plt.show()
或者使用plt.subplots(3, 4)
import matplotlib.pyplot as plt
# 直接创建12张表
plt.subplots(3,4)
plt.show()
二者结果一致:
2. 创建一图多表
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
# 使用系统文字,用来正常显示中文标签
plt.rcParams['font.sans-serif'] = ['SimHei']
# x 轴数据
x = [1, 2, 3, 4]
# 创建第一张表数据位置在图的(1,1)位置
plt.subplot(2, 2, 1)
# y 轴数据
y = [8, 7, 6, 5]
plt.plot(x, y, c='r')
# 单张表标题
plt.title("表1")
# 创建第二张表数据位置在图的(1,2)位置
plt.subplot(2, 2, 2)
y = [2, 4, 6, 8]
plt.plot(x, y, c='b')
# 单张表标题
plt.title("表2")
# 创建第三张表数据位置在图的(2,1)位置
plt.subplot(2, 2, 3)
y = [3, 4, 5, 7]
plt.plot(x, y, c='y')
# 单张表标题
plt.title("表3")
# 创建第四张表数据位置在图的(2,2)位置
plt.subplot(2, 2, 4)
# 这里更改了表4 x 轴数据
x = [2, 4, 6, 8]
y = [9, 7, 3, 1]
plt.plot(x, y, c='g')
# 单张表标题
plt.title("表4")
# 多表图标题
plt.suptitle("四张表")
# 绘图
plt.show()
运行结果: