Skip to content

Commit d475105

Browse files
Merge pull request #330 from AaltoSciComp/course-2025/vega-altair
content/vega-altair: Added some new content for the 2025 run
2 parents 34c06c6 + 5449fce commit d475105

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

content/plotting-vega-altair.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,62 @@ Monthly temperature ranges for two cities in Norway.
318318
::::
319319

320320

321+
## Customizing channels for extra beauty
322+
323+
All of the visual channels can be modified with multiple different parameters.
324+
The shorthand notation for specifying the channels can be modified to use channel
325+
objects initialized by the user.
326+
327+
For the whole list of available channels and corresponding classes, see
328+
[this page](https://altair-viz.github.io/user_guide/encodings/channels.html).
329+
330+
331+
For example, we can rename channels to make them more beautiful:
332+
333+
```python
334+
alt.Chart(data_monthly).mark_area(opacity=0.5).encode(
335+
x=alt.X("yearmonth(date):T", title="Date"),
336+
y=alt.Y("max temperature", title="Temperature range (min-max)"),
337+
y2="min temperature",
338+
color=alt.Color("name", title="Station name"),
339+
)
340+
```
341+
342+
:::{figure} plotting-vega-altair/channels-renamed.svg
343+
:alt: Monthly temperature ranges for two cities in Norway.
344+
345+
Monthly temperature ranges for two cities in Norway.
346+
:::
347+
348+
## Saving charts
349+
350+
Saving charts is easy: you just call the
351+
[chart.save()](https://altair-viz.github.io/user_guide/saving_charts.html)-method.
352+
353+
```python
354+
alt.Chart(data_monthly).mark_area(opacity=0.5).encode(
355+
x=alt.X("yearmonth(date):T", title="Date"),
356+
y=alt.Y("max temperature", title="Temperature range (min-max)"),
357+
y2="min temperature",
358+
color=alt.Color("name", title="Station name"),
359+
).save("temperature_range.png", ppi=144)
360+
```
361+
362+
Altair supports png, svg and pdf saving. For png files you can adjust the image
363+
resolution by giving an extra keyword argument (`ppi`).
364+
365+
It also supports exporting the image as
366+
[HTML](https://altair-viz.github.io/user_guide/saving_charts.html#html-format)
367+
or as
368+
[JSON](https://altair-viz.github.io/user_guide/saving_charts.html#json-format)
369+
that can be embedded
370+
to any web page.
371+
372+
You can even use
373+
[chart.to_url()](https://altair-viz.github.io/user_guide/saving_charts.html#sharable-url)-method
374+
to get a shareable url that you can view in an online editor (remember that
375+
the URL will also contain some of the data used to create the plot).
376+
321377
## More fun with visual channels
322378

323379
Now we will try to **plot the daily data and look at snow depths**. We first
@@ -419,6 +475,38 @@ Snow depth (in cm) for the months December 2022 to May 2023 for two cities in No
419475
:::
420476

421477

478+
## Data transformations
479+
480+
Vega-Altair also provides many different
481+
[data transformations](https://altair-viz.github.io/user_guide/transform/index.html)
482+
that you can use to quickly modify plots.
483+
484+
For example, lets plot weekly average snowfall:
485+
486+
```python
487+
alt.Chart(data_daily).mark_bar().encode(
488+
x="week(date)",
489+
y="mean(snow depth)",
490+
color="max temperature",
491+
column="name",
492+
)
493+
```
494+
495+
Here we have used the `mean`-transform from
496+
[Aggregate](https://altair-viz.github.io/user_guide/transform/aggregate.html)-transformations
497+
and `week`-transform from
498+
[TimeUnit](https://altair-viz.github.io/user_guide/transform/timeunit.html)-transformations.
499+
500+
Vega-Altair will then automatically do a group by-operation over weekly periods
501+
across aggregated columns.
502+
503+
:::{figure} plotting-vega-altair/weekly-snow-depth.svg
504+
:alt: Weekly mean snow depth (in cm) for the months December 2022 to May 2023 for two cities in Norway. Colored by daily max temperature.
505+
506+
Weekly mean snow depth (in cm) for the months December 2022 to May 2023 for two cities in Norway. Colored by weekly max temperature.
507+
508+
:::
509+
422510
## Themes
423511

424512
In [Vega-Altair](https://altair-viz.github.io/) you can change the theme and
Lines changed: 1 addition & 0 deletions
Loading

content/plotting-vega-altair/weekly-snow-depth.svg

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)