Align secondary ticks of the y axis with stripplot on the primary x axis

import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
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);

Link to chart produced

I would like to create some labels on the secondary y axis that line up with the tick line Hamiltonians on the first y axis. I would like E to be at the same height as D, F with C, etc.

I tried several different methods to even out ticks, but no luck.

Any other suggestions on how to add these shortcuts would be greatly appreciated.

+3
source share
1 answer

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()

enter image description here

+1
source

Source: https://habr.com/ru/post/1691729/


All Articles