Create Waterfall chart
Tags: #plotly #chart #warterfall #dataviz #snippet
Author: Jeremy Ravenel

Input

Import libraries

1
import naas
2
import plotly.graph_objects as go
Copied!

Variables

1
title = "EBITDA (simple)"
2
3
# Output paths
4
output_image = f"{title}.png"
5
output_html = f"{title}.html"
Copied!

Model

Create Waterfall chart

1
fig = go.Figure(go.Waterfall(
2
name = "20", orientation = "v",
3
measure = ["relative", "relative", "total", "relative", "relative", "total"],
4
decreasing = {"marker":{"color":"#d94228"}},
5
increasing = {"marker":{"color":"#5ee290"}},
6
totals = {"marker":{"color":"#3f3f3f"}},
7
x = ["Sales", "Consulting", "Revenue", "Direct expenses", "Other expenses", "EBITDA"],
8
textposition = "outside",
9
text = ["+60", "+80", "140", "-40", "-20", "80"],
10
y = [60, 80, 0, -40, -20, 0],
11
connector = {"line":{"color":"white"}},
12
))
13
14
fig.update_layout(
15
title=title ,
16
plot_bgcolor="#ffffff",
17
width=1200,
18
height=800,
19
xaxis_tickfont_size=14,
20
yaxis=dict(
21
title='USD (millions)',
22
titlefont_size=16,
23
tickfont_size=14,
24
),
25
legend=dict(
26
x=0,
27
y=1.0,
28
bgcolor='white',
29
bordercolor='white'
30
),
31
bargap=0.1, # gap between bars of adjacent location coordinates.
32
bargroupgap=0.1 # gap between bars of the same location coordinate.
33
)
34
config = {'displayModeBar': False}
35
fig.show(config=config)
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