matplotlib绘制子图 subplot

1.绘制子图

使用subplot() 方法,subplot()方法传入numrows,numcols,fignum参数:fignum范围是 1 到numrows*numcols

import matplotlib.pyplot as plt
import numpy as np

x=np.linspace(0.0, 5.0, 50)
y1 = np.sin(x)
y2 = np.exp(x) * np.cos(x)

figures = plt.figure(1)
axeses = [plt.subplot(221), plt.subplot(224)]

plt.subplot(221)
lines=plt.plot(x, y1, x, y2)
plt.subplot(224)
plt.plot(x)

plt.setp(figures, facecolor='c')
plt.setp(lines, linestyle='dashdot')

plt.show()

另外,绘制的子图也可以不在矩形网格上, 使用axes()命令, 可以在指定位置为axes([left,bottom,width,height])绘图,其中所有参数值都为小数(0 to 1) 坐标,即是与原图的比值。

import matplotlib.pyplot as plt
import numpy as np
# 创建数据

t = np.arange(0.0, 10.0, 0.01)
r = np.exp(-t[:1000]/0.05) # impulse response
x = np.random.randn(len(t))
s = np.convolve(x, r)[:len(x)]*dt # colored noise

# 默认主轴图axes是subplot(111)
plt.plot(t, s)
plt.axis([0, 1, 1.1*np.amin(s), 2*np.amax(s)])

#内嵌图
a = plt.axes([.65, .6, .2, .2], facecolor='y')
n, bins, patches = plt.hist(s, 400, normed=1)
plt.title('Probability')
plt.xticks([])
plt.yticks([])

#另外一个内嵌图
a = plt.axes([0.2, 0.6, .2, .2], facecolor='y')
plt.plot(t[:len(r)], r)
plt.title('Impulse response')
plt.xlim(0, 0.2)
plt.xticks([])
plt.yticks([])

plt.show()

2.绘制子图详解

MATLAB和pyplot, 都具有当前图框和当前子图的概念。此前所有绘图命令均适用于当前子图。 函数gca()"get current axes"返回当前子图(一个matplotlib.axes.Axes实例), 而gcf()"get current figure"返回当前图框(一个matplotlib.figure.Figure实例).

3.通过GridSpec来定制Subplot布局

GridSpec指定子图所放置的几何网格。

3.1 subplotgrid 示例

subplot2grid类似于“pyplot.subplot”,但是它从0开始索引。以下两句效果相同。

ax = plt.subplot2grid((2,2),(0, 0))
ax = plt.subplot(2,2,1)

subplot2grid 创建跨多个单元格的子图

ax1 = plt.subplot2grid((3,3), (0,0), colspan=3)
ax2 = plt.subplot2grid((3,3), (1,0), colspan=2)
ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3,3), (2, 0))
ax5 = plt.subplot2grid((3,3), (2, 1))

3.2 使用GridSpec 和 SubplotSpec

可以显式创建GridSpec并使用它们来创建子图

ax = plt.subplot2grid((2,2),(0, 0))

相当于

import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 2)
ax = plt.subplot(gs[0, 0])

gridspec提供类似数组的索引来返回SubplotSpec实例,所以也可以使用切片(slice)来合并单元格。

gs = gridspec.GridSpec(3, 3)
ax1 = plt.subplot(gs[0, :])
ax2 = plt.subplot(gs[1,:-1])
ax3 = plt.subplot(gs[1:, -1])
ax4 = plt.subplot(gs[-1,0])
ax5 = plt.subplot(gs[-1,-2])

3.3 调整GridSpec布局

当一个 GridSpec 实例显示使用,可以调整从gridspec创建的子图的布局参数。

gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.05, right=0.48, wspace=0.05)

这类似于tosubplots_adjust(之前的子图列表参数设置),但它只影响从给定GridSpec创建的子图。举例:

gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.05, right=0.48, wspace=0.05)
ax1 = plt.subplot(gs1[:-1, :])
ax2 = plt.subplot(gs1[-1, :-1])
ax3 = plt.subplot(gs1[-1, -1])

gs2 = gridspec.GridSpec(3, 3)
gs2.update(left=0.55, right=0.98, hspace=0.05)
ax4 = plt.subplot(gs2[:, :-1])
ax5 = plt.subplot(gs2[:-1, -1])
ax6 = plt.subplot(gs2[-1, -1])

参考

matplotlib中文手册

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
matplotlib中,我们可以使用不同的方法来绘制子图。一种方法是使用面向对象的方式,另一种方法是使用函数式的方式。 使用面向对象的方式创建子图,首先需要创建一个figure对象,例如`fig = plt.figure()`,然后在图像布局中绘制子图。例如,使用`fig.add_subplot()`方法可以指定子图的布局。例如,`ax1 = fig.add_subplot(221)`表示创建一个2行2列的图像布局,并从左往右第1个子图开始绘图。接下来,我们可以使用`ax1.plot()`方法在该子图绘制数据。依次类推,可以创建多个子图并在每个子图绘制不同的数据。最后,通过`plt.show()`显示绘制的图像。一个例子如下所示: ```python import numpy as np import matplotlib.pyplot as plt x = np.arange(100) fig = plt.figure(figsize=(12, 6)) ax1 = fig.add_subplot(221) ax1.plot(x, x) ax2 = fig.add_subplot(222) ax2.plot(x, -x) ax3 = fig.add_subplot(223) ax3.plot(x, x ** 2) ax4 = fig.add_subplot(224) ax4.plot(-x, x ** 2) plt.show() ``` 另一种方法是使用函数式的方式创建子图。在这种方式下,我们可以直接使用`plt.subplots()`方法来创建多个子图。该方法会返回一个包含所有子图对象的列表。然后,我们可以通过索引来访问每个子图对象,并在其中绘制数据。一个例子如下所示: ```python import numpy as np import matplotlib.pyplot as plt x = np.arange(100) fig, axs = plt.subplots(2, 2, figsize=(12, 6)) axs<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python使用matplotlib:subplot绘制多个子图的示例](https://download.csdn.net/download/weixin_38730129/13706968)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [在matplotlib中创建子图的多种方式](https://blog.csdn.net/itanders/article/details/88763245)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值