I'm plotting the counts of a categorical variable and want to add a second y-axis that shows the percentage of the total number of samples.
import matplotlib.pyplot as plt
import seaborn as sns
titanic = sns.load_dataset("titanic")
g = sns.catplot(x="alive", col="embark_town", col_wrap=4,
data=titanic[titanic.deck.notnull()],
kind="count", height=4, aspect=.8)
for i, ax in enumerate(g.axes.flat):
# Create second y-axis for the percentages on the right
ax1 = ax.twinx()
### Attempt to fix percentages by plotting the bars over
#g = sns.catplot(x="alive", col="embark_town", col_wrap=4,
# data=titanic[titanic.deck.notnull()],
# kind="count", height=4, aspect=.8,
# ax = ax1)
# Label by the percentages
ax1.set_ylim(ax.get_ylim())
ax1.set_yticklabels(np.round(ax.get_yticks()/titanic[titanic.deck.notnull()].shape[0],1))
ax1.set_ylabel('Percentage')
# Rotate x-labels
labels = ax.get_xticklabels() # get x labels
ax.set_xticklabels(labels, rotation=90)
# Ensure good spacing
g.fig.tight_layout()
Right, now my issue is that the percentages are being duplicated on the right y-axis, as show in the image below
I've tried to correct this by plotting the counts on the new axis, but that adds another row of subplots (see commented out code in the for loop). How can I get the right y-axis labels to not have duplicate values and actually reflect the percentages of the total count?
question from:
https://stackoverflow.com/questions/65850300/add-percentage-axis-to-seaborn-catplot-with-correct-axis-tick-labels 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…