Ticks are already installed correctly using
ax2.set(yticks=ax.get_yticks(), yticklabels=labels2)
. The disadvantage is the limits of the axes. This should be the same for both axes.
you can use
ax2.set_ylim(ax.get_ylim())
or agree to .set
:
ax2.set(yticks=ax.get_yticks(), yticklabels=labels2, ylim=ax.get_ylim())
Full example:
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
labels1 = ['A','B','C','D']
labels2 = ['E','F','G','H']
fig, ax = plt.subplots()
df = pd.DataFrame(np.random.random(size=(10,4)), columns=('A','B','C','D'))
sns.stripplot(data=df, orient='h', ax=ax)
ax2 = ax.twinx()
ax2.set(yticks=ax.get_yticks(), yticklabels=labels2, ylim=ax.get_ylim())
plt.show()

source
share