Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

show percentage in seaborn countplot site:stackoverflow.com

def with_hue(plot, feature, Number_of_categories, hue_categories):
    a = [p.get_height() for p in plot.patches]
    patch = [p for p in plot.patches]
    for i in range(Number_of_categories):
        total = feature.value_counts().values[i]
        for j in range(hue_categories):
            percentage = '{:.1f}%'.format(100 * a[(j*Number_of_categories + i)]/total)
            x = patch[(j*Number_of_categories + i)].get_x() + patch[(j*Number_of_categories + i)].get_width() / 2 - 0.15
            y = patch[(j*Number_of_categories + i)].get_y() + patch[(j*Number_of_categories + i)].get_height() 
            ax.annotate(percentage, (x, y), size = 12)
    plt.show()

def without_hue(plot, feature):
    total = len(feature)
    for p in ax.patches:
        percentage = '{:.1f}%'.format(100 * p.get_height()/total)
        x = p.get_x() + p.get_width() / 2 - 0.05
        y = p.get_y() + p.get_height()
        ax.annotate(percentage, (x, y), size = 12)
    plt.show()
Comment

PREVIOUS NEXT
Code Example
Python :: Removing Elements from Python Dictionary Using pop() method 
Python :: reaction role discord.py 
Python :: selenium wait until 
Python :: extend python 
Python :: class method in python 
Python :: itertools count 
Python :: define a function in python without arguments 
Python :: shape of variable python 
Python :: cosine similarity numpy 
Python :: oop in python 
Python :: exponent function in python 
Python :: daraja mpesa 
Python :: reading from a text file 
Python :: list dataframe to numpy array 
Python :: plotly express change legend labels 
Python :: python strings 
Python :: models django 
Python :: convert python code to pseudocode online 
Python :: possible substrings of a string python 
Python :: how to run other python files in python 
Python :: python how to print 
Python :: cmap seaborn 
Python :: sum python 
Python :: Python String count() example 
Python :: add new element to python dictionary 
Python :: python variable type 
Python :: manual merge sort 
Python :: object has no attribute python 
Python :: full body tracking module 
Python :: display list 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =