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 Data Layer

For more information on concepts covered in this lesson, you can check out the following resources:

00:00 Now you’ll look at the most important layers of this layered grammar of graphics one by one. You’ll start off with what is arguably the most fundamental layer, which is the data layer, because without data, there’s nothing that you can plot. So heading back over to your Jupyter Notebook, I’m going to just take a note, make sure by pressing Escape that I’m in command mode, and then convert this cell to Markdown by going to Cell > Cell Type > Markdown or pressing M when you’re in command mode.

00:34 And now I’m going to say, “We’re going to talk about data!” And I’m executing these cells by going to Cell > Run Cell or pressing Shift + Enter.

00:45 If you want to learn more about how to use Jupyter Notebooks, we have a dedicated course and tutorial on that. I will just assume some fundamental knowledge of Jupyter Notebooks for this course. Okay, so let’s talk about the data.

00:57 There are a couple of datasets that plotnine comes with, so you can import from plotnine.data. You can import, for example, the mpg dataset, which is the fuel economy dataset that you looked at before in the slides.

01:11 And then, you want to display the data, and the function for this, you also need to import it. from plotnine import ggplot. This is the one, as you can see, it’s named after the parent library from the R programming language, and this is the function that you pass your dataset to in order to register the data. So, I can do these two imports, and then I’m ready to say ggplot(mpg).

01:40 And you get this gray square of not much data. Now, just to make sure that the data is actually there, I will take a look at the head of the mpg dataset. And you can see that all the data is there and it’s the same dataset that you saw in the slides before, but once you try to plot it using plotnine and the ggplot() function, you just get this gray square, which is because we have so far only defined the first layer of the layered grammar of graphics. You said “There is data,” but you didn’t say anything about what to do with this data, so all that plotnine knows to do for now is give you this gray square, where you can, layer by layer, continue to paint your graphic on.

02:23 In the next lesson, you will put the second layer on top, which will be the aesthetics and the mappings to the different axes.

Become a Member to join the conversation.