Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

The Aesthetics Layer

00:00 In the previous lesson, you created the data layer, but all you got was a gray square. In this lesson, you will apply aesthetics to this data layer and put them together to end up with a graph that’s already more meaningful than the previous one.

00:16 So, head back over to your Jupyter Notebook and create a new cell. I will convert this again to Markdown and just

00:26 leave a note on what we’re doing, Aesthetics.

00:30 And you will need an additional function for displaying the aesthetics, so I will import this. from plotnine import aes (aesthetics), this is what this function is called.

00:44 And just to make sure that you remember, also ggplot() was the one that you imported before that you need to assign the data. And with this, you already have everything you need.

00:55 You can say ggplot(), passing in the mpg dataset. So here, you’re passing in the mpg dataset and creating the data layer, and then you’re adding aesthetics to it.

01:08 Now the mappings, let’s take another look at the mpg dataset, just to know what are the columns that you want to map. Now, you want to map class to the x-axis, so you can say x="class".

01:24 And it’s enough to just give the names of the columns in the DataFrame that you passed into the data layer.

01:31 And y is going to be the efficiency on the highway, so how many miles it can drive per liter. And with these two inputs, you’re done. You can execute this, and you get a graph that’s already much more meaningful than the previous one.

01:47 So, in here, you can see that the highway column was mapped to the y-axis, and the class column was mapped to the x-axis.

01:57 There’s also a couple of other things that happened behind the scenes. So, plotnine applied a coordinate system for you, just by default. This is the Cartesian coordinate system.

02:09 And it also applied some scales that it just inferred by looking at the data that you’re passing in. So, these are defaults that are usually quite good.

02:17 You don’t have to change them yourself. But just know that these are additional layers that you can also adapt if you want to. Now, while this looks much better and you applied two aesthetics successfully, you still don’t see anything because there’s no geometric objects defined, so plotnine doesn’t know how to plot the data points for you.

02:37 You will learn how to do that in the next lesson.

Become a Member to join the conversation.