Python Data Visualization Transcripts
Chapter: Seaborn
Lecture: Heatmap

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In this exercise will go through an example of some of the specialized plots within Seaborn
0:06 So I've created a new notebook called it exercise three using the same data set
0:11 and the first visualization we're going to work on as a heat map before we do this, we actually need to transform our data.
0:18 We're going to use the cross tab function in pandas to do that. So what the pandas crossed have does you've got the fuel type summary across the top
0:33 here at the class summary Down here. So you've got the combined fuel economy for diesel cars is 32.89.
0:42 Get this because we pass combination 8 to values and tell it to do a mean.
0:47 So behind the scenes Pandas is using that cross tab to combine the data and do averages. What you want to do with the heat map is make sure it's in
0:55 this format and once it's in a grid like this, then you just pass that the heat map and now we've got this nice grid that
1:04 has a color coding that corresponds to the values from this table. And as your table gets bigger and bigger.
1:11 This is a way to identify outfliers and trends in your data. This one may be a little bit hard to read.
1:18 So let me go through a way you can customize it and what we've done here is past the same data frame but told it to annotate the value.
1:29 So now you can see the actual mean fuel economy summary as well as change the format so that the decimal points are not there.
1:39 And then I also changed the color map, to yellow, green, blue, which I think highlights some of the variability a little bit more now.
1:49 I want to show how to customize this as well. Now what we have in the code here is we're going back to matplot lib to create our figures and axes.
1:59 So we're going to create a similar heat map this time we're going to use the ice fire color map and we're going to plot it on our matplot lib axis
2:08 We're gonna add a Y label and X label and then I'm actually gonna save it as an Svg using the, matplot lib commands that we've already talked through.
2:18 So it looks like I forgot to do one of my imports. I'll add that back in there. Now I have a different plot.
2:28 I like this new ice fire and it's a little bit bigger because I changed the figure size, I have the annotation and now I have the different X and Y
2:38 labels. You may be wondering a little bit about how I use the various color maps. So I want to introduce the pal plot function,
2:50 which is a nice way to visualize the palettes. So this one is actually creating a diverging palette. You can look in the documentation,
2:58 understand what these parameters are, but this is the hue and the saturation and the
3:03 number of colors. So basically you have to play around with this to find a
3:07 plot color that you like. So I'll show another example where maybe we'll call this one purple. So instead of a diverging palette,
3:18 let's do a light palette. And now you have a purple palette. All this is an area where you can spend a lot of time exploring and playing
3:27 around with the different colors and finding something that works well for your own visualization.
3:33 So let me show an example of how to actually take a custom palette and apply it to your heat map. So we'll stick with the same cross tab that we
3:44 already built, but maybe use a light palette to show it in green colors since we're talking about fuel economy. So I've used my figure and axes.
3:59 Again, I've created a custom color map using the light palette with green, I pass as C map equals false because it's not needed for the heat map.
4:10 I passed the C map to my heatmap function and then I set my label. So now I have a custom green palette and it does a nice job of highlighting
4:21 those outliers where those electric fuel vehicles have a much higher fuel efficiency than, say gas, diesel and other.


Talk Python's Mastodon Michael Kennedy's Mastodon