In this course, we will be use the Plotnine library [1] to create plots in Python. Plotnine uses a style of plotting known as "Grammar of Graphics". The name stems from the idea that you build graphics from the bottom up using a specific syntax, similar to how you build a sentence up using specific grammar! For example, you could write a basic sentence: "The fox jumps." It has all of the necessary components, but is fairly simple. You could increase the complexity of the sentence by adding some adjectives: "The quick, brown fox jumps." Next, you could keep building this sentence with a preposition: "The quick, brown fox jumps over the dog." Finally, you could add another adjective, adding further complexity to the sentence: "The quick, brown fox jumps over the lazy dog." In this way, we will use the "Grammar of Graphics" to start simple and build the complexity of our plots.
There are three key components of any "Grammar of Graphics" (or "ggplot") plot:
1 | ggplot(...) |
1 | aes(...) |
1 | geom_xyz(...) |
The most basic syntax, similar to our basic sentence above, is:
1 | ggplot(...) + geom_xyz(aes(...), ...) |
As the plots get more advanced, we can add things like labels:
1 | ggplot(...) + geom_xyz(aes(...), ...) + xlab(...) + ylab(...) |
Or axis controls via `theme`:
1 | ggplot(...) + geom_xyz(aes(...), ...) + xlab(...) + ylab(...) + theme(...) |
Other plotting tools in Python include matplotlib [2] and seaborn [3], but for the purpose of this co