from lets_plot import *
from lets_plot.bistro import *
LetsPlot.setup_html()
data = {
'stage': ["A", "B", "C", "D", "E", "F"],
'count': [598, -264, 156, -4, 330, -85],
'pct': [100, 44.14, 46.71, 1.81, 67.9, 10.41],
}
waterfall_plot(data, 'stage', 'count')
The waterfall_plot()
function now supports two additional parameters, relative_labels
and absolute_labels
, which provide separate control over labeling for relative and absolute bars.
waterfall_plot(
data, 'stage', 'count',
relative_labels=layer_labels().line('@pct%').format('@pct', '.0f'),
absolute_labels=layer_labels().line('[email protected]..').size(20),
)
These new parameters also support multiline label configuration.
waterfall_plot(
data, 'stage', 'count',
relative_labels=layer_labels()
.line('@count')
.line('@pct%')
.format('@pct', '.0f'),
absolute_labels=layer_labels().line('[email protected]..').size(20),
)
label_format
parameter and layer_labels().format()
¶By default, labels are generated from the ..label..
data column. The label_format
parameter specifies the display format for this column and is convenient if you are not using relative_labels
or absolute_labels
.
If you're using relative_labels
or absolute_labels
, it's recommended to use the layer_labels().format()
method instead — it allows you to define formatting for each label line individually.
If needed, you can also override the format of ..label..
explicitly using:
layer_labels().line("@..label..").format("@..label..", ".0f")
waterfall_plot(
data, 'stage', 'count',
label_format='.1f',
relative_labels=layer_labels()
.line('@..label..')
.line('@count')
.line('@pct%')
.format('@pct', '.0f'),
absolute_labels=layer_labels().line('[email protected]..')
)
The new parameters allow you to hide labels for relative and absolute bars independently.
waterfall_plot(
data, 'stage', 'count',
relative_labels='none',
absolute_labels=layer_labels().line('[email protected]..').size(20),
)
waterfall_plot(
data, 'stage', 'count',
color='flow_type', # <--- Apply the 'flow type' color palette the boxes (borders).
size=2,
alpha=0.1,
relative_labels=layer_labels()
.line('@..label..')
.line('@count')
.line('@pct%')
.format('@pct', '.0f')
.inherit_color(), # <--- Let text in the 'relative boxes' to inherit the box color.
absolute_labels=layer_labels()
.line('[email protected]..')
.size(20),
label=element_text(color='gray30') # <--- Set gray text color to all the labels except the 'relative' (because.. see above)
)
Another, easier way in most cases to inherit the geometry color in text labels is to use the 'inherit'
value in the label
parameter.
This affects text in both 'relative' and 'absolute' bars:
waterfall_plot(data, 'stage', 'count',
color='flow_type', # <--- Apply the 'flow type' color palette the boxes (borders).
size=2,
alpha=0.1,
label=element_text(color='inherit') # <--- Use 'inherit' color for labels.
)
gggrid([
waterfall_plot(data, 'stage', 'count', width=.2),
waterfall_plot(data, 'stage', 'count', width=.2) + flavor_darcula()
])