Links

Create Leaderboard stacked

Tags: #plotly #chart #horizontalbar #dataviz #snippet #operations #image #html
Author: Florent Ravenel
Learn more on the Plotly doc : https://plotly.com/python/horizontal-bar-charts/

Input

Import libraries

import plotly.express as px
import pandas as pd

Variables

title = "Leaderboard_stacked"
# Output paths
output_image = f"{title}.png"
output_html = f"{title}.html"

Model

Get data model

data = [
{"LABEL": "A", "GROUPS": "1", "VALUE": 88},
{"LABEL": "A", "GROUPS": "2", "VALUE": 12},
{"LABEL": "B", "GROUPS": "1", "VALUE": 43},
{"LABEL": "B", "GROUPS": "2", "VALUE": 43},
{"LABEL": "C", "GROUPS": "1", "VALUE": 2},
{"LABEL": "C", "GROUPS": "2", "VALUE": 87},
{"LABEL": "D", "GROUPS": "1", "VALUE": 67},
{"LABEL": "D", "GROUPS": "2", "VALUE": 111},
{"LABEL": "E", "GROUPS": "1", "VALUE": 24},
{"LABEL": "E", "GROUPS": "2", "VALUE": 123},
]
df = pd.DataFrame(data)
df

Prep data

# Calc VALUE TOTAL by LABEL to sort bar
def prep_data(df):
# Calc total
df_tot = df.groupby(["LABEL"], as_index=False).agg({"VALUE": "sum"}).rename(columns={"VALUE": "VALUE_TOT"})
# Merge dataframe
df = pd.merge(df, df_tot, on="LABEL")
# Sort values
df = df.sort_values(by=["VALUE_TOT", "LABEL"])
return df
df_plot = prep_data(df)
df_plot

Create the plot

def create_barchart(df, label, groups, value):
last_value = '{:,.0f}'.format(df[value].sum())
colors = {
"1": "blue",
"2": "green",
}
fig = px.bar(df,
y=label,
color=groups,
x=value,
orientation='h',
color_discrete_map=colors,
text=value)
fig.update_layout(
title=f"<b>Ranking by label</b><br><span style='font-size: 13px;'>Total value: {last_value}</span>",
title_font=dict(family="Arial", size=18, color="black"),
legend_title="Packs",
legend_title_font=dict(family="Arial", size=11, color="black"),
legend_font=dict(family="Arial", size=10, color="black"),
font=dict(family="Arial", size=12, color="black"),
plot_bgcolor="#ffffff",
width=1200,
height=800,
xaxis_title=None,
xaxis_showticklabels=False,
yaxis_title=None,
margin_pad=10,
margin_t=100,
)
fig.update_yaxes(categoryarray=df[label].unique())
# Display fig
config = {'displayModeBar': False}
fig.show(config=config)
return fig
fig = create_barchart(df_plot, "LABEL", "GROUPS", "VALUE")

Output

Export in PNG and HTML

fig.write_image(output_image, width=1200)
fig.write_html(output_html)

Generate shareable assets

link_image = naas.asset.add(output_image)
link_html = naas.asset.add(output_html, {"inline":True})
#-> Uncomment the line below to remove your assets
# naas.asset.delete(output_image)
# naas.asset.delete(output_html)