matplotlib画多个图(着重讲图片的位置如何摆放)

前言

所有的图都是基于函数 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,第一个是行,第二个是列,且在第一个位置作图。

  • 2x2显然是4个位置:

在这里插入图片描述

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

在这里插入图片描述

使用元组确定位置

plt.subplot(n_row, n_col, (first, last))

其中:
n_row:行数
n_col:列数
(first, last):表示左上角位置和右下角位置

示例1

评论区中有同学问,3x3的图,在(4,5,7,8)位置画图,该怎么画?
这用上面的方法是不行的,位置必须传一个二元组
在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np


x = np.linspace(-10, 10, 100)
y = [2*i + 1 for i in x]

plt.figure(1)
plt.subplot(3, 3, (1, 3))  # 左上角是位置1,右下角是位置3
plt.plot(x, y)

plt.subplot(3, 3, (4, 8))  # 左上角是位置4,右下角是位置8
plt.plot(x, y)

plt.subplot(3, 3, (6, 9))  # 左上角是位置6,右下角是位置9
plt.plot(x, y)
plt.show()

在这里插入图片描述
解释:
在这里插入图片描述

  • 6
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值