waterfall_plot()
with Other Geometry Layers¶This notebook demonstrates how to enrich a waterfall plot with background and foreground layers. Foreground layers can be added using the regular +
operator. To add background layers, use the new background_layers
property.
Limitations:
import pandas as pd
from lets_plot import *
from lets_plot.bistro import waterfall_plot
LetsPlot.setup_html()
# Waterfall data
df = pd.DataFrame({
"Accounts": ["initial", "revenue", "costs", "revenue", "costs", "revenue", "costs", "revenue", "costs", "total"],
"Values": [200, 200, -150, 250, -170, 150, -50, 280, -25, None],
"Measure": ["absolute", "relative", "relative", "relative", "relative", "relative", "relative", "relative",
"relative", "total"],
})
# Background layer and its data
quarter_data = {
"period_start": [0.5, 4.5],
"period_end": [4.5, 8.5],
"ai_introduced": [False, True],
}
quarter_layer = geom_band(
aes(
xmin="period_start",
xmax="period_end",
paint_a="ai_introduced"
),
data=quarter_data,
alpha=0.2,
# We use "paint_a" to color the bands based on a separate category (e.g., quarters),
# so they have their own color palette independent from the waterfalls
fill_by="paint_a",
color_by="paint_a"
)
# Foreground layers and their data
quarter_label_data = {
"name": ["Q1", "Q2", "Q3", "Q4"],
"x": [1.5, 3.5, 5.5, 7.5],
"y": [750] * 4
}
quarter_ai_status_data = {
"text": ["Before AI\nintroduction", "After AI\nintroduction"],
"x": [2.5, 6.5],
"y": [100, 100],
}
text_layers = geom_text(aes(x="x", y="y", label="name"), data=quarter_label_data, size=8) + \
geom_text(aes(x="x", y="y", label="text"), data=quarter_ai_status_data, size=12)
# Whole plot
(waterfall_plot(df, "Accounts", "Values", measure="Measure",
background_layers=quarter_layer) # Background layer
+ text_layers # Foreground layers
+ scale_hue("paint_a", guide="none") # Color for the background layer (bands)
+ ggsize(750, 450)
+ ggtitle("Waterfall with additional layers"))