Skip to content

Commit 8a64b75

Browse files
committed
update episode on customizing plots
1 parent 3d41776 commit 8a64b75

File tree

4 files changed

+708
-1114
lines changed

4 files changed

+708
-1114
lines changed

content/customizing-plots.md

Lines changed: 31 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
(gapminder)=
1+
# Customizing plots
22

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+
:::
411

512

613
## Loading and plotting a dataset
@@ -23,19 +30,19 @@ data = pd.read_csv(url_prefix + "gapminder_with_codes.csv")
2330
data
2431
```
2532

26-
With very few lines we can get the first plot:
33+
With very few lines we can get the first raw plot:
2734
```python
2835
alt.Chart(data).mark_point().encode(
2936
x="gdpPercap",
3037
y="lifeExp",
3138
)
3239
```
3340

34-
```{figure} img/gapminder/all-data.svg
41+
:::{figure} img/gapminder/all-data.svg
3542
:alt: First raw plot with all countries and all years.
3643

3744
First raw plot with all countries and all years.
38-
```
45+
:::
3946

4047
Observe how we connect (encode) **visual channels** to data columns:
4148
- x-coordinate with "gdpPercap"
@@ -47,18 +54,19 @@ easier to read:
4754
alt.Chart(data).mark_point().encode(x="gdpPercap", y="lifeExp")
4855
```
4956

50-
```{discussion} Let us pause and explain the code
57+
:::{discussion} Let us pause and explain the code
5158
- `alt` is a short-hand for `altair` which we imported on top of the notebook
5259
- `Chart()` is a function defined inside `altair` which takes the data as argument
5360
- `mark_point()` is a function that produces scatter plots
5461
- `encode()` is a function which encodes data columns to visual channels
55-
```
62+
:::
5663

5764

58-
## Filtering data directly in [Vega-Altair](https://altair-viz.github.io)
65+
## Filtering data
5966

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:
6270
```{code-block} python
6371
---
6472
emphasize-lines: 4
@@ -69,11 +77,13 @@ alt.Chart(data).mark_point().encode(
6977
).transform_filter(alt.datum.year == 2007).interactive()
7078
```
7179

72-
```{figure} img/gapminder/only-2007.svg
80+
:::{figure} img/gapminder/only-2007.svg
7381
:alt: Now we only keep the year 2007.
7482

7583
Now we only keep the year 2007.
76-
```
84+
:::
85+
86+
Alternatively, we could have filtered the data before plotting using pandas.
7787

7888

7989
## Using color as additional channel
@@ -92,18 +102,17 @@ alt.Chart(data).mark_point().encode(
92102
).transform_filter(alt.datum.year == 2007).interactive()
93103
```
94104

95-
```{figure} img/gapminder/color.svg
105+
:::{figure} img/gapminder/color.svg
96106
:alt: Using different colors for different continents.
97107

98108
Using different colors for different continents.
99-
```
109+
:::
100110

101111

102112
## Changing to log scale
103113

104114
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:
107116
```{code-block} python
108117
---
109118
emphasize-lines: 2-3
@@ -115,11 +124,11 @@ alt.Chart(data).mark_point().encode(
115124
).transform_filter(alt.datum.year == 2007).interactive()
116125
```
117126

118-
```{figure} img/gapminder/log-scale.svg
127+
:::{figure} img/gapminder/log-scale.svg
119128
:alt: Changing the x axis to log scale.
120129

121130
Changing the x axis to log scale.
122-
```
131+
:::
123132

124133

125134
## Improving axis titles
@@ -135,11 +144,11 @@ alt.Chart(data).mark_point().encode(
135144
).transform_filter(alt.datum.year == 2007).interactive()
136145
```
137146

138-
```{figure} img/gapminder/axis-titles.svg
147+
:::{figure} img/gapminder/axis-titles.svg
139148
:alt: Improving the axis titles.
140149

141150
Improving the axis titles.
142-
```
151+
:::
143152

144153

145154
## Faceted charts
@@ -177,86 +186,17 @@ alt.Chart(data).mark_circle().encode(
177186
).transform_filter(alt.datum.year == 2007).interactive()
178187
```
179188

180-
```{figure} img/gapminder/population-size.svg
189+
:::{figure} img/gapminder/population-size.svg
181190
:alt: Circle sizes are proportional to population sizes.
182191

183192
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`.
254193
:::
255194

256195

257196
## Title and axis values
258197

259198
In the next step we modify a number of things:
199+
- We go back to the version where all circles have the same size
260200
- Add figure title
261201
- Modify axis domains to "zoom into" the interesting part of the plot
262202
- Set axis values
@@ -590,24 +530,3 @@ tab and put it on your home page or research group website:
590530
```python
591531
chart.save("chart.html")
592532
```
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-
:::

content/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ Day 1 afternoon:
6868
Day 2 morning:
6969
- {doc}`gallery`
7070
- {doc}`customizing-plots`
71+
[![nbviewer badge](https://img.shields.io/badge/view%20on-nbviewer-brightgreen.svg)](https://nbviewer.org/github/coderefinery/data-visualization-python/blob/main/notebooks/customizing.ipynb)
72+
[![colab badge](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/coderefinery/data-visualization-python/blob/main/notebooks/customizing.ipynb)
73+
[![binder badge](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/coderefinery/data-visualization-python/HEAD?labpath=notebooks%2Fcustomizing.ipynb)
7174

7275
Day 3 morning:
7376
- Layering plots

notebooks/customizing.ipynb

Lines changed: 674 additions & 94 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)