secondary axes 10 times smaller that the main axes using twinx()

Question:

I am producing a plot and I want to make the secondary y-axes 10 times smaller than the main y-axes.

ax2 = ax.twinx()
sns.lineplot(data=df["A"], ax=ax)
sns.lineplot(data=df["B"], ax=ax2)

is it possible to do define ax2 = ax.twinx()/10? how can specifcy that ax2 should be 10 times lower than ax? for example if ax goes from 0 to 100, ax2 should go from 0 to 1.

Answers:

IIUC, use get_ylim and set_ylim to re-adjust the limit of ax2 :

ax2 = ax.twinx()
sns.lineplot(data=df["A"], ax=ax)
sns.lineplot(data=df["B"], ax=ax2)
ax2.set_ylim(0, ax.get_ylim()[1]/100) # <- add this line

NB : If you need a view limit from 0 to 1, you need to divide by 100 and not 10.

Output :

enter image description here

Answered By: Timeless
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.