subplot2grid()--matplotlib

1. 函数功能

在规则网格内的指定位置创建子区

2. 函数语法

matplotlib.pyplot.subplot2grid(shape, loc, rowspan=1, colspan=1, fig=None, **kwargs)

3. 函数参数与示例

参数含义
shape(int, int) 网格布局
loc(int, int)绘制图形的位置,初始位置(0,0),表示第一行第一列
rowspan整数,默认为1;图形跨越的行数
colspan整数,默认为1;图形跨越的列数
fig可选参数,放置subplot子图的画布,默认为当前画布
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.linspace(0, 8, 500)
y1 = x
y2 = np.sin(x)
y3 = np.power(x, 2)
y4 = np.cos(4 * x)

plt.subplot2grid(shape=(3, 3), loc=(0, 0), colspan=3)
plt.plot(x, y2)
plt.subplot2grid(shape=(3, 3), loc=(1, 0), colspan=2)
plt.plot(x, y3, c='r')
plt.subplot2grid(shape=(3, 3), loc=(1, 2), rowspan=2)
plt.plot(x, y1, c='y')
plt.subplot2grid(shape=(3, 3), loc=(2, 0), colspan=1)
plt.plot(x, y4, c='g')
plt.subplot2grid(shape=(3, 3), loc=(2, 1), colspan=1, polar=True)
plt.plot(x, y1, c='m')
plt.show()

在这里插入图片描述

4. 与subplot函数对比

上述跨区绘制的图形也可以通过subplot函数实现,代码如下。

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False

x = np.linspace(0, 8, 500)
y1 = x
y2 = np.sin(x)
y3 = np.power(x, 2)
y4 = np.cos(4 * x)

plt.subplot(3, 3, (1, 3))
plt.plot(x, y2)
plt.subplot(3, 3, (4, 5))
plt.plot(x, y3, c='r')
plt.subplot(3, 3, (6, 9))
plt.plot(x, y1, c='y')
plt.subplot(3, 3, 7)
plt.plot(x, y4, c='g')
plt.subplot(3, 3, 8, polar=True)
plt.plot(x, y1, c='m')
plt.show()

在这里插入图片描述
目前来看subplot()与subplot2grid()函数均可以实现跨区绘制图形,只是subplot指定绘制位置的时候使用每个图形的索引(从1开始,从左向右增加);而subplot2gird()函数,运用位置索引(行、列索引均从0开始),然后指定横向和纵向跨区数量。对于划分行列较多的绘图subplot2grid()函数使用位置索引和跨越区域数量的方式会更方便。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值