python - Change color of parasite axis in matplotlib -


i want change color of parasitic axis.

base code example http://matplotlib.org/examples/axes_grid/demo_parasite_axes2.html

i add

par1.spines['right'].set_color("red") 

to change color (according https://stackoverflow.com/a/12059429/716469). doesn't work.

the full code:

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(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") 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())  # why doesn't work? par1.spines['right'].set_color("red")  plt.draw() plt.show() 

i use matplotlib 2.0.0

the host_subplot creates instance of mpl_toolkits.axisartist.axis_artist.axisartist not same normal axes.

you therefore need set attributes of axis label,

host.axis["right"].line.set_color(p2.get_color()) par1.axis["right"].major_ticks.set_color(p2.get_color()) par1.axis["right"].major_ticklabels.set_color(p2.get_color())  par2.axis["right"].line.set_color(p3.get_color()) par2.axis["right"].major_ticks.set_color(p3.get_color()) par2.axis["right"].major_ticklabels.set_color(p3.get_color()) 

enter image description here


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -