matplotlib的使用——picture in picture画中画的使用

画中画显示的方法

画中画显示呢,常常需要用到fig.add_axes()函数,其一共需要传入一个矩阵,矩阵中包含4个参数,分别为[left,bottom,width,height]。

fig.add_axes()的使用方式

fig.add_axes()函数传入参数的方式为:

ax1 = fig.add_axes([left,bottom,width,height])

其中left,bottom,width,height均代表百分比,代表其占整个图像的百分比。
left为坐标轴最左侧举例边缘的百分比;
bottom为坐标轴最下侧举例边缘的百分比;
width代表左右坐标轴的距离;
height代表上下坐标轴的距离;
在这里插入图片描述

应用示例

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()

x = [1,2,3,4,5,6,7]
y = [7,6,5,4,3,2,1]

left,bottom,width,height = 0.1,0.1,0.8,0.8

# 添加最大的图像
ax1 = fig.add_axes([left,bottom,width,height])
ax1.plot(x,y,'r')
ax1.set_title("ax1")

# 添加第一幅画中画
left,bottom,width,height = 0.15,0.15,0.3,0.3
ax2 = fig.add_axes([left,bottom,width,height])
ax2.plot(x,y,'r')
ax2.set_title("ax2")

# 添加第二幅画中画
left,bottom,width,height = 0.55,0.55,0.3,0.3
ax3 = fig.add_axes([left,bottom,width,height])
ax3.plot(x,y,'r')
ax3.set_title("ax3")

plt.show()

实现结果为

在这里插入图片描述

posted @ 2021-12-13 10:11  wuyuan2011woaini  阅读(97)  评论(0编辑  收藏  举报