Python Data Visualization Transcripts
Chapter: Matplotlib
Lecture: Customizing multiple plots

Login or purchase this course to watch this video and the rest of the course contents.
0:00 For the final exercise, we're going to pull together all of the concept we've talked
0:04 about and build a really complex customized visualization in matplot lib. So the first thing I want to do to plot is get what the average fuel
0:15 cost is for the years between 2010 and 2020. So let's build simple data frame.
0:25 So what I've done here is created a new data frame called DF 2010 that only has the years 2010 and higher.
0:33 And then just calculate the average fuel cost using the mean function and rounding it to
0:38 zero decimal places. Now I'm going to build a complex visualization. I'm going to copy and paste the code in here and then I'll walk through each
0:49 line what it does and we'll start at the end with the visualization that we have So I now have two plots side by side in one image that shows the
1:00 fuel cost versus the year. I have a trend line. I have an average line that's annotated and then I also show a histogram with
1:09 the average value annotated. So let's go through the code that we had to pull this together. So we already talked about how I calculated the 1970 cost.
1:20 I decided for this example I would use the gg plot style, I set up my figure and my axes to do one row and two columns and
1:30 I set the figure size a little bit bigger so that it was easier to see Then I plotted my scatter plot of year versus fuel cost on axes one and
1:40 then I also plotted my fitted value and change the colored forest green and added a line style of the two dashes.
1:50 I changed the labels for the year and the fuel cost to clean that up. I also set the wide limit in the X limit Using Axl. Set.
2:00 I said a formatter so that the value was indicated with a dollar sign and had a comma. I also added a horizontal line that was orange with the average
2:12 fuel cost and then I annotated it so that you could see that number 1970. The X. Y position tells it where to put it.
2:21 So I told it to put that line at start or the annotation starting at 2017 That is everything we put in place for axes one.
2:30 This first image I plotted, histogram on axis 2, I changed the color to sky blue and the edge color to white.
2:38 That's what you see is set the format er again so that the dollars would show up nicely on the X axis.
2:47 I added the vertical line and annotated it with 1970 as the average price and then I set the position for the Y position at 3500.
3:00 I also added a title says EPA estimated fuel costs. Set the weight to bold in the size to 14.
3:08 I also then save this final figure using "bbox inches" equals tight so that it
3:13 is nicely formatted and this is a great example to show how powerful matplot lib
3:19 is to combine multiple visualizations together and generate a really nice image that you can include
3:26 in presentations or emails or other activities that you need to do to explain your analysis.


Talk Python's Mastodon Michael Kennedy's Mastodon