1
- (gapminder)=
1
+ # Customizing plots
2
2
3
- # Plotting the Gapminder dataset
3
+ :::{objectives}
4
+ - Know where to look to find out how to tweak plots
5
+ - Start with a relatively simple example and build up more and more features
6
+ - See the process of going from a raw plot towards a publication-ready plot
7
+ - We will build up
8
+ [ this notebook] ( https://nbviewer.org/github/coderefinery/data-visualization-python/blob/main/notebooks/customizing.ipynb )
9
+ (spoiler alert!)
10
+ :::
4
11
5
12
6
13
## Loading and plotting a dataset
@@ -23,19 +30,19 @@ data = pd.read_csv(url_prefix + "gapminder_with_codes.csv")
23
30
data
24
31
```
25
32
26
- With very few lines we can get the first plot:
33
+ With very few lines we can get the first raw plot:
27
34
``` python
28
35
alt.Chart(data).mark_point().encode(
29
36
x = " gdpPercap" ,
30
37
y = " lifeExp" ,
31
38
)
32
39
```
33
40
34
- ``` {figure} img/gapminder/all-data.svg
41
+ ::: {figure} img/gapminder/all-data.svg
35
42
:alt: First raw plot with all countries and all years.
36
43
37
44
First raw plot with all countries and all years.
38
- ```
45
+ :::
39
46
40
47
Observe how we connect (encode) ** visual channels** to data columns:
41
48
- x-coordinate with "gdpPercap"
@@ -47,18 +54,19 @@ easier to read:
47
54
alt.Chart(data).mark_point().encode(x = " gdpPercap" , y = " lifeExp" )
48
55
```
49
56
50
- ``` {discussion} Let us pause and explain the code
57
+ ::: {discussion} Let us pause and explain the code
51
58
- ` alt ` is a short-hand for ` altair ` which we imported on top of the notebook
52
59
- ` Chart() ` is a function defined inside ` altair ` which takes the data as argument
53
60
- ` mark_point() ` is a function that produces scatter plots
54
61
- ` encode() ` is a function which encodes data columns to visual channels
55
- ```
62
+ :::
56
63
57
64
58
- ## Filtering data directly in [ Vega-Altair ] ( https://altair-viz.github.io )
65
+ ## Filtering data
59
66
60
- In [ Vega-Altair] ( https://altair-viz.github.io ) we can chain functions. Let us
61
- add two more:
67
+ In [ Vega-Altair] ( https://altair-viz.github.io ) we can chain functions. Let us
68
+ add two more functions: The first will apply a filter, the second will make the
69
+ plot interactive:
62
70
``` {code-block} python
63
71
---
64
72
emphasize-lines: 4
@@ -69,11 +77,13 @@ alt.Chart(data).mark_point().encode(
69
77
).transform_filter(alt.datum.year == 2007).interactive()
70
78
```
71
79
72
- ``` {figure} img/gapminder/only-2007.svg
80
+ ::: {figure} img/gapminder/only-2007.svg
73
81
:alt: Now we only keep the year 2007.
74
82
75
83
Now we only keep the year 2007.
76
- ```
84
+ :::
85
+
86
+ Alternatively, we could have filtered the data before plotting using pandas.
77
87
78
88
79
89
## Using color as additional channel
@@ -92,18 +102,17 @@ alt.Chart(data).mark_point().encode(
92
102
).transform_filter(alt.datum.year == 2007).interactive()
93
103
```
94
104
95
- ``` {figure} img/gapminder/color.svg
105
+ ::: {figure} img/gapminder/color.svg
96
106
:alt: Using different colors for different continents.
97
107
98
108
Using different colors for different continents.
99
- ```
109
+ :::
100
110
101
111
102
112
## Changing to log scale
103
113
104
114
For this data set we will get a better insight when switching the x-axis from
105
- linear to log scale (we changed two lines to show both the "method syntax" and
106
- the "attribute syntax"):
115
+ linear to log scale:
107
116
``` {code-block} python
108
117
---
109
118
emphasize-lines: 2-3
@@ -115,11 +124,11 @@ alt.Chart(data).mark_point().encode(
115
124
).transform_filter(alt.datum.year == 2007).interactive()
116
125
```
117
126
118
- ``` {figure} img/gapminder/log-scale.svg
127
+ ::: {figure} img/gapminder/log-scale.svg
119
128
:alt: Changing the x axis to log scale.
120
129
121
130
Changing the x axis to log scale.
122
- ```
131
+ :::
123
132
124
133
125
134
## Improving axis titles
@@ -135,11 +144,11 @@ alt.Chart(data).mark_point().encode(
135
144
).transform_filter(alt.datum.year == 2007).interactive()
136
145
```
137
146
138
- ``` {figure} img/gapminder/axis-titles.svg
147
+ ::: {figure} img/gapminder/axis-titles.svg
139
148
:alt: Improving the axis titles.
140
149
141
150
Improving the axis titles.
142
- ```
151
+ :::
143
152
144
153
145
154
## Faceted charts
@@ -177,86 +186,17 @@ alt.Chart(data).mark_circle().encode(
177
186
).transform_filter(alt.datum.year == 2007).interactive()
178
187
```
179
188
180
- ``` {figure} img/gapminder/population-size.svg
189
+ ::: {figure} img/gapminder/population-size.svg
181
190
:alt: Circle sizes are proportional to population sizes.
182
191
183
192
Circle sizes are proportional to population sizes.
184
- ```
185
-
186
- ---
187
-
188
- ``` {discussion} Where to go from here?
189
- In few steps and few lines of code we have achieved a lot!
190
-
191
- These plots are perhaps not publication quality yet but we will learn how to
192
- customize and improve in {ref}`customizing-plots`.
193
- ```
194
-
195
- ``` {keypoints}
196
- - Avoid manual post-processing, try to script all steps.
197
- - Browse a number of example galleries to help you choose the library that fits best your work/style.
198
- - Figures for presentation slides and figures for manuscripts have different
199
- requirements. More about that later.
200
- ```
201
- (customizing-plots)=
202
-
203
- # Customizing plots
204
-
205
- :::{objectives}
206
- - Know where to look to find out how to tweak plots
207
- - Be able to prepare a plot for publication
208
- - Know how to tweak example plots from a gallery for your own purpose
209
- - Start with a relatively simple example and build up more and more features
210
- - We will build up
211
- [ this notebook] ( https://nbviewer.org/github/coderefinery/data-visualization-python/blob/main/notebooks/customizing.ipynb )
212
- (spoiler alert!)
213
- :::
214
-
215
- [ this lesson is adapted from < https://aaltoscicomp.github.io/python-for-scicomp/data-visualization/ > ]
216
-
217
-
218
- ## Styling and customizing plots
219
-
220
- - Before you customize plots "manually" using a graphical program, please
221
- consider how this affects reproducibility.
222
- - ** Try to minimize manual post-processing** . This might bite you when you
223
- need to regenerate 50 figures one day before submission deadline or
224
- regenerate a set of figures after the person who created them left the group.
225
- - All the plotting libraries in Python allow to customize almost every aspect
226
- of a plot.
227
-
228
-
229
- ## Starting point
230
-
231
- This is where we left off at the end of {ref}` gapminder ` :
232
-
233
- ``` python
234
- # import necessary libraries
235
- import altair as alt
236
- import pandas as pd
237
-
238
- # read the data
239
- url_prefix = " https://raw.githubusercontent.com/plotly/datasets/master/"
240
- data = pd.read_csv(url_prefix + " gapminder_with_codes.csv" )
241
-
242
- # generate an interactive plot
243
- alt.Chart(data).mark_point().encode(
244
- x = alt.X(" gdpPercap" ).scale(type = " log" ).title(" GDP per capita (PPP dollars)" ),
245
- y = alt.Y(" lifeExp" ).title(" Life expectancy (years)" ),
246
- color = " continent" ,
247
- ).transform_filter(alt.datum.year == 2007 ).interactive()
248
- ```
249
-
250
- :::{figure} img/gapminder/axis-titles.svg
251
- :alt: Our example plot at the end of "Generating our first plot" episode.
252
-
253
- Our example plot at the end of {ref}` gapminder ` .
254
193
:::
255
194
256
195
257
196
## Title and axis values
258
197
259
198
In the next step we modify a number of things:
199
+ - We go back to the version where all circles have the same size
260
200
- Add figure title
261
201
- Modify axis domains to "zoom into" the interesting part of the plot
262
202
- Set axis values
@@ -590,24 +530,3 @@ tab and put it on your home page or research group website:
590
530
``` python
591
531
chart.save(" chart.html" )
592
532
```
593
-
594
-
595
- ## Themes
596
-
597
- In [ Vega-Altair] ( https://altair-viz.github.io/ ) you can change the theme and
598
- select from a long [ list of themes] ( https://github.com/vega/vega-themes ) . On
599
- top of your notebook try to add:
600
- ``` python
601
- alt.themes.enable(' dark' )
602
- ```
603
- Then re-run all cells. Later you can try some other themes such as:
604
- - ` fivethirtyeight `
605
- - ` latimes `
606
- - ` urbaninstitute `
607
-
608
- You can even define your own themes!
609
-
610
- :::{keypoints}
611
- - Think about color-vision deficiencies when choosing colors. Use existing
612
- solutions for this problem.
613
- :::
0 commit comments