在多y轴图matplotlib中反转y轴

时间:2018-03-14 16:54:55

标签: python matplotlib axes

我知道plt.gca()。invert_yaxis()在正常的matplotlib图上反转y轴,但我无法弄清楚如何获得最右边y轴的当前轴(不是主要的y轴)轴),所以我不能反转那个轴..

我的代码基于下面链接的问题,但我会在这里重写。我想要的是'par2(下例中的速度)'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_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")

我想在y轴标记的速度下反转。但我不承诺如何获取轴对象(?)来执行.invert_yaxis()

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

#plt.savefig("Test")

multiple axis in matplotlib with different scales

0 个答案:

没有答案