I want to create a barplot in which I can show the percentage of sales.
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()
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.
** 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()