使用twinx的多个y轴matplotlib图:如何移动y轴比例因子以使其各自的轴对齐?

时间:2020-04-13 09:04:37

标签: python matplotlib plot twinx

以下代码针对比例因子x10 ^ -4,x10 ^ -10和x10 ^ 13的数据生成了多个y轴图。

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))

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

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

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

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.draw()
plt.show()

Ouput from code

如输出图像中所示,三个数据集的比例因子不在其各自的y轴上方,而是在图的右侧显示了一个比例因子,其他两个大概埋在其下方。

使用twinx绘制多轴时,是否可以将比例因子移动到它们所属的y轴上方?

0 个答案:

没有答案