Python Data Visualization Transcripts
Lecture: Basic API examples
0:00 For this exercise on pandas data visualization. I've created a notebook to import all the modules I need read in the source file
0:09 and create my data frame. So restart and run all. Now we'll create our first plot which will be a histogram and as I mentioned there's two API.
0:24 So let me show you the other example. And as you can see both plots look very similar or actually look exactly the same
0:34 And the difference is really what is preferential for you. There are some benefits to using the '.plot.hist' and that you can use
0:44 some of the command completion. But sometimes this is maybe a little bit easier to
0:50 understand. It's really up to you on how you would prefer to plot with these two different API's. So let's do a different example.
0:59 And instead of doing a histogram we'll do a box plot. And there are some other examples we can do.
1:10 Let's let's play around with the different kind options. one example is a density plot which looks very similar to a histogram,
1:21 but it's actually not exactly the same. I'm not going to go into the difference but just wanted to highlight an example. Another example is a 'kde',
1:34 which once again is a very similar kind of plot and depending on the data, it may or may not come out differently.
1:41 So for the next set of plots we need to plot the average fuel efficiency by year and I'm going to create a new data frame to do that.
1:55 So now we have a new data frame with each year and then the highway, the city and the combined MPG.
2:03 Let's do some, some plotting on that to show some more examples of our API. This example we're going to plot a box plot on that average by
2:15 year data frame and give it its title. This is going to take each column and plot a box plot.
2:23 We could do a similar effect by doing a line plot if we'd like. And we can also do some additional customization of this to make it look a little
2:36 bit better. So you'll notice the years have some decimals on it and maybe we want to change the range to only go from 2000 to 2022 and clean up this
2:49 X axis. We also can maybe do some other customization is to make it look a little bit better. So let's look at what we've done now.
3:01 So we have created another line plot but you'll start to see some references to
3:07 matplot lib. I specialized a fig size As an argument to the plot method. I also specified which x ticks to use and that it's arranged from 2000 to 2022
3:20 I also set the wide limit and rotated the labels by 45° and you can see all that is very similar to matplot lib because matplot lib is a
3:33 basis for the plotting function in pandas. The next type of plot I'm going to walk through is a bar plot using the
3:42 same API we pass kind equals bar and now we have a bar plot for each year for the three different MPG,
3:51 but if we wanted to customize this a little bit to make it look nicer, do a few things, maybe we want to rotate. Now we have a bigger plot,
4:04 that's a little bit easier to read. one of the other things we can do, sometimes the plots are easier to read if they are horizontal,
4:16 so we can pass the bar H for horizontal bar plot and that's a lot easier to read with the years and then the final type of plot I want to talk
4:26 about in this section is the area plot. Once again, it's really easy to change your plot types just by changing that kind
4:35 parameter and now we have an area plot and I'm going to show an example of
4:43 how to even provide more customization based on some of the matplot lib functions, so we create our figure in axes and pass that to the plot.
4:55 So now we have that information that axes that we have customized in the past and can do some more customization there.
5:03 Let me show a specific example of that. So now we've done a couple of things, we are doing another area plot,
5:12 we've decided not to stack it so that the values show their relative value a little
5:19 bit better. We also change the formatter so that it's a little bit cleaner and we set our labels and title two MPG or MPG.
5:29 And set a tile title for average by year, all based on the matplot lib examples that we have walked through in the past.