add_subplot()--matplotlib

1. add_subplot()函数

1.1 函数功能

Add an Axes to the figure as part of a subplot arrangement.

在画布中增加轴域

1.2 函数语法:

add_subplot(nrows, ncols, index, **kwargs)

1.3 函数参数

1.3.1 nrows, ncols, index

子图的位置由以下三个参数决定:

Three integers (nrows, ncols, index). 
The subplot will take the index position on a grid with nrows rows and ncols columns. 
index starts at 1 in the upper left corner and increases to the right. 
index can also be a two-tuple specifying the (first, last) indices (1-based, and including last) of the subplot, 
e.g., fig.add_subplot(3, 1, (1, 2)) makes a subplot that spans the upper 2/3 of the figure. 

参数nrows与ncols决定画布被划分为nrows*ncols几部分。索引index从左上角开始,向右边递增。
索引也可以指定为二元数组,表示图表占据位置为:从索引的第1个参数到第2个参数的位置。
例如: fig.add_subplot(3, 1, (1, 2)),表示子图占据的位置为将画布三等分后的前两份位置。

import matplotlib.pyplot as plt
import numpy as np

x1 = np.linspace(-2 * np.pi, 2 * np.pi, 500)
y1 = np.sin(x1)

plt.subplot(1, 3, 1)
plt.plot(x1, y1)

plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

x1 = np.linspace(-2 * np.pi, 2 * np.pi, 500)
y1 = np.sin(x1)

plt.subplot(1, 3, (1,2))
plt.plot(x1, y1)

plt.show()

在这里插入图片描述

1.3.2 projection

projection{None, 'aitoff', 'hammer', 'lambert', 'mollweide', 'polar', 
'rectilinear', str}, optional

可选参数。当取值为polar表示绘制极线图。


import matplotlib.pyplot as plt
import numpy as np

x1 = np.linspace(-2 * np.pi, 2 * np.pi, 500)
y1 = np.sin(x1)

plt.subplot(1, 3, (1,2), projection='polar')
plt.plot(x1, y1)

plt.show()

在这里插入图片描述

1.3.3 polar

polar  bool, default: False
If True, equivalent to projection='polar'.

布尔值类型,默认取值为:FALSE。当取值为True,相当于projection=‘polar’

import matplotlib.pyplot as plt
import numpy as np

x1 = np.linspace(-2 * np.pi, 2 * np.pi, 500)
y1 = np.sin(x1)

plt.subplot(1, 3, (1, 2), polar=True)
plt.plot(x1, y1)

plt.show()

在这里插入图片描述

2. add_subplot 与subplot

目前理解到的为:

  1. subplot方法属于API绘图,而add_subplot方法为面向对象绘图
  2. 使用subplot方法自动生产画布与轴域,而add_subplot方法需要先添加画布figure,再在画布上添加轴域,add_subplot返回的即为子图的轴域
  3. 当绘制的子图之间出现遮挡,subplot方法会绘制代码位置靠后的图形,而add_subplot则会绘制所有图形,只是出现遮挡。
import matplotlib.pyplot as plt
import numpy as np

x1 = np.linspace(-2 * np.pi, 2 * np.pi, 500)
y1 = np.sin(x1)
x2 = np.arange(-3, 3.5, 0.5)
y2 = np.power(x2, 2)

plt.subplot(1, 3, (1, 2))
plt.plot(x1, y1)

plt.subplot(2, 2, 3)
plt.scatter(x2, y2)

plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure('add_subplot')

x1 = np.linspace(-2 * np.pi, 2 * np.pi, 500)
y1 = np.sin(x1)
x2 = np.arange(-3, 3.5, 0.5)
y2 = np.power(x2, 2)

sw1 = fig.add_subplot(1, 3, (1, 2))
sw1.plot(x1, y1)

sw2 = fig.add_subplot(2, 2, 3)
sw2.scatter(x2, y2, s=20, facecolor='y')

plt.show()

在这里插入图片描述

参考网站:https://matplotlib.org/stable/api/figure_api.html#matplotlib.figure.Figure.add_subplot

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值