Matplotlib多图布局:让你的数据图不再‘挤’眉弄眼,井然有序笑开花!

在这里插入图片描述

1.导包

# 导包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# 如果浏览器不显示图片,就需要加上这句话
%matplotlib inline

# 让图片中可以显示中文
plt.rcParams['font.sans-serif'] = "SimHei"
# 让图片中可以显示负号
plt.rcParams["axes.unicode_minus"] = False

# 支持svg矢量图
%config Inlinebackend.figure_format = "svg"

2.subplot()函数

# 2行2列子图
fig = plt.figure(figsize=(8,5))

x = np.linspace(-np.pi,np.pi,30)
y = np.sin(x)

# 子图1
ax1 = plt.subplot(2,2,1)   # 2行2列中的第1个图
ax1.plot(x,y)
ax1.set_title("子图1")

# 子图2
ax2 = plt.subplot(2,2,2)   # 2行2列中的第2个图
ax2.plot(x,y)
ax2.set_title("子图2")

# 子图3
ax3 = plt.subplot(2,2,3)   # 2行2列中的第3个图
ax3.plot(x,y)
ax3.set_title("子图3")

# 子图4
ax4 = plt.subplot(2,2,4)   # 2行2列中的第4个图
ax4.plot(x,y)
ax4.set_title("子图4")

# 自动调整布局
fig.tight_layout()

在这里插入图片描述

# 第二种方式
plt.figure(figsize=(8,5))

x = np.linspace(-np.pi,np.pi,30)
y = np.sin(x)

# 子图1
ax1 = plt.subplot(2,2,1)
ax1.plot(x,y)

# 子图2
ax2 = plt.subplot(2,2,2)
ax2.plot(x,y)

# 子图3
ax3 = plt.subplot(2,1,2)   # 2行1列中的第2行
ax3.plot(x,np.sin(x*x))
[<matplotlib.lines.Line2D at 0x136d47df410>]

在这里插入图片描述

3.使用 subplots() 函数

x = np.linspace(0, 2*np.pi)

# 3行3列
fig, ax = plt.subplots(3, 3)
ax1, ax2, ax3 = ax
ax11, ax12, ax13 = ax1
ax21, ax22, ax23 = ax2
ax31, ax32, ax33 = ax3

# fig设置画布大小
fig.set_figwidth(8)
fig.set_figheight(8)

# 第1行
ax11.plot(x, np.sin(x))
ax12.plot(x, np.cos(x))
ax13.plot(x, np.tan(x))

# 第2行
ax21.plot(x, np.tanh(x))
ax22.plot(x, np.cosh(x))
ax23.plot(x, np.sinh(x))

# 第3行
ax31.plot(x, np.sin(x) + np.cos(x))
ax32.plot(x, np.sin(x*x) + np.cos(x*x))
ax33.plot(x, np.sin(x) * np.cos(x))

plt.tight_layout()

在这里插入图片描述

4.图形嵌套

  • add_subplot()函数
fig = plt.figure(figsize=(8,5))

# 子图1
axes1 = fig.add_subplot(1, 1, 1)
axes1.plot([0, 1], [1, 3])

# 子图2:嵌套图
axes2 = fig.add_subplot(2, 2, 1, facecolor="pink")
axes2.plot([1, 3])
[<matplotlib.lines.Line2D at 0x136d69e1490>]

在这里插入图片描述

  • 使用 axes() 函数 和 add_axes() 函数
fig = plt.figure(figsize=(8,5))

# 图1
x = np.linspace(0, 2*np.pi, 30)
y = np.sin(x)

plt.plot(x, y)

# 嵌套图1,使用 axes() 函数
# 参数:[left, bottom, width, height]
axes1 = plt.axes([0.55, 0.55, 0.3, 0.3])
axes1.plot(x, y, color="g")

# 嵌套图2,使用 add_axes() 函数
axes2 = fig.add_axes([0.18, 0.18, 0.3, 0.3])
axes2.plot(x, y, color="r")
[<matplotlib.lines.Line2D at 0x136d7336c10>]

在这里插入图片描述

5.双轴显示

plt.figure(figsize=(6, 4))

x = np.linspace(0, 10, 100)

# 图1
axes1 = plt.gca()   # 得到当前轴域
axes1.plot(x, np.exp(x), c="r")
axes1.set_xlabel("time")
axes1.set_ylabel("exp", c="r")
axes1.tick_params(axis="y", labelcolor="red")

# 图2
axes2 = axes1.twinx()   # 和图1共享x轴
axes2.plot(x, np.sin(x), c="b")
axes2.set_ylabel("sin", c="b")
axes2.tick_params(axis="y", labelcolor="blue")

# 自动调整布局
plt.tight_layout()

在这里插入图片描述

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

腾飞开源

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值