Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

python 3.x - Add percentage axis to Seaborn catplot with correct axis tick labels

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

enter image description here

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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)

# calculate numbre of samples:
total_samples = len(titanic[titanic.deck.notnull()])


for i, ax in enumerate(g.axes.flat):
    
#     bounds of the left y-axis:
    ymin, ymax = ax.get_ylim()    
    
#     # Create second y-axis for the percentages on the right    
    ax1 = ax.twinx()
    
    # scale right axis labels to total samples and mutliply with 100 for percentages
    ax1.set_ylim(100*ymin/total_samples, 100*ymax/total_samples)
    
# Ensure good spacing  
g.fig.tight_layout()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...