您可以根据宽度和子批次参数计算图形的高度。下面假设子图的宽度相等,并且只有一行子图,并且它们都有一个定义的方面(即不是“自动”)。
import numpy as np
import matplotlib.pyplot as plt
fig, (ax1,ax2) = plt.subplots(1,2, figsize=(8.5,8.5))
for ax in (ax1, ax2):
ax.set_aspect("equal")
ax1.plot([1,3], [4,6], marker="o")
ax2.plot([26,84], [64,-13], marker="s")
def resize(axes):
# this assumes a fixed aspect being set for the axes.
for ax in axes:
if ax.get_aspect() == "auto":
return
elif ax.get_aspect() == "equal":
ax.set_aspect(1)
fig = axes[0].figure
s = fig.subplotpars
n = len(axes)
axw = fig.get_size_inches()[0]*(s.right-s.left)/(n+(n-1)*s.wspace)
r = lambda ax: np.diff(ax.get_ylim())[0]/np.diff(ax.get_xlim())[0]*ax.get_aspect()
a = max([r(ax) for ax in axes])
figh = a*axw/(s.top-s.bottom)
fig.set_size_inches(fig.get_size_inches()[0],figh)
resize((ax1,ax2))
plt.show()