1

I know about the plt.gca().invert_yaxis() to invert the y axis on a normal matplotlib figure, but I cannot figure out how to get the current axis of the far right y axis (not the main host y axis), and so I cannot invert that axis..

The code I have is based on the question linked below, but I will rewrite here. What I want is the 'par2 (velocity in the example below) ' y axis to be inverted

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

I would like this below y axis labelled velocity to be inverted. But I do not undertand how to get the axis object(?) for it to do the .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

pwprnt
  • 521
  • 3
  • 9
  • 27
  • 1
    Any reason your are using `axisartist` and not normal axes as in [this answer](https://stackoverflow.com/a/45925049/4124317)? – ImportanceOfBeingErnest Mar 14 '18 at 17:00
  • 1
    In any case, it seems you just want to call `par2.invert_yaxis()`. – ImportanceOfBeingErnest Mar 14 '18 at 17:05
  • @ImportanceOfBeingErnest Ok I was doing par2.axis.invert_yaxis() and not your par2.invert_yaxis(). I don't know how axes objects work. I can't find consistent documentation on how to build a figure "from the ground up" , eg. setting a figure space, some people do not set axes at the same time as when they set the figure, if at all. i've found at example pages https://matplotlib.org/gallery/pyplots/pyplot_simple.html#sphx-glr-gallery-pyplots-pyplot-simple-py but only when I want to change something I have to search for specific examples for each case, and it gets really messy. thanks – pwprnt Mar 14 '18 at 17:14
  • Matplotlib has a really good documentation. It also has a lot of examples on the page. You may always check the types of objects in use to find out which methods are available. – ImportanceOfBeingErnest Mar 14 '18 at 17:21

0 Answers0