Waterfall#
Download this notebook from GitHub (right-click to download).
Title: Waterfall Element#
Dependencies: Bokeh
Backends: Bokeh, Matplotlib
import polars as pl
import holoviews as hv
hv.extension('bokeh')
The Waterfall Element is a type of bar chart that is used to show how an initial value is affected by a series of intermediate positive or negative values, leading to a final value. It is often used in financial analysis to visualize the cumulative effect of sequentially introduced positive or negative values.
The Waterfall Element can be created using the hv.Waterfall function, which takes a list of tuples where each tuple contains a category and its corresponding value. The categories are typically displayed on the x-axis, while the values are represented as bars on the y-axis. Positive values will extend upwards from the baseline, while negative values will extend downwards.
data = [('Revenue', 100), ('COGS', 50), ('Opex', -30), ('Tax', -10)]
w = hv.Waterfall(data)
w
We can achieve the same plot using a Pandas DataFrame.
df = pl.DataFrame({
'Category': ['Revenue', 'COGS', 'Opex', 'Tax'],
'Amount': [100, 50, -30, -10]
})
w = hv.Waterfall(df, kdims='Category', vdims='Amount')
w
If your dataframe contains the final values (after totals), you can pre-process it to get the intermediate values:
df = pl.DataFrame({
"Category": ["Revenue", "COGS", "Opex", "Tax"],
"Final": [100, 150, 120, 110],
})
df = df.with_columns(
pl.col("Final").diff().fill_null(pl.col("Final")).alias("Amount")
)
w = hv.Waterfall(df.to_pandas(), kdims="Category", vdims="Amount")
w
Waterfall charts also support numeric values, like years.
df = pl.DataFrame({
'Year': [2017, 2018, 2019, 2020],
'Amount': [100, 25, -75, 25]
})
w = hv.Waterfall(df, kdims='Year', vdims='Amount')
w
The color of the start and end bars can be customized using the start_color and total_color options. The positive_color and negative_color options can be used to customize the colors of the positive and negative bars too.
w.opts(start_color='white', total_color='gray', positive_color="#c0ffc0", negative_color="#B52626")
If the total_color is not specified, the last bar will be mirrored to the start_color by default, and vice versa.
w = hv.Waterfall(df, kdims='Year', vdims='Amount')
w.opts(start_color="gold")
total_label can be used to customize the label of the total bar.
w.opts(total_label="Result")
Alternatively, the total bar can be hidden by setting show_total=False.
w.opts(show_total=False)
The connectors can also be customized using the connector_line_color and connector_width options.
w.opts(connector_line_color="black", connector_line_width=1.5, connector_line_dash='solid')
The connectors can also be turned off with show_connectors=False.
w.opts(show_connectors=False)
For full documentation and the available style and plot options, use hv.help(hv.Waterfall).
Download this notebook from GitHub (right-click to download).