Add data labels to Seaborn factor plot

前端 未结 3 1783
春和景丽
春和景丽 2020-12-28 10:28

I would like to add data labels to factor plots generated by Seaborn. Here is an example:

import pandas as pd
from pandas import Series, DataFrame
import nu         


        
3条回答
  •  滥情空心
    2020-12-28 11:12

    You could do something even simpler

    plt.figure(figsize=(4, 3))
    plot = sns.catplot(x='Sex', y='count', kind='bar', data=titanic_df)
    
    # plot.ax gives the axis object
    # plot.ax.patches gives list of bars that can be access using index starting at 0
    
    for i, bar in enumerate(plot.ax.patches):
        h = bar.get_height()
        plot.ax.text(
            i, # bar index (x coordinate of text)
            h+10, # y coordinate of text
            '{}'.format(int(h)),  # y label
            ha='center', 
            va='center', 
            fontweight='bold', 
            size=14)
    

提交回复
热议问题