要使用matplotlib使每个轴上具有不同范围的子图具有相同的图形大小,可以按照以下步骤进行操作:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(nrows=2, ncols=2)
axs[0, 0].plot(x1, y1)
axs[0, 1].plot(x2, y2)
axs[1, 0].plot(x3, y3)
axs[1, 1].plot(x4, y4)
axs[0, 0].set_xlim(xmin, xmax)
axs[0, 0].set_ylim(ymin, ymax)
axs[0, 1].set_xlim(xmin, xmax)
axs[0, 1].set_ylim(ymin, ymax)
axs[1, 0].set_xlim(xmin, xmax)
axs[1, 0].set_ylim(ymin, ymax)
axs[1, 1].set_xlim(xmin, xmax)
axs[1, 1].set_ylim(ymin, ymax)
plt.subplots_adjust(hspace=0.5, wspace=0.5)
完整的代码示例:
import matplotlib.pyplot as plt
# 创建画布和轴对象
fig, axs = plt.subplots(nrows=2, ncols=2)
# 在每个子图上绘制数据
axs[0, 0].plot(x1, y1)
axs[0, 1].plot(x2, y2)
axs[1, 0].plot(x3, y3)
axs[1, 1].plot(x4, y4)
# 设置每个轴的范围
axs[0, 0].set_xlim(xmin, xmax)
axs[0, 0].set_ylim(ymin, ymax)
axs[0, 1].set_xlim(xmin, xmax)
axs[0, 1].set_ylim(ymin, ymax)
axs[1, 0].set_xlim(xmin, xmax)
axs[1, 0].set_ylim(ymin, ymax)
axs[1, 1].set_xlim(xmin, xmax)
axs[1, 1].set_ylim(ymin, ymax)
# 设置每个轴的相同图形大小
plt.subplots_adjust(hspace=0.5, wspace=0.5)
# 显示图形
plt.show()
这样,每个轴上具有不同范围的子图将具有相同的图形大小。
领取专属 10元无门槛券
手把手带您无忧上云