Waterfall plots #
A waterfall chart is a way to show the breakdown of part to whole. Typically, it consists of a series of connected ‘rising’ bars, and then a series of connected ‘falling’ bars. In this way, one can show how a series of bars add up to a whole, and how that whole is consumed by other parts.
Plotly has the ability to directly make these charts, and you can see the documentation here. If you look at the top of the documentation, you see a bit of example code, and then the image it produces. Let us consider their code some.
import plotly.graph_objects as go
fig = go.Figure(go.Waterfall(
name = "20", orientation = "v",
measure = ["relative", "relative", "total", "relative", "relative", "total"],
x = ["Sales", "Consulting", "Net revenue", "Purchases", "Other expenses", "Profit before tax"],
textposition = "outside",
text = ["+60", "+80", "", "-40", "-20", "Total"],
y = [60, 80, 0, -40, -20, 0],
connector = {"line":{"color":"rgb(63, 63, 63)"}},
))
fig.update_layout(
title = "Profit and loss statement 2018",
showlegend = True
)
fig.show()
In looking at this example, there are few things you need to understand.
It is using the “graph_objects” approach to plotting. One makes a figure object go.Figure and then adds a waterfall plot to this go.Waterfall. This is a totally fine approach, but in our class, we have been using a slightly different way to construct figures. We first create a figure object, and then add a plot to it. Changing the example code from the Plotly site to reflect this, we arrive at:
from plotly.subplots import make_subplots
fig = make_subplots()
fig.add_waterfall(
name = "20", orientation = "v",
measure = ["relative", "relative", "total", "relative", "relative", "total"],
x = ["Sales", "Consulting", "Net revenue", "Purchases", "Other expenses", "Profit before tax"],
textposition = "outside",
text = ["+60", "+80", "", "-40", "-20", "Total"],
y = [60, 80, 0, -40, -20, 0],
connector = {"line":{"color":"rgb(63, 63, 63)"}},
)
fig.show()
The second thing worth knowing is that {} is similar to dict(). Both create dictionaries, but the former requires that you specify key-value pairs using strings for both, separated by a colon, while the later lets you use equals to assign values to variables.
If you use the approach we have been using, this ends up looking like:
from plotly.subplots import make_subplots
fig = make_subplots()
fig.add_waterfall(
name = "20", orientation = "v",
measure = ["relative", "relative", "total", "relative", "relative", "total"],
x = ["Sales", "Consulting", "Net revenue", "Purchases", "Other expenses", "Profit before tax"],
textposition = "outside",
text = ["+60", "+80", "", "-40", "-20", "Total"],
y = [60, 80, 0, -40, -20, 0],
connector = dict(line = dict(color = "rgb(63, 63, 63)")),
)
fig.show()
Running this will produce the same figure as in the Waterfall documentation:
So, w have translated the approach shown in the documentation to our approach to making Plotly figures.
It is worth learning how to read and understand documentation, so this tutorial consists of me telling you to consider the above example, and then modifying it to show data of your interest. You can, of course, always ask an AI tool for help or come to class to ask me for help! But learning to work from documentation is a great skill to develop