Python 在matplotlib中自动偏移寄生轴

Python 在matplotlib中自动偏移寄生轴,python,matplotlib,Python,Matplotlib,我正在使用Matplotlib 3.1和PARSISTATE_轴创建最多5个PARSISTATE y轴的绘图,并且我希望自动分隔Yax,而不是在代码中指定偏移。目前,我必须绘制图形以查看轴是否重叠,然后手动调整图形的宽度,为寄生轴留出正确的空间量,然后手动为每个轴添加偏移,以便它们不会重叠。如果图形中有任何变化,例如DPI或标签中的位数,那么我必须再次检查所有这些 我试着只得到yaxis的bbox,但除了整个情节的bbox之外,我似乎什么也得不到。在不知道轴的宽度的情况下,似乎无法计算这些轴的正

我正在使用Matplotlib 3.1和PARSISTATE_轴创建最多5个PARSISTATE y轴的绘图,并且我希望自动分隔Yax,而不是在代码中指定偏移。目前,我必须绘制图形以查看轴是否重叠,然后手动调整图形的宽度,为寄生轴留出正确的空间量,然后手动为每个轴添加偏移,以便它们不会重叠。如果图形中有任何变化,例如DPI或标签中的位数,那么我必须再次检查所有这些

我试着只得到yaxis的bbox,但除了整个情节的bbox之外,我似乎什么也得不到。在不知道轴的宽度的情况下,似乎无法计算这些轴的正确间距

#Example from the matplotlib website:
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt

host = host_subplot(111, axes_class=AA.Axes)
plt.subplots_adjust(right=0.75)

par1 = host.twinx()
par2 = host.twinx()

offset = 60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right",
                                axes=par2,
                                offset=(offset, 0))

par1.axis["right"].toggle(all=True)
par2.axis["right"].toggle(all=True)

host.set_xlim(0, 2)
host.set_ylim(0, 2)

host.set_xlabel("Distance")
host.set_ylabel("Density")
par1.set_ylabel("Temperature")
par2.set_ylabel("Velocity")

p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity")

par1.set_ylim(0, 4)
par2.set_ylim(1, 65)

host.legend()

host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())
par2.axis["right"].label.set_color(p3.get_color())

plt.show()