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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

The Geometric Objects Layer

00:00 After looking at data and aesthetics, you’ll now learn about the third important layer in plotnine, which is geometric objects. These tell plotnine how to display your data, how it’s supposed to draw it onto the graph.

00:16 So, let me make a new Markdown cell here, just to take a note on what we’re working with here, Geometric Objects. And then, again, you will need the import.

00:29 So, from plotnine import ggplot, for the data layer, aes for the aesthetics layer, and then geom_point, for example, for the geometric objects layer. Now I said, “for example” because there’s a bunch of those that all started with geom_ and they encode the different geometric objects that you want Python to draw for you.

00:55 So we’ll look at this one first, but I’m also going to show you some other ones after that. And let me introduce you to a different way of writing this that is common when you’re using plotnine, which is not putting everything into one line, but splitting it up in lines by the layers that you’re actually creating.

01:12 So if you make these brackets and open up these round parentheses in Python, then you can chain different commands together. So, you would start with the ggplot() layer, the data, where you pass in the data. Then on a new line, the next layer is going to be aesthetics, where you do the mappings from x to “class” and y to

01:37 "hwy" (highway).

01:40 And then, now, the next layer geom_point().

01:45 If you write it like this, it kind of splits it up nicely. You can see each layer separately. You know this is the data layer, this is the aesthetics layer, and this is now the geometric objects layer. Now, when you execute this, you can see a nicely, fully-fledged graph that displays the information that you want it to display.

02:03 So you have the mappings of aesthetics, highway efficiency here on the left, on the y-axis, and the class of cars on the x-axis. And now you have points drawn for each of the data points at the specific points on the graph where the data is located.

02:21 And this is what you did with this additional geometric objects layer.

02:26 Now, the cool thing about these is that you can easily swap them out. So without changing anything about the data layer or the aesthetics layer, you could use a different layer for displaying the different geometric objects.

02:41 Of course, you will need to import them, so I can say from plotnine import geom_boxplot, for example, for a box plot graph. And now I just need to change this one layer.

02:52 I’m going to say geom_boxplot() instead of geom_point(), and leave the rest as it is. And you can see a different graph gets made from that. And there’s more!

03:03 You can say from plotnine import geom_violin.

03:10 And then, again, the same thing as before, I just need to change out the geometric objects layer to get a quite different graph.

03:21 And this is the cool thing about this layered grammar of graphics, that it splits up the ideas of what makes up a graphic into these different layers. It says the data is one thing. And in this case, like for these three graphs, the data always stays the same. Now, the aesthetics, the mappings, in this case, also, you want to stick with the same things. You want to keep mapping "class" to x and "hwy" to y, right?

03:47 But you want to change out this, the way that the data is drawn onto the map. And there’s a lot of different options for that that you can look up in the documentation.

03:56 But I want you to take away this idea that by splitting up the data in these different layers, it gives you a lot of flexibility of what you want to change out. For example, here, I could change out, instead of changing out the geometric objects, I could also go in here and change the aesthetic mappings.

04:13 So I could say instead of comparing "class" to "hwy", I want to compare "class" to "cty" (city). So, how efficient are they when driving in the city?

04:22 I don’t need to change anything about the rest, and I get a different graph that encodes now what’s the fuel efficiency when the cars drive in the city. So, the layered grammar of graphics gives you this flexibility by splitting up the ideas of what makes up a graphic into separate parts that you can then adapt individually from each other.

04:43 And that pretty much sums up the main ideas about the grammar of graphics and about the three main layers that you need to work with when you’re using plotnine. In the next couple of lessons, you will learn about some additional layers that exist and that you can also adapt if you want to, but often you don’t have to because the defaults are pretty good.

05:04 See you in the next lesson!

Become a Member to join the conversation.