1

Hello I'm trying to make a graph with one Y axis and 4 X axis

I don't want to make separate graphs and was wondering if it is possible to put multiple X axis using the twinx() command.

The problem I'm having is that the final axis I'm trying to add ends up appearing on both sides of the graph and I just want it on the left.

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)

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

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

host.set_xlabel("Y axis")
host.set_ylabel("X axis 1 on the left")
par1.set_ylabel("X axis 2 on the right") par2.set_ylabel("X axis 3 on the right")

par3 = host.twinx()

new_fixed_axis = par3.get_grid_helper().new_fixed_axis
par3.axis["left"] = new_fixed_axis(loc="left",axes=par3,offset=(-60, 0))
par3.axis["left"].toggle(all=True)

par3.set_ylabel("X axis 4 on the left")

p1, = host.plot([1,2,3,4],[1,2,3,4], label="1")
p2, = par1.plot([1,2,3,4],[2,2,2,2], label="2")
p3, = par2.plot([1,2,3,4],[3,3,2,1], label="3")
p4, = par3.plot([1,2,3,4],[1,1,2.5,3.5], label="4")

plt.show()

1 Answer 1

1

Just set the visibility to False.

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(bottom=0.1, left=0.2, right=0.8, top=0.9)

par1 = host.twinx()
par2 = host.twinx()
par3 = host.twinx()
par3.axis["right"].set_visible(False)

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

host.set_xlabel("Y axis")
host.set_ylabel("X axis 1 on the left")
par1.set_ylabel("X axis 2 on the right")
par2.set_ylabel("X axis 3 on the right")
par3.set_ylabel("X axis 4 on the left")


new_fixed_axis = par3.get_grid_helper().new_fixed_axis
par3.axis["left"] = new_fixed_axis(loc="left",axes=par3,offset=(-60, 0))


p1, = host.plot([1,2,3,4],[1,2,3,4], label="1")
p2, = par1.plot([1,2,3,4],[2,2,2,2], label="2")
p3, = par2.plot([1,2,3,4],[3,3,2,1], label="3")
p4, = par3.plot([1,2,3,4],[1,1,2.5,3.5], label="4")

plt.show()

By the way, when asking such a question, it would be helpful not to reverse the standard understanding of x-axis and y-axis! Also, I had to add a subplot_adjust to see the whole figure with mpl's standard settings.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that was a nice easy solution. I didn't know about the visibility setting.
Hearty agreement on not "flipping" the standard X and Y. Seeing set_xlabel("Y axis") made the hairs on the back of my neck stand up...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.