Skip to content Skip to sidebar Skip to footer

Matplotlib - Possible To Make Sub Plots With Parasite Axes?

I'm trying to make a chart with two subplots, each of which has a parasite axis, as shown in the documentation here. However, although I can replicate the example with a single plo

Solution 1:

You had a few small mistakes in there. First of all the plt.subplot() commands where overriding your host_subplot() commands (see the note in the matplotlib.pyplot.subplot() documentation: "Creating a subplot will delete any pre-existing subplot that overlaps with it beyond sharing a boundary"). Further, you have to track the instances for the two plots separately. I solved it like this that I created host1, par11, and par12 for the first Axes and host2, par21, and par22 for the second Axes. The entire code now looks like this:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA

fig = plt.figure()

host1 = host_subplot(211, axes_class=AA.Axes)
plt.subplots_adjust(right=0.75)

par11 = host1.twinx()
par12 = host1.twinx()

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

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

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

host1.set_xlabel("Distance")
host1.set_ylabel("Density")
par11.set_ylabel("Temperature")
par12.set_ylabel("Velocity")

p1, = host1.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par11.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = par12.plot([0, 1, 2], [50, 30, 15], label="Velocity")

par11.set_ylim(0, 4)
par12.set_ylim(1, 65)

host1.legend()

host1.axis["left"].label.set_color(p1.get_color())
par11.axis["right"].label.set_color(p2.get_color())
par12.axis["right"].label.set_color(p3.get_color())

#####2#####
host2 = host_subplot(212, axes_class=AA.Axes)
plt.subplots_adjust(right=0.75)
par21 = host2.twinx()
par22 = host2.twinx()

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

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

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

host2.set_xlabel("Distance")
host2.set_ylabel("Density")
par21.set_ylabel("Temperature")
par22.set_ylabel("Velocity")

p1, = host2.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par21.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = par22.plot([0, 1, 2], [50, 30, 15], label="Velocity")

par21.set_ylim(0, 4)
par22.set_ylim(1, 65)

host2.legend()

host2.axis["left"].label.set_color(p1.get_color())
par21.axis["right"].label.set_color(p2.get_color())
par22.axis["right"].label.set_color(p3.get_color())    

fig.tight_layout()

plt.draw()
plt.show()

And here is the result:

result of the above code

Hope this helps.


Post a Comment for "Matplotlib - Possible To Make Sub Plots With Parasite Axes?"