Skip to content

Commit 1af89f2

Browse files
committed
fine-tuning "Plotting with Vega-Altair"; closes #300
1 parent 91c4b7b commit 1af89f2

File tree

3 files changed

+216
-83
lines changed

3 files changed

+216
-83
lines changed

content/plotting-vega-altair.md

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Plotting with [Vega-Altair](https://altair-viz.github.io/)
22

3+
:::{objectives}
4+
- Be able to create simple plots with Vega-Altair and tweak them
5+
- Know how to look for help
6+
- Reading data with Pandas from disk or a web resource
7+
- Know how to tweak example plots from a gallery for your own purpose
8+
- We will build up
9+
[this notebook](https://nbviewer.org/github/AaltoSciComp/python-for-scicomp/blob/master/resources/notebooks/plotting.ipynb)
10+
(spoiler alert!)
11+
:::
12+
313
:::{instructor-note}
414
- 10 min: Introduction
515
- 10 min: Type-along (creating a first plot)
@@ -51,11 +61,11 @@ From [Claus O. Wilke: "Fundamentals of Data Visualization"](https://clauswilke.c
5161
- "Simple, friendly and consistent API" allows us to focus on the data
5262
visualization part and get started without too much Python knowledge
5363
- The way it **combines visual channels with data columns** can feel intuitive
54-
- Interfaces very nicely with [Pandas](https://pandas.pydata.org/)
64+
- Interfaces very nicely with [Pandas](https://pandas.pydata.org/) ({ref}`earlier episode <pandas>`)
5565
- Easy to change figures
5666
- Good documentation
5767
- Open source
58-
- Makes it easy to save figures in a number of formats
68+
- Makes it easy to save figures in a number of formats (svg, png, html)
5969
- Easy to save interactive visualizations to be used in websites
6070

6171

@@ -66,7 +76,7 @@ KlimaServiceSenter](https://seklima.met.no/observations/), Meteorologisk
6676
institutt (MET) (CC BY 4.0). The data is in CSV format (comma-separated
6777
values) and contains daily and monthly weather data for two cities in Norway:
6878
Oslo and Tromsø. You can browse the data
69-
[here](https://github.com/AaltoSciComp/python-for-scicomp/tree/master/resources/data/plotting).
79+
[here in the lesson repository](https://github.com/AaltoSciComp/python-for-scicomp/tree/master/resources/data/plotting).
7080

7181
We will use the Pandas library to read the data into a dataframe. We have learned about Pandas in an {ref}`earlier episode <pandas>`.
7282

@@ -150,7 +160,7 @@ Observe how we connect (encode) **visual channels** to data columns:
150160
:::
151161

152162
We can improve the plot by giving Vega-Altair a bit more information that the
153-
x-axis is a **time series** (T) and that we would like to see the year and
163+
x-axis is **temporal** (T) and that we would like to see the year and
154164
month (yearmonth):
155165
```{code-block} python
156166
---
@@ -163,6 +173,14 @@ alt.Chart(data_monthly).mark_bar().encode(
163173
)
164174
```
165175

176+
Apart from T (temporal), there are other
177+
[encoding data types](https://altair-viz.github.io/user_guide/encodings/index.html#encoding-data-types):
178+
- Q (quantitative)
179+
- O (ordinal)
180+
- N (nominal)
181+
- T (temporal)
182+
- G (geojson)
183+
166184
:::{figure} plotting-vega-altair/precipitation-on-top-yearmonth.svg
167185
:alt: Monthly precipitation for the cities Oslo and Tromsø over the course of a year.
168186

@@ -226,6 +244,12 @@ alt.Chart(data_monthly).mark_area(opacity=0.5).encode(
226244
Monthly temperature ranges for two cities in Norway.
227245
:::
228246

247+
:::{discussion} What other marks and other visual channels exist?
248+
- [Overview of available marks](https://altair-viz.github.io/user_guide/marks/index.html)
249+
- [Overview of available visual channels](https://altair-viz.github.io/user_guide/encodings/channels.html)
250+
- [Gallery of examples](https://altair-viz.github.io/gallery/index.html)
251+
:::
252+
229253

230254
## Exercise: Using visual channels to re-arrange plots
231255

@@ -238,7 +262,7 @@ Monthly temperature ranges for two cities in Norway.
238262
:::{figure} plotting-vega-altair/precipitation-stacked-y.svg
239263
:::
240264

241-
1. Ask the web-search or AI how to change the axis title from "precipitation"
265+
1. Ask the Internet or AI how to change the axis title from "precipitation"
242266
to "Precipitation (mm)".
243267

244268
1. Modify the temperature range plot to show the temperature ranges for the
@@ -292,7 +316,7 @@ Monthly temperature ranges for two cities in Norway.
292316
::::
293317

294318

295-
## Using visual channels
319+
## More fun with visual channels
296320

297321
Now we will try to **plot the daily data and look at snow depths**. We first
298322
read and concatenate two datasets:
@@ -372,10 +396,24 @@ With the following result:
372396
Snow depth (in cm) for the months December 2022 to May 2023 for two cities in Norway. Colored by daily max temperature. Warmer days are often followed by reduced snow depth.
373397
:::
374398

375-
:::{discussion} What other marks and other visual channels exist?
376-
- [Overview of available marks](https://altair-viz.github.io/user_guide/marks/index.html)
377-
- [Overview of available visual channels](https://altair-viz.github.io/user_guide/encodings/channels.html)
378-
- [Gallery of examples](https://altair-viz.github.io/gallery/index.html)
399+
Let's try one more change to show that we can experiment with different
400+
plot types by changing `mark_bar()` to something else, in this case `mark_circle()`:
401+
```{code-block} python
402+
---
403+
emphasize-lines: 1
404+
---
405+
alt.Chart(data_daily).mark_circle().encode(
406+
x="date",
407+
y="snow depth",
408+
color=alt.Color("max temperature").scale(scheme="plasma"),
409+
column="name",
410+
)
411+
```
412+
413+
:::{figure} plotting-vega-altair/snow-depth-circles.svg
414+
:alt: Snow depth (in cm) for the months December 2022 to May 2023 for two cities in Norway. Colored by daily max temperature. Warmer days are often followed by reduced snow depth.
415+
416+
Snow depth (in cm) for the months December 2022 to May 2023 for two cities in Norway. Colored by daily max temperature. Warmer days are often followed by reduced snow depth.
379417
:::
380418

381419

@@ -404,7 +442,6 @@ remember everything so this strategy is useful to practice:
404442
- Select an example that is close to what you have in mind
405443
- Being able to adapt it to your needs
406444
- Being able to search for help
407-
- Being able to understand help request answers (not easy)
408445

409446
::::{exercise} Plotting-2: Adapting a gallery example
410447
**This is a great exercise which is very close to real life.**
@@ -421,7 +458,7 @@ remember everything so this strategy is useful to practice:
421458
- [Heatmap](https://altair-viz.github.io/gallery/simple_heatmap.html)
422459
- [Layered histogram](https://altair-viz.github.io/gallery/layered_histogram.html)
423460
- Then try to print out the data that is used in this example just before the call of the plotting function
424-
to learn about its structure.
461+
to learn about its structure. Consider writing the data to file before changing it.
425462
- Then try to modify the data a bit.
426463
- If you have time, try to feed it different, simplified data.
427464
**This will be key for adapting the examples to your projects.**
@@ -570,7 +607,7 @@ mydata = pd.read_csv("mydata.csv")
570607
mydata
571608
```
572609

573-
Now I can replace the example with my own data (note how I could comment out
610+
Now I can replace the example with my own data (note how I now can comment out
574611
some code that I don't need any longer):
575612
```{code-block} python
576613
---
@@ -644,9 +681,10 @@ nbviewer](https://nbviewer.org/github/AaltoSciComp/python-for-scicomp/blob/maste
644681
:::{keypoints}
645682
- Browse a number of example galleries to help you choose the library
646683
that fits best your work/style.
647-
- Figures for presentation slides and figures for manuscripts have
648-
different requirements.
649-
- Think about color-vision deficiencies when choosing colors. There are color
650-
palettes optimized for this.
651684
- Minimize manual post-processing and try to script all steps.
685+
- CSV (comma-separated values) files are often a good format to store the data
686+
that we wish to plot.
687+
- Read the data into a Pandas dataframe and then plot it with Vega-Altair
688+
where you connect data columns to
689+
[visual channels](https://altair-viz.github.io/user_guide/encodings/channels.html).
652690
:::

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

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)