Tuesday 15 May 2012

python - Parasite axis appearing at two positions and overlapping -



python - Parasite axis appearing at two positions and overlapping -

using illustration guidance: http://matplotlib.org/examples/axes_grid/demo_parasite_axes2.html tried create plot multiple axes using twiny in stead of twinx. however, 1 of axes - par2 appears both in top , bottom location, overlapping par1 on top. doing wrong?

from mpl_toolkits.axes_grid1 import host_subplot import mpl_toolkits.axisartist aa import matplotlib.pyplot plt host = host_subplot(111, axes_class=aa.axes) plt.subplots_adjust(bottom=0.2) par1 = host.twiny() par2 = host.twiny() new_fixed_axis = par2.get_grid_helper().new_fixed_axis par2.axis["bottom"] = new_fixed_axis(loc="bottom", axes=par2, offset=(0, -40)) par2.axis["bottom"].toggle(all=true) host.set_xlim(0, 2) host.set_ylim(0, 2) host.set_ylabel("distance") host.set_xlabel("density") par1.set_xlabel("temperature") par2.set_xlabel("velocity") p1, = host.plot([0, 1, 2], [0, 1, 2], label="density") p2, = par1.plot([0, 3, 2], [0, 1, 2], label="temperature") p3, = par2.plot([50, 30, 15], [0, 1, 2], label="velocity") par1.set_xlim(0, 4) par2.set_xlim(1, 65) host.legend() host.axis["bottom"].label.set_color(p1.get_color()) par1.axis["top"].label.set_color(p2.get_color()) par2.axis["bottom"].label.set_color(p3.get_color()) plt.draw() plt.show()

the difference between matplotlib website illustration , code you're adding parasite axes adjacent "normal" axis location (i.e., bottom) whereas illustration adding them adjacent "abnormal" or twinned axis.

the par2 = host.twiny() phone call sets par2 top axis visible , isn't undone new_fixed_axis() call, need switch off using,

par2.axis["top"].toggle(all=false)

note that, weirdly, can accomplish replacing top axis rather bottom one:

par2.axis["top"] = new_fixed_axis(loc="bottom", axes=par2, offset=(0, -40))

but that's whole world of confusion waiting happen.

python matplotlib

No comments:

Post a Comment