Skip to content

Instantly share code, notes, and snippets.

@kurasaiteja
Last active March 21, 2021 12:30
Show Gist options
  • Save kurasaiteja/cc5de27975940d90f9326c894fb4307f to your computer and use it in GitHub Desktop.
Save kurasaiteja/cc5de27975940d90f9326c894fb4307f to your computer and use it in GitHub Desktop.
def fill_title_and_subtitle(title:str, subtitle:str):
return f"{title}<br><sub>{subtitle}</sub>"
def create_bar_chart(data: pd.DataFrame, xcolumn: str, ycolumn:str, title:str, colors:str, ylabel="Count", n=None):
hovertemplate ='<br><b>%{x}</b>'+f'<br><b>{ylabel}: </b>'+'%{y}<br><extra></extra>'
data = data.sort_values(ycolumn, ascending=False).dropna(subset=[ycolumn])
if n is not None:
data = data.iloc[:n]
else:
n = ""
fig = go.Figure(go.Bar(
hoverinfo='skip',
x=data[xcolumn],
y=data[ycolumn],
hovertemplate = hovertemplate,
marker=dict(
color = data[ycolumn],
colorscale=colors,
),
),
)
fig.update_layout(
title=title,
xaxis_title=f"Top {n} {xcolumn.title()}",
yaxis_title=ylabel,
plot_bgcolor='rgba(0,0,0,0)',
hovermode="x"
)
fig.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment