matplotlib绘制子图 subplot

本文详细介绍使用Matplotlib绘制子图的方法,包括subplot(), subplot2grid(), GridSpec()等函数的使用,以及如何通过GridSpec调整子图布局,适合于需要进行复杂图表布局的数据可视化项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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中文手册

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值