add_axes为新增子区域,该区域可以座落在figure内任意位置,且该区域可任意设置大小。
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,6))
x = [2,4,6,8,10]
y = [20,40,55,80,110]
#ax1 = fig.add_axes([left, bottom, width, height])
ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])#主图
ax2 = fig.add_axes([0.2, 0.6, 0.25, 0.25])#子图
#分别绘图,并设置属性
ax1.plot(x, y, 'b')
ax1.set_title('main picture')
ax2.plot(x, y, 'r')
# ax2.set_title('secondary picture')
plt.show()
显示结果:
ax = fig.add_axes([left, bottom, width, height])的理解:
(1)rect:新轴的尺寸[左、底、宽、高]。所有数量都以图形宽度和高度的分数表示。
(2)ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])表示ax1是从figure 10%的位置开始绘制, 宽高是figure的80%。
(3)ax2 = fig.add_axes([0.2, 0.6, 0.25, 0.25])表示ax2是从figure左侧20%,距离底部60%,宽高是25%绘制的。
(4)ax = fig.add_axes([0,0,1,1])表示全部绘制在figure上面。
参考资料:https://matplotlib.org/3.3.4/api/_as_gen/matplotlib.figure.Figure.html