Skip to content

Commit 88c76e3

Browse files
docs: Promote when-then-otherwise in User Guide (#3544)
Co-authored-by: Dan Redding <[email protected]> Co-authored-by: Joel Ostblom <[email protected]> Co-authored-by: Joel Ostblom <[email protected]>
1 parent 211f7c5 commit 88c76e3

File tree

64 files changed

+485
-350
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+485
-350
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ brush = alt.selection_interval()
5757
points = alt.Chart(source).mark_point().encode(
5858
x='Horsepower',
5959
y='Miles_per_Gallon',
60-
color=alt.condition(brush, 'Origin', alt.value('lightgray'))
60+
color=alt.when(brush).then("Origin").otherwise(alt.value("lightgray"))
6161
).add_params(
6262
brush
6363
)

altair/vegalite/v5/api.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -913,14 +913,17 @@ def then(self, statement: _StatementType, /, **kwds: Any) -> Then[Any]:
913913
A spec or value to use when the preceding :func:`.when()` clause is true.
914914
915915
.. note::
916-
``str`` will be encoded as `shorthand<https://altair-viz.github.io/user_guide/encodings/index.html#encoding-shorthands>`__.
916+
``str`` will be encoded as `shorthand`_.
917917
**kwds
918918
Additional keyword args are added to the resulting ``dict``.
919919
920920
Returns
921921
-------
922922
:class:`Then`
923923
924+
.. _shorthand:
925+
https://altair-viz.github.io/user_guide/encodings/index.html#encoding-shorthands
926+
924927
Examples
925928
--------
926929
Simple conditions may be expressed without defining a default::
@@ -990,10 +993,12 @@ def otherwise(
990993
Roughly equivalent to an ``else`` clause.
991994
992995
.. note::
993-
``str`` will be encoded as `shorthand<https://altair-viz.github.io/user_guide/encodings/index.html#encoding-shorthands>`__.
996+
``str`` will be encoded as `shorthand`_.
994997
**kwds
995998
Additional keyword args are added to the resulting ``dict``.
996999
1000+
.. _shorthand:
1001+
https://altair-viz.github.io/user_guide/encodings/index.html#encoding-shorthands
9971002
9981003
Examples
9991004
--------
@@ -1071,14 +1076,16 @@ def when(
10711076
When ``predicate`` is a ``Parameter`` that is used more than once,
10721077
``alt.when().then().when(..., empty=...)`` provides granular control for each occurrence.
10731078
**constraints
1074-
Specify `Field Equal Predicate <https://vega.github.io/vega-lite/docs/predicate.html#equal-predicate>`__'s.
1079+
Specify `Field Equal Predicate`_'s.
10751080
Shortcut for ``alt.datum.field_name == value``, see examples for usage.
10761081
10771082
Returns
10781083
-------
10791084
:class:`ChainedWhen`
10801085
A partial state which requires calling :meth:`ChainedWhen.then()` to finish the condition.
10811086
1087+
.. _Field Equal Predicate:
1088+
https://vega.github.io/vega-lite/docs/predicate.html#equal-predicate
10821089
10831090
Examples
10841091
--------
@@ -1176,14 +1183,17 @@ def then(self, statement: _StatementType, /, **kwds: Any) -> Then[_Conditions]:
11761183
A spec or value to use when the preceding :meth:`Then.when()` clause is true.
11771184
11781185
.. note::
1179-
``str`` will be encoded as `shorthand<https://altair-viz.github.io/user_guide/encodings/index.html#encoding-shorthands>`__.
1186+
``str`` will be encoded as `shorthand`_.
11801187
**kwds
11811188
Additional keyword args are added to the resulting ``dict``.
11821189
11831190
Returns
11841191
-------
11851192
:class:`Then`
11861193
1194+
.. _shorthand:
1195+
https://altair-viz.github.io/user_guide/encodings/index.html#encoding-shorthands
1196+
11871197
Examples
11881198
--------
11891199
Multiple conditions with an implicit default::
@@ -1247,7 +1257,7 @@ def when(
12471257
When ``predicate`` is a ``Parameter`` that is used more than once,
12481258
``alt.when(..., empty=...)`` provides granular control for each occurrence.
12491259
**constraints
1250-
Specify `Field Equal Predicate <https://vega.github.io/vega-lite/docs/predicate.html#equal-predicate>`__'s.
1260+
Specify `Field Equal Predicate`_'s.
12511261
Shortcut for ``alt.datum.field_name == value``, see examples for usage.
12521262
12531263
Returns
@@ -1257,11 +1267,12 @@ def when(
12571267
12581268
Notes
12591269
-----
1260-
- Directly inspired by the ``when-then-otherwise`` syntax used in ``polars.when``.
1270+
- Directly inspired by the ``when-then-otherwise`` syntax used in `polars.when`_.
12611271
1262-
References
1263-
----------
1264-
`polars.when <https://docs.pola.rs/py-polars/html/reference/expressions/api/polars.when.html>`__
1272+
.. _Field Equal Predicate:
1273+
https://vega.github.io/vega-lite/docs/predicate.html#equal-predicate
1274+
.. _polars.when:
1275+
https://docs.pola.rs/py-polars/html/reference/expressions/api/polars.when.html
12651276
12661277
Examples
12671278
--------

doc/case_studies/exploring-weather.rst

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -226,31 +226,28 @@ of the selection (for more information on selections, see
226226
.. altair-plot::
227227

228228
brush = alt.selection_interval()
229-
230-
points = alt.Chart().mark_point().encode(
231-
alt.X('temp_max:Q').title('Maximum Daily Temperature (C)'),
232-
alt.Y('temp_range:Q').title('Daily Temperature Range (C)'),
233-
color=alt.condition(brush, 'weather:N', alt.value('lightgray'), scale=scale),
234-
size=alt.Size('precipitation:Q').scale(range=[1, 200])
229+
color = alt.Color("weather:N").scale(scale)
230+
temp_range = alt.datum["temp_max"] - alt.datum["temp_min"]
231+
232+
points = alt.Chart(width=600, height=400).mark_point().encode(
233+
alt.X("temp_max:Q").title("Maximum Daily Temperature (C)"),
234+
alt.Y("temp_range:Q").title("Daily Temperature Range (C)"),
235+
color=alt.when(brush).then(color).otherwise(alt.value("lightgray")),
236+
size=alt.Size("precipitation:Q").scale(range=[1, 200]),
235237
).transform_calculate(
236-
"temp_range", "datum.temp_max - datum.temp_min"
237-
).properties(
238-
width=600,
239-
height=400
238+
temp_range=temp_range
240239
).add_params(
241240
brush
242241
)
243242

244-
bars = alt.Chart().mark_bar().encode(
245-
x='count()',
246-
y='weather:N',
247-
color=alt.Color('weather:N').scale(scale),
243+
bars = alt.Chart(width=600).mark_bar().encode(
244+
x="count()",
245+
y="weather:N",
246+
color=color
248247
).transform_calculate(
249-
"temp_range", "datum.temp_max - datum.temp_min"
248+
temp_range=temp_range
250249
).transform_filter(
251250
brush
252-
).properties(
253-
width=600
254251
)
255252

256253
alt.vconcat(points, bars, data=df)

doc/user_guide/compound_charts.rst

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,28 +364,25 @@ layered chart with a hover selection:
364364
.. altair-plot::
365365

366366
hover = alt.selection_point(on='pointerover', nearest=True, empty=False)
367+
when_hover = alt.when(hover)
367368

368369
base = alt.Chart(iris).encode(
369370
x='petalLength:Q',
370371
y='petalWidth:Q',
371-
color=alt.condition(hover, 'species:N', alt.value('lightgray'))
372+
color=when_hover.then("species:N").otherwise(alt.value("lightgray"))
372373
).properties(
373374
width=180,
374375
height=180,
375376
)
376377

377-
points = base.mark_point().add_params(
378-
hover
379-
)
378+
points = base.mark_point().add_params(hover)
380379

381380
text = base.mark_text(dy=-5).encode(
382-
text = 'species:N',
383-
opacity = alt.condition(hover, alt.value(1), alt.value(0))
381+
text="species:N",
382+
opacity=when_hover.then(alt.value(1)).otherwise(alt.value(0)),
384383
)
385384

386-
alt.layer(points, text).facet(
387-
'species:N',
388-
)
385+
(points + text).facet("species:N")
389386

390387
Though each of the above examples have faceted the data across columns,
391388
faceting across rows (or across rows *and* columns) is supported as

doc/user_guide/interactions/bindings_widgets.rst

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ where a drop-down is used to highlight cars of a specific ``Origin``:
5151

5252
input_dropdown = alt.binding_select(options=['Europe', 'Japan', 'USA'], name='Region ')
5353
selection = alt.selection_point(fields=['Origin'], bind=input_dropdown)
54-
color = alt.condition(
55-
selection,
56-
alt.Color('Origin:N').legend(None),
57-
alt.value('lightgray')
54+
color = (
55+
alt.when(selection)
56+
.then(alt.Color("Origin:N").legend(None))
57+
.otherwise(alt.value("lightgray"))
5858
)
5959

6060
alt.Chart(cars).mark_point().encode(
@@ -72,7 +72,7 @@ and selection parameters follow the same pattern as you will see further down
7272
in the :ref:`encoding-channel-binding` section.
7373

7474
As you can see above,
75-
we are still using ``conditions`` to make the chart respond to the selection,
75+
we are still using :ref:`conditions <conditions>` to make the chart respond to the selection,
7676
just as we did without widgets.
7777
Bindings and input elements can also be used to filter data
7878
allowing the user to see just the selected points as in the example below.
@@ -137,11 +137,7 @@ to see the point highlighted
137137
x='Horsepower:Q',
138138
y='Miles_per_Gallon:Q',
139139
tooltip='Name:N',
140-
opacity=alt.condition(
141-
search_input,
142-
alt.value(1),
143-
alt.value(0.05)
144-
)
140+
opacity=alt.when(search_input).then(alt.value(1)).otherwise(alt.value(0.05)),
145141
).add_params(
146142
search_input
147143
)
@@ -185,16 +181,12 @@ which would have been the case if we just wrote ``xval < selector``.
185181

186182
slider = alt.binding_range(min=0, max=100, step=1, name='Cutoff ')
187183
selector = alt.param(name='SelectorName', value=50, bind=slider)
184+
predicate = alt.datum.xval < selector
188185

189186
alt.Chart(df).mark_point().encode(
190187
x='xval',
191188
y='yval',
192-
color=alt.condition(
193-
alt.datum.xval < selector,
194-
# 'datum.xval < SelectorName', # An equivalent alternative
195-
alt.value('red'),
196-
alt.value('blue')
197-
)
189+
color=alt.when(predicate).then(alt.value("red")).otherwise(alt.value("blue")),
198190
).add_params(
199191
selector
200192
)
@@ -213,16 +205,12 @@ points based on whether they are smaller or larger than the value:
213205
bind=slider,
214206
value=[{'cutoff': 50}]
215207
)
208+
predicate = alt.datum.xval < selector.cutoff
216209

217210
alt.Chart(df).mark_point().encode(
218211
x='xval',
219212
y='yval',
220-
color=alt.condition(
221-
alt.datum.xval < selector.cutoff,
222-
# 'datum.xval < SelectorName.cutoff', # An equivalent alternative
223-
alt.value('red'),
224-
alt.value('blue')
225-
)
213+
color=alt.when(predicate).then(alt.value("red")).otherwise(alt.value("blue")),
226214
).add_params(
227215
selector
228216
)
@@ -263,11 +251,7 @@ just if the value of the check box is True (checked) or False (unchecked):
263251
alt.Chart(cars).mark_point().encode(
264252
x='Horsepower:Q',
265253
y='Miles_per_Gallon:Q',
266-
size=alt.condition(
267-
param_checkbox,
268-
'Acceleration:Q',
269-
alt.value(25)
270-
)
254+
size=alt.when(param_checkbox).then("Acceleration:Q").otherwise(alt.value(25)),
271255
).add_params(
272256
param_checkbox
273257
)
@@ -315,7 +299,7 @@ Altair provides the ``bind='legend'`` option to facilitate the creation of click
315299
x='Horsepower:Q',
316300
y='Miles_per_Gallon:Q',
317301
color='Origin:N',
318-
opacity=alt.condition(selection, alt.value(0.8), alt.value(0.2))
302+
opacity=alt.when(selection).then(alt.value(0.8)).otherwise(alt.value(0.2)),
319303
).add_params(
320304
selection
321305
)

doc/user_guide/interactions/expressions.rst

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,14 @@ To try this out, you can type ``mazda|ford`` in the search input box below.
169169
name='Search ',
170170
)
171171
)
172+
search_matches = alt.expr.test(alt.expr.regexp(search_input, "i"), alt.datum.Name)
173+
172174
alt.Chart(cars).mark_point(size=60).encode(
173175
x='Horsepower:Q',
174176
y='Miles_per_Gallon:Q',
175177
tooltip='Name:N',
176-
opacity=alt.condition(
177-
alt.expr.test(alt.expr.regexp(search_input, 'i'), alt.datum.Name),
178-
# f"test(regexp({search_input.name}, 'i'), datum.Name)", # Equivalent js alternative
179-
alt.value(1),
180-
alt.value(0.05)
181-
)
182-
).add_params(
183-
search_input
184-
)
178+
opacity=alt.when(search_matches).then(alt.value(1)).otherwise(alt.value(0.05)),
179+
).add_params(search_input)
185180

186181
And remember, all this interactivity is client side.
187182
You can save this chart as an HTML file or put it on a static site generator such as GitHub/GitLab pages

doc/user_guide/interactions/jupyter_chart.rst

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,12 @@ is available as ``jchart.params.cutoff``.
105105
106106
slider = alt.binding_range(min=0, max=100, step=1)
107107
cutoff = alt.param(name="cutoff", bind=slider, value=50)
108+
predicate = alt.datum.xval < cutoff
108109
109110
chart = alt.Chart(df).mark_point().encode(
110111
x='xval',
111112
y='yval',
112-
color=alt.condition(
113-
alt.datum.xval < cutoff,
114-
alt.value('red'), alt.value('blue')
115-
)
113+
color=alt.when(predicate).then(alt.value("red")).otherwise(alt.value("blue")),
116114
).add_params(
117115
cutoff
118116
)
@@ -201,14 +199,12 @@ variable's value only from the ``IntSlider`` ipywidget.
201199
})
202200
203201
cutoff = alt.param(name="cutoff", value=50)
202+
predicate = alt.datum.xval < cutoff
204203
205204
chart = alt.Chart(df).mark_point().encode(
206205
x='xval',
207206
y='yval',
208-
color=alt.condition(
209-
alt.datum.xval < cutoff,
210-
alt.value('red'), alt.value('blue')
211-
)
207+
color=alt.when(predicate).then(alt.value("red")).otherwise(alt.value("blue"))
212208
).add_params(
213209
cutoff
214210
)
@@ -253,7 +249,7 @@ the legend.
253249
chart = alt.Chart(source).mark_point().encode(
254250
x='Horsepower:Q',
255251
y='Miles_per_Gallon:Q',
256-
color=alt.condition(brush, 'Origin:N', alt.value('grey')),
252+
color=alt.when(brush).then("Origin:N").otherwise(alt.value("grey")),
257253
).add_params(brush)
258254
259255
jchart = alt.JupyterChart(chart)
@@ -306,7 +302,7 @@ extract the selected rows in the input DataFrame.
306302
chart = alt.Chart(source).mark_point().encode(
307303
x='Horsepower:Q',
308304
y='Miles_per_Gallon:Q',
309-
color=alt.condition(brush, 'Origin:N', alt.value('grey')),
305+
color=alt.when(brush).then("Origin:N").otherwise(alt.value("grey")),
310306
).add_params(brush)
311307
312308
jchart = alt.JupyterChart(chart)
@@ -344,7 +340,7 @@ is a dictionary from column names to selection intervals
344340
chart = alt.Chart(source).mark_point().encode(
345341
x='Horsepower:Q',
346342
y='Miles_per_Gallon:Q',
347-
color=alt.condition(brush, 'Cylinders:O', alt.value('grey')),
343+
color=alt.when(brush).then("Cylinders:O").otherwise(alt.value("grey")),
348344
).add_params(brush)
349345
350346
jchart = alt.JupyterChart(chart)
@@ -399,7 +395,7 @@ is used to combine the chart and HTML table in a column layout.
399395
chart_widget = alt.JupyterChart(alt.Chart(source).mark_point().encode(
400396
x='Horsepower:Q',
401397
y='Miles_per_Gallon:Q',
402-
color=alt.condition(brush, 'Cylinders:O', alt.value('grey')),
398+
color=alt.when(brush).then("Cylinders:O").otherwise(alt.value("grey")),
403399
).add_params(brush))
404400
405401
table_widget = HTML(value=source.iloc[:0].to_html())

0 commit comments

Comments
 (0)