Matplotlib图中图(在一个图片中嵌入多个小坐标系)
一. 创建数据
x = [1, 2, 3, 4, 5, 6, 7]
y = [1, 3, 4, 2, 5, 8, 6]
二. 绘制大图
- 确定大图左下角在整个figure中的位置。假设figure为10x10,创建左下角在(1, 1)、宽为8、高为8的坐标系
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
- 利用 ax = fig.add_axes(position) 将大图加到figure中,在使用add_axes时,传递的参数中,前两个元素为axes的左下角在fig的图像坐标上的位置,后两个元素指axes在fig的图像坐标上x方向和y方向的长度。fig的图像坐标称为Figure坐标,储存在为fig.transFigure当中。取自link: add_axes与各种对象
fig = plt.figure()
ax1 = fig.add_axes([left, bottom, width, height])
ax1.plot(x, y, color = 'red')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_title('title')
plt.show()

文章图片
三. 绘制小图 法一:用add_axes的方式,新建起点在左上角,高和宽为2.5的坐标系
left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
ax2 = fig.add_axes([left, bottom, width, height])
ax2.plot(y, x, 'b')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_title('title inside 1')

文章图片
法二:直接利用 plt.axes() 绘制坐标系
plt.axes([0.6, 0.2, 0.25, 0.25])
plt.plot(y[::-1], x, 'g') # 注意对y进行了逆序处理
plt.xlabel('x')
plt.ylabel('y')
plt.title('title inside 2')

文章图片
参考:
matplotlib多图合并显示教程
【Matplotlib图中图(在一个图片中嵌入多个小坐标系)】fig, axes =plt.subplots()绘制多个子图
C.f. plt.axis()和plt.axes()