Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to show percentage in a seaborn graph?

I want to create a barplot in wich I can show the percentage of sells.

I have the following code

plt.figure(figsize=(10,5))
graph = df.groupby("Day")["Barcode"].count().sort_values(ascending=False).reset_index()
graph_sells_day = sns.barplot(data=graph, x='Day', y='Barcode', palette='viridis')

graph_sells_day.bar_label(graph_sells_day.containers[0], fmt='%.1i', fontsize= 7)


plt.title('SELLS BY DAY')

plt.ylabel('Sells', fontsize=12)

plt.xlabel('Day', fontsize=12)
plt.show()

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

And i want to replace this line :

graph_sells_day.bar_label(graph_sells_day.containers[0], fmt='%.1i', fontsize= 7)

To make the label a percentage where the highest value is 100% and the others are proportional to that. For example, I want to show the day with 300 sells (highest value) as 100%, days with 150 sells as 50%, 100 sells as 33.3%, etc.

>Solution :

Edit

** correcting the y axis to maintain "Sells" as the y values

You can create a new column called, let’s say, "Percent" and divide all the sells values by the max value in your sells column and then multiply them by 100, making the max value 100% and the others a percent of that max value. See below:

# Sample data
day = [1, 2, 3, 4, 5, 6, 7, 8]
sells = [30, 40, 300, 50, 10, 200, 250, 140]
df = pd.DataFrame(zip(day, sells), columns = ['Day', 'Sells'])
############

# Create a percent column based on the max value in the "Sells" column
df['Percent'] = df.Sells / df.Sells.max() * 100

# Graph as usual but change the labels
graph_sells_day = sns.barplot(data = df, x = 'Day', y = 'Sells')

# This is where you create the labels for the graph as percents
labels = (df.Percent.round(1).astype(str) + '%').values

graph_sells_day.bar_label(graph_sells_day.containers[0], labels = labels, fmt='%.1i', fontsize= 7)
plt.title('SELLS BY DAY')

plt.ylabel('Sells', fontsize=12)

plt.xlabel('Day', fontsize=12)
plt.show()

Graph:
enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading