pythonmatplotlibseabornhistplot

Show count in each histplot bin


As explained in seaborn histplot - print y-values above each bar, counts can be displayed on each bar in a 1-d histogram by using .bar_label(ax.containers[0]).

I'm struggling to figure out how to do the equivalent for a 2-d histogram (created with sns.histplot(data, x='var1', y='var2')).

I know I can make an annotation for the (a,b) bin with .annotate('foo', xy=(a, b)), but I'm not sure how to retrieve the count for that bin (to pass it to .annotate()).

I'd like the result to look similar to the one shown at https://seaborn.pydata.org/examples/spreadsheet_heatmap.html, except that it's a histplot, not a heatmap.


Solution

  • I found that the counts can be extracted from the histplot()'s .collections attribute and then .annotate()'d, as follows:

    import numpy as np
    import seaborn as sns
    
    ax = sns.histplot(data, x='var1', y='var2', cbar=True)
    w = ax.collections[0].get_coordinates().shape[1] - 1
    for k, v in enumerate(ax.collections[0].get_array()):
        if not np.ma.is_masked(v):
            ax.annotate(v, xy=(k % w, k // w), ha='center', color='white')