EME 210
Data Analytics for Energy Systems

Introduction to Plotnine

PrintPrint

Introduction to Plotnine

  Read It: Introduction to Plotnine

In this course, we will be use the Plotnine library 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(...)
    This command starts the plotting object and specify the pandas dataframe that you are going to use for visualization
  2. aes(...)
    This command defines the variables that you are plotting
  3. geom_xyz(...)
    This command defines the type of plot. You can use points, lines, bars, and many others. For example, in today's tutorial, we use geom_bar as demo for plotting histogram.

The most basic syntax, similar to our basic sentence above, is: 

ggplot(...) + geom_xyz(aes(...), ...)

As the plots get more advanced, we can add things like labels:

ggplot(...) + geom_xyz(aes(...), ...) + xlab(...) + ylab(...)

Or axis controls via `theme`:

ggplot(...) + geom_xyz(aes(...), ...) + xlab(...) + ylab(...) + theme(...)

Other plotting tools in Python include matplotlib and seaborn, but for the purpose of this co

 Assess It

Knowledge Check