The Coordinate Systems Layer
00:00 In this lesson, you’re going to take a look at another layer that is often just taken care of for you by plotnine with a reasonable default, which is coordinate systems.
00:10
You’re going to explicitly change the coordinate system to some of the data that you’re displaying. Let’s head back over to the Notebook. You’re going to work again with the mpg
dataset from the beginning of the course, so import it again if you don’t have it loaded anymore, mpg
.
00:28 And then, also, always good to take a look at it.
00:34
So this is what the dataset looks like. And now, you could build a graph for that, for example, just using a bar plot. You will have to import again, so I’m going to say from plotnine import
, data, ggplot
, you want to have aesthetics, and you want to use the geom bar plot for geometric objects.
00:56
And now you can already build a plot that takes care of the coordinate system for you. So, passing in the dataset, defining the aesthetics. And here, you’re just going to need to define the x
because a bar plot is just going to aggregate all the cars that belong to a certain class and then display a count, so the height of the bar is going to depend on the count. That’s the default that it works with in a bar plot. So, geom_bar()
, and then you can call this, and you’ve got a nice graph that already makes sense.
01:28 You have the class mapped here on the x-axis and the count on the y-axis, and you see how many cars are in each of those classes. But now, for some reason, you might not want to display this with the class on the x-axis, but it would maybe fit better to your report if you had class mapped here and count on the x-axis and the bars would go from left to right. So, this is related to the coordinate system.
01:52 Again, you can see here, it’s been taken care of for you automatically and plotnine decided that a good default for this is to use the Cartesian coordinate system and mapped your values down here on the x-axis, but you can change that by adapting the defaults.
02:10
And you can do this, for example, to do this flip, you can import, from plotnine
, you can import something called coord_flip
(coordinate flip),
02:20 and then essentially build the same graph that you did here.
02:26 But now you’re going to add another layer for the coordinate system. So, for the coordinate system, you’re going to say “Flip it!”
02:36 And now in your result you see that it gives you, again, the same plot, essentially, but flipped around that the class is now mapped on the x-axis and the count is on the y-axis.
02:47 And you might wonder why it’s not descending by amounts or counts. You can see that it’s actually ordered alphabetically, so it starts with 2, then c, m, m, p, s, and s, which is the same over here. But since this is flipped, now you have the suv up top.
03:04 Now, you might want to display this, though, in a descending or ascending order. And this is actually not possible to do right away in plotnine with any sort of function that you would pass in there, but if you go to their documentation, you will also see that this is one of the examples where they suggest you to fall back to the underlying libraries that it uses.
03:23 So, you can do this with pandas.
03:26 And this is why it’s good to know that all of this builds up on pandas and Matplotlib, so that you know that if there’s something you can’t do in plotnine directly, you can always fall back on the underlying libraries, do the data aggregation, transformation—whatever you need to do, sorting, in this case—and then plot it again with plotnine. And how to do this, you’re going to see in the next lesson.
Become a Member to join the conversation.