Skip to content

Commit 1160fdc

Browse files
authored
07-visualization-ggplot-python.md: various fixes (#390)
1 parent 954dc39 commit 1160fdc

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

_episodes/07-visualization-ggplot-python.md

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ keypoints:
2323
## Disclaimer
2424

2525
Python has powerful built-in plotting capabilities such as `matplotlib`, but for
26-
this episode, we will be using the [`plotnine`](https://plotnine.readthedocs.io/en/stable/)
26+
this episode, we will be using the [`plotnine`][plotnine]
2727
package, which facilitates the creation of highly-informative plots of
28-
structured data based on the R implementation of [`ggplot2`](http://ggplot2.org/)
29-
and [The Grammar of Graphics](http://link.springer.com/book/10.1007%2F0-387-28695-0)
30-
by Leland Wilkinson. The [`plotnine`](https://plotnine.readthedocs.io/en/stable/)
28+
structured data based on the R implementation of [`ggplot2`][ggplot2]
29+
and [The Grammar of Graphics][grammar-of-graphics]
30+
by Leland Wilkinson. The `plotnine`
3131
package is built on top of Matplotlib and interacts well with Pandas.
3232

3333
Just as with the other packages, `plotnine` need to be imported. It is good
@@ -51,9 +51,9 @@ surveys_complete = surveys_complete.dropna()
5151
~~~
5252
{: .language-python}
5353

54-
# Plotting with plotnine
54+
## Plotting with plotnine
5555

56-
The `plotnine` package (cfr. other packages conform [The Grammar of Graphics](http://link.springer.com/book/10.1007%2F0-387-28695-0)) supports the creation of complex plots from data in a
56+
The `plotnine` package (cfr. other packages conform The Grammar of Graphics) supports the creation of complex plots from data in a
5757
dataframe. It uses default settings, which help creating publication quality
5858
plots with a minimal amount of settings and tweaking.
5959

@@ -148,7 +148,7 @@ and `y` axis you set up in `aes()`.
148148
- You can also specify aesthetics for a given `geom` independently of the
149149
aesthetics defined globally in the `ggplot()` function.
150150
151-
# Building your plots iteratively
151+
## Building your plots iteratively
152152
153153
Building plots with `plotnine` is typically an iterative process. We start by
154154
defining the dataset we'll use, lay the axes, and choose a geom. Hence, the
@@ -258,7 +258,7 @@ set the background to white using the function `theme_bw()`.
258258
> Adapt the bar plot of the previous exercise by mapping the `sex` variable to
259259
> the color fill of the bar chart. Change the `scale` of the color fill by
260260
> providing the colors `blue` and `orange` manually
261-
> (see [API reference](https://plotnine.readthedocs.io/en/stable/api.html#Color-and-fill-scales) to find the appropriate function).
261+
> (see [API reference][plotnine-api] to find the appropriate function).
262262
>
263263
> > ## Answers
264264
> >
@@ -276,7 +276,7 @@ set the background to white using the function `theme_bw()`.
276276
{: .challenge}
277277
278278
279-
# Plotting distributions
279+
## Plotting distributions
280280
281281
Visualizing distributions is a common task during data exploration and
282282
analysis. To visualize the distribution of `weight` within each `species_id`
@@ -345,7 +345,7 @@ better idea of the number of measurements and of their distribution:
345345
{: .challenge}
346346
347347
348-
# Plotting time series data
348+
## Plotting time series data
349349
350350
Let's calculate number of counts per year for each species. To do that we need
351351
to group data first and count the species (`species_id`) within each group.
@@ -395,7 +395,7 @@ modifying the aesthetic function and map the species_id to the color:
395395
396396
![png](../fig/06_time_plot.png)
397397
398-
# Faceting
398+
## Faceting
399399
400400
As any other library supporting the Grammar of Graphics, `plotnine` has a
401401
special technique called *faceting* that allows to split one plot into multiple
@@ -506,12 +506,12 @@ survey_2000 = surveys_complete[surveys_complete["year"].isin([2000, 2001])]
506506
{: .challenge}
507507
508508
509-
# Further customization
509+
## Further customization
510510
511511
As the syntax of `plotnine` follows the original R package `ggplot2`, the
512512
documentation of `ggplot2` can provide information and inspiration to customize
513-
graphs. Take a look at the `ggplot2` [cheat sheet](https://www.rstudio.com/wp-content/uploads/2015/08/ggplot2-cheatsheet.pdf), and think of ways to improve the plot. You can write down some
514-
of your ideas as comments in the Etherpad.
513+
graphs. Take a look at the `ggplot2` [cheat sheet][ggplot2-cheat-sheet], and think of ways to
514+
improve the plot. You can write down some of your ideas as comments in the Etherpad.
515515
516516
The theming options provide a rich set of visual adaptations. Consider the
517517
following example of a bar plot with the counts per year.
@@ -563,13 +563,13 @@ my_custom_theme = p9.theme(axis_text_x = p9.element_text(color="grey", size=10,
563563
564564
> ## Challenge - customization
565565
> Please take another five minutes to either improve one of the plots
566-
generated in this exercise or create a beautiful graph of your own.
566+
> generated in this exercise or create a beautiful graph of your own.
567567
>
568568
> Here are some ideas:
569569
>
570570
> * See if you can change thickness of lines for the line plot .
571571
> * Can you find a way to change the name of the legend? What about its labels?
572-
> * Use a different color palette (see http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/)
572+
> * Use a different color palette (see <http://www.cookbook-r.com/Graphs/Colors_(ggplot2)>)
573573
{: .challenge}
574574
575575
@@ -587,5 +587,10 @@ my_plot.save("scatterplot.png", width=10, height=10, dpi=300)
587587
~~~
588588
{: .language-python}
589589
590-
{% include links.md %}
590+
[ggplot2-cheat-sheet]: https://www.rstudio.com/wp-content/uploads/2015/08/ggplot2-cheatsheet.pdf
591+
[ggplot2]: https://ggplot2.tidyverse.org
592+
[grammar-of-graphics]: http://link.springer.com/book/10.1007%2F0-387-28695-0
593+
[plotnine-api]: https://plotnine.readthedocs.io/en/stable/api.html#Color-and-fill-scales
594+
[plotnine]: https://plotnine.readthedocs.io/en/stable
591595
596+
{% include links.md %}

0 commit comments

Comments
 (0)