Create beautiful presentations

ANNA LAPUSHNER - Sep 3 - - Dev Community

The following code is going to enable you to create a very pretty bar plot, in an attractive color scheme, with the bars lining up in ascending order AND labeled with the corresponding % of the data.

Always remember, a spot on analysis is very important...and your presentation of that analysis is that cool refreshing mint iced-tea on a very hot summer day!

def my_labeled_barplot(data, feature, perc=False, n=None, order=None, palette='viridis', ascending=False):
    """
    Creates a bar plot with labels showing counts or percentages, with sorting options.

    Args:
        data: The DataFrame containing the data.
        feature: The name of the categorical column to plot.
        perc: If True, display percentages on labels, otherwise counts.
        n: (Optional) Width of the plot figure.
        order: (Optional) 'asc' for ascending order, 'desc' for descending order,
               or None for no specific order.
        palette: (Optional) Color palette for the bars.
        ascending: (Optional) Used when 'order' is not specified. Sorts bars in
                   ascending order if True, descending if False.
    """
    plt.figure(figsize=(n if n else len(data.columns) + 1, 5))

    if order == 'asc':
        order_index = data[feature].value_counts(ascending=True).index
    elif order == 'desc':
        order_index = data[feature].value_counts(ascending=False).index
    else:
        order_index = data[feature].value_counts(ascending=not ascending).index # Sort based on ascending parameter

    ax = sns.countplot(data=data, x=feature, order=order_index, palette=palette)

    if perc:
        total = len(data[feature])
        for p in ax.patches:
            percentage = '{:.1f}%'.format(100 * p.get_height() / total)
            x = p.get_x() + p.get_width() / 2 - 0.1
            y = p.get_y() + p.get_height()
            ax.annotate(percentage, (x, y), size=10)
    else:
        for p in ax.patches:
            x = p.get_x() + p.get_width() / 2 - 0.1
            y = p.get_y() + p.get_height()
            ax.annotate(p.get_height(), (x, y), size=10)

    plt.xticks(rotation=90, fontsize=12)

# Example usage:
# my_labeled_barplot(data, 'Club', perc=True, order='asc')  # Ascending order
# my_labeled_barplot(data, 'Club', perc=True, order='desc') # Descending order
# my_labeled_barplot(data, 'Club', perc=True, ascending=False) # Descending order without using 'order' parameter
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

. . . . . . . . . . . . . . . . .
Terabox Video Player