Create Vertical Barchart
Tags: #plotly #chart #verticalbarchart #group #dataviz #snippet
Author: Florent Ravenel

Input

Import libraries

1
import naas
2
import plotly.graph_objects as go
3
import pandas as pd
Copied!

Variables

1
title = "Sales Evolution"
2
3
# Output paths
4
output_image = f"{title}.png"
5
output_html = f"{title}.html"
Copied!

Get data

1
x_axis = [1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012]
2
y_axis = [219, 146, 112, 127, 124, 180, 236, 207, 236, 263, 350, 430, 474, 526, 488, 537, 500, 439]
3
value_dict = zip(x_axis, y_axis)
4
5
df = pd.DataFrame(value_dict, columns=["LABEL", "VALUE"])
6
df
Copied!

Model

Create Vertical Barchart

1
def create_barchart(df, label, value, title):
2
fig = go.Figure()
3
4
fig.add_trace(go.Bar(x=df[label],
5
y= df[value],
6
marker_color='#5ee290'))
7
fig.update_layout(
8
title=title,
9
plot_bgcolor="#ffffff",
10
width=1200,
11
height=800,
12
xaxis_tickfont_size=14,
13
legend=None,
14
bargap=0.1, # gap between bars of adjacent location coordinates.
15
bargroupgap=0.1 # gap between bars of the same location coordinate.
16
)
17
config = {'displayModeBar': False}
18
fig.show(config=config)
19
return fig
20
21
fig = create_barchart(df, label="LABEL", value="VALUE", title=title)
Copied!

Output

Export in PNG and HTML

1
fig.write_image(output_image, width=1200)
2
fig.write_html(output_html)
Copied!

Generate shareable assets

1
link_image = naas.asset.add(output_image)
2
link_html = naas.asset.add(output_html, {"inline":True})
3
4
#-> Uncomment the line below to remove your assets
5
# naas.asset.delete(output_image)
6
# naas.asset.delete(output_html)
Copied!
Copy link
Edit on GitHub