Skip to content

Commit 131ccd4

Browse files
authored
Chapitre de visualisation: plus de place pour la grammaire des graphiques (#643)
* matplotlib intro * Gros chantier sur le chapitre * gros chantier * refonte du chapitre
1 parent ca020f2 commit 131ccd4

File tree

12 files changed

+900
-493
lines changed

12 files changed

+900
-493
lines changed

_quarto.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ format:
6363
sidebar-width: 250px
6464
body-width: 900px
6565
margin-width: 300px
66+
highlight-style: monokai
6667
ipynb: default
6768

6869

content/visualisation/01_matplotlib/_exo1_solution.qmd

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,50 @@ p1 = df1.plot(kind = "barh", color = 'red')
3131
```
3232

3333
:::: {.content-visible when-profile="fr"}
34-
Figure 1, sans travail sur le style, présente les données sous forme de _barplot_ basique. Bien qu'elle montre les informations essentielles, elle manque de mise en page esthétique, de couleurs harmonieuses et d'annotations claires, nécessaires pour améliorer la lisibilité et l'impact visuel.
34+
La @fig-figure1-exo1-fr présente les données sous forme de _barplot_ basique. Bien qu'elle montre les informations essentielles, elle manque de mise en page esthétique, de couleurs harmonieuses et d'annotations claires, nécessaires pour améliorer la lisibilité et l'impact visuel.
3535
::::
3636

3737
:::: {.content-visible when-profile="en"}
38-
Figure 1, without any styling, displays the data in a basic _barplot_. While it conveys the essential information, it lacks aesthetic layout, harmonious colors, and clear annotations, which are necessary to improve readability and visual impact.
38+
@fig-figure1-exo1-en, displays the data in a basic _barplot_. While it conveys the essential information, it lacks aesthetic layout, harmonious colors, and clear annotations, which are necessary to improve readability and visual impact.
3939
::::
4040

4141

4242
:::: {.cell .markdown}
43-
<details>
43+
<details open>
44+
45+
::: {.content-visible when-profile="fr"}
4446

4547
<summary>
46-
Figure 1
48+
Figure 1 (cliquer ici pour rétracter)
4749
</summary>
4850

51+
52+
```{python}
53+
#| echo: false
54+
#| fig-cap: "Première ébauche de la figure \"Les 10 compteurs avec la moyenne horaire la plus élevée\""
55+
#| label: fig-figure1-exo1-fr
56+
p1.figure
57+
```
58+
59+
:::
60+
61+
::: {.content-visible when-profile="en"}
62+
63+
<summary>
64+
Figure 1 (click here to mask)
65+
</summary>
66+
67+
4968
```{python}
5069
#| echo: false
70+
#| fig-cap: "First draft for 'The 10 meters with the highest hourly average'"
71+
#| label: fig-figure1-exo1-en
5172
p1.figure
5273
```
5374

75+
:::
76+
77+
5478
</details>
5579
::::
5680

@@ -74,30 +98,39 @@ p2 = df2.plot(kind = "barh", color = 'green')
7498

7599
::::: {.cell .markdown}
76100

77-
<details>
101+
<details open>
78102

79103
:::: {.content-visible when-profile="fr"}
80104

81105
<summary>
82-
Figure 2 sans travail sur le style:
106+
Figure 2 sans travail sur le style (cliquer ici pour rétracter):
83107
</summary>
84108

109+
```{python}
110+
#| echo: false
111+
#| fig-cap: "Première ébauche de la figure \"Les 10 compteurs ayant comptabilisés le plus de vélos\""
112+
#| label: fig-figure2-exo1-fr
113+
p2.figure
114+
```
115+
85116
::::
86117

87118
:::: {.content-visible when-profile="en"}
88119

89120
<summary>
90-
Figure 2 without styling:
121+
Figure 2 without styling (click here to mask):
91122
</summary>
92123

93-
::::
94-
95-
96124
```{python}
97125
#| echo: false
126+
#| fig-cap: "First draft of the figure 'The 10 counters that recorded the most bicycles'"
127+
#| label: fig-figure2-exo1-en
98128
p2.figure
99129
```
100130

131+
::::
132+
133+
101134
</details>
102135

103136
:::::

content/visualisation/01_matplotlib/_exo2.qmd

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,56 @@
1+
::: {.content-visible when-profile="fr"}
2+
Nous allons nous contenter de reproduire la @fig-figure1-exo1-fr avec `seaborn`. Pour cela, voici le code nécessaire afin d'avoir un `DataFrame` prêt à l'emploi:
3+
:::
4+
5+
::: {.content-visible when-profile="en"}
6+
We will simply reproduce @fig-figure1-exo1-en with `seaborn`. To do this, here is the code needed to have a ready-to-use `DataFrame`:
7+
:::
8+
9+
```{python}
10+
#| echo: true
11+
#| label: preparation-exo2
12+
#| code-fold: true
13+
df1 = (
14+
df
15+
.groupby('nom_compteur')
16+
.agg({'sum_counts': "mean"})
17+
.sort_values('sum_counts', ascending = False)
18+
.head(10)
19+
.sort_values('sum_counts')
20+
)
21+
22+
23+
df1 = df1.reset_index().sort_values("sum_counts", ascending = False)
24+
25+
df1.head()
26+
```
27+
128
:::: {.content-visible when-profile="fr"}
229

330
::: {.callout-tip}
4-
## Exercice 2: reproduire la première figure avec seaborn
31+
## Exercice 2: reproduire la première figure avec `seaborn`
532

6-
1. Réinitialiser l'index des _dataframes_ `df1` et `df2`
7-
pour avoir une colonne *'Nom du compteur'*. Réordonner les données
8-
de manière décroissante pour obtenir un graphique ordonné dans
9-
le bon sens avec `seaborn`.
33+
1. Refaire le graphique précédent avec la fonction `catplot` de `seaborn`. Pour contrôler la taille du graphique vous pouvez utiliser les arguments `height` et `aspect`.
1034

11-
2. Refaire le graphique précédent avec la fonction `catplot` de `seaborn`. Pour
12-
contrôler la taille du graphique vous pouvez utiliser les arguments `height` et
13-
`aspect`.
35+
2. Ajouter les titres des axes et le titre du graphique
1436

15-
3. Ajouter les titres des axes et le titre du graphique pour le premier graphique
16-
17-
4. Essayez de colorer en rouge l'axe des `x`. Vous pouvez pré-définir un
18-
style avec `sns.set_style("ticks", {"xtick.color": "red"})`
37+
3. Même si cela n'apporte rien en termes d'information, essayez de colorer en rouge, comme sur la figure du portail _open data_, l'axe des `x`. Vous pouvez pré-définir un
38+
style avec `sns.set_style("ticks", {"xtick.color": "red"})`
1939

2040
:::
2141

2242
::::
2343

2444
:::: {.content-visible when-profile="en"}
2545
::: {.callout-tip}
26-
## Exercise 2: Reproduce the First Figure with Seaborn
27-
28-
1. Reset the index of the dataframes `df1` and `df2` to have a column *'Nom du compteur'*. Reorder the data in descending order to obtain a correctly ordered graph with `seaborn`.
46+
## Exercise 2: reproduce the first figure with `seaborn`
2947

30-
2. Redo the previous graph using seaborn's `catplot` function. To control the size of the graph, you can use the `height` and `aspect` arguments.
48+
1. Redraw the previous graph using the `catplot` function from `seaborn`. To control the size of the graph, you can use the `height` and `aspect` arguments.
3149

32-
3. Add axis titles and the graph title for the first graph.
50+
2. Add axis titles and a title to the graph.
3351

34-
4. Try coloring the `x` axis in red. You can pre-define a style with `sns.set_style("ticks", {"xtick.color": "red"})`.
52+
3. Even if it does not add any information, try colouring the `x` axis red, as in the figure on the _open data_ portal. You can predefine a
53+
style with `sns.set_style('ticks', {'xtick.color': 'red'})`.
3554

3655
:::
3756

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
```{python}
2-
#| output: false
3-
4-
# Question 1. Reset index and order
5-
df1 = df1.reset_index().sort_values("sum_counts", ascending = False)
6-
df2 = df2.reset_index().sort_values("sum_counts", ascending = False)
7-
```
8-
91
```{python}
102
#| output: false
113
import seaborn as sns
@@ -20,26 +12,34 @@ g = sns.catplot(
2012
```
2113

2214
::: {.content-visible when-profile="fr"}
23-
À l'issue de la question 2, c'est-à-dire en utilisant `seaborn` pour reproduire de manière minimale un _barplot_, on obtient :
15+
À l'issue de la question 1, c'est-à-dire en utilisant `seaborn` pour reproduire de manière minimale un _barplot_, on obtient la @fig-fig1-exo2-fr. C'est déjà un peu plus propre que la version précédente (@fig-figure1-exo1-fr) et cela peut déjà suffire pour un travail exploratoire.
2416
:::
2517

2618
::: {.content-visible when-profile="en"}
27-
At the end of question 2, that is, by using `seaborn` to minimally reproduce a _barplot_, we get:
19+
At the end of question 1, i.e. using `seaborn` to reproduce a minimal barplot, we obtain @fig-fig1-exo2-en. This is already a little cleaner than the previous version (@fig-figure1-exo1-en) and may already be sufficient for exploratory work.
2820
:::
2921

22+
::: {.content-visible when-profile="fr"}
3023

3124
```{python}
3225
#| echo: false
33-
g
34-
plt.show()
26+
#| label: fig-fig1-exo2-fr
27+
#| caption: "Deuxième ébauche de la figure \"Les 10 compteurs avec la moyenne horaire la plus élevée\""
28+
g.fig
3529
```
3630

37-
::: {.content-visible when-profile="fr"}
38-
Après quelques réglages esthétiques, à l'issue des questions 3 et 4, on obtient une figure proche de celle du portail _open data_ parisien.
3931
:::
4032

33+
4134
::: {.content-visible when-profile="en"}
42-
After some aesthetic adjustments, at the end of questions 3 and 4, we get a figure close to that of the Paris _open data_ portal.
35+
36+
```{python}
37+
#| echo: false
38+
#| label: fig-fig1-exo2-en
39+
#| caption: "Second draft for \"Les 10 compteurs avec la moyenne horaire la plus élevée\""
40+
g.fig
41+
```
42+
4343
:::
4444

4545

@@ -57,11 +57,11 @@ plt.title('Les 10 compteurs avec la moyenne horaire la plus élevée')
5757
```
5858

5959
::: {.content-visible when-profile="fr"}
60-
Les paramètres supplémentaires proposés à la question 4 permettent finalement d'obtenir la figure
60+
A la fin de l'exercice, on obtient une figure proche de celle qu'on essaie de reproduire. La principale différence tient à l'absence, sur la nôtre, des valeurs numériques.
6161
:::
6262

6363
::: {.content-visible when-profile="en"}
64-
The additional parameters proposed in question 4 ultimately allow us to obtain the figure
64+
At the end of the exercise, we obtain a figure close to the one we are trying to reproduce. The main difference is that ours does not include numerical values.
6565
:::
6666

6767
```{python}
@@ -72,12 +72,23 @@ sns.set_style("ticks", {"xtick.color": "red"})
7272
g = sns.catplot(x='sum_counts', y='nom_compteur', data=df1, kind = "bar", height = 10, aspect = 2, color = "red")
7373
g.set_axis_labels('Moyenne du comptage par heure sur la période sélectionnée', 'Nom du compteur')
7474
plt.title('Les 10 compteurs avec la moyenne horaire la plus élevée')
75-
plt.savefig('top10_sns.png', bbox_inches='tight')
7675
```
76+
:::
7777

78+
::: {.content-visible when-profile="fr"}
7879
```{python}
7980
#| echo: false
80-
g
81-
plt.show()
81+
#| label: fig-fig2-exo2-fr
82+
#| fig-caption: "Dernière ébauche de la figure \"Les 10 compteurs avec la moyenne horaire la plus élevée\""
83+
g.fig
8284
```
85+
:::
8386

87+
::: {.content-visible when-profile="en"}
88+
```{python}
89+
#| echo: false
90+
#| label: fig-fig2-exo2-en
91+
#| fig-caption: "Final draft for \"Les 10 compteurs avec la moyenne horaire la plus élevée\" using seaborn"
92+
g.fig
93+
```
94+
:::

0 commit comments

Comments
 (0)