Seaborn heatmap | How to make a heatmap in Python Seaborn and adjust the heatmap style

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome back to this intro to seaborn series today's video is going to be fire because we're talking about the seabourn heat map [Music] just to give you a little preview of what's coming up in this video i'll start out with the basics of the heat map including what a heat map is as well as how you can build one using seaborne i'll also introduce you to one of the most popular types of heat maps for displaying the correlation among your variables next we'll move on to color palette choices which are super important when building an effective heat map as well as how to annotate your heat maps directly then we'll move on to some additional styling including how to change your x and y tick labels so with that said let's dive in [Applause] to motivate the use of a heat map let's say that you own a cafe and you track the average number of each beverage you sell on each workday with tabular data like these it can be really difficult to notice that coffee is your best seller on wednesdays or that soda is your worst seller in a heap map you're just going to map each value to a particular color so that you can find these insights much quicker and so that you can communicate your results to others so let's go ahead and look at that seaborn code i'm going to start by importing the seabourn library as well as pi plot and numpy and i'll load in some data from seaborn and these data are about cars so each row gives the statistics about a specific car before building our heat map let's go ahead and group up some of that data so i'm going to group by origin or region where each car was made and then also take a look at the number of cylinders of each car so i'm just doing a value count here so i know that there were 63 different cars with four cylinders from europe etc and you'll see that these data right now have this multi-level index so what we can do to actually tidy this up is go ahead and unstack our data once we do that we get back into tidy format so we know how many cars were produced in each region with each number of cylinders you'll see that this process has created a few different null values here and in this case we can be quite sure that these null values were just created because we didn't have any cars from this region with that specific cylinder type so let's go ahead and fill those missing values with zeros it turns out that seaborne's heat map will accept missing values but we would end up with blank squares so to avoid that we're just filling with zeros here all right so let's go ahead and save that data and we're ready to build our very first heat map in order to build a heat map within seaborne you just need to reference the seaborne library and call up the heat map and now you can pass in this data frame directly and there we go so you'll see that however your data was structured you'll see the different rows here as well as columns here and we've mapped each of those values to a specific color so zero gets mapped to this darkest black color and our largest value which was just over 100 gets mapped to this lighter shade just wanted to also point out that was a pandas data frame but we could also use numpy arrays so here is a little simple numpy array and so we could pass this to the seaborne heat map as well so just a couple more notes on common practices with the seaborne heat map the first one is you can actually transpose your heat map pretty easily so just in case you aren't familiar if you'd like to transpose any kind of pandas data frame or a numpy array all you need to do is reference this dot t property so that will completely invert your matrix so that now cylinders represent the rows and origins and the columns so you can plot this as well using seaborne's heat map we're just passing in that origin cylinder matrix but we've transposed everything another really common matrix to visualize with heat map is the correlation matrix so if you have a pandas data frame like this one you can access these correlations just by using this dot core method what we're doing here is taking all of our numeric features from the original data frame and checking the correlations between those features so moles per gallon is perfectly correlated with miles per gallon and negatively correlated with cylinders by the way if you are familiar with correlations this is the pearson correlation but you can actually switch this if you'd prefer so this is a really common matrix to visualize with the heat map because we have so many different values so let's go ahead and put that into our heat map there we go so now you'll see values that are positively correlated are getting mapped to this lighter shade and values that are negatively correlated are getting mapped to this darker shade so one thing we're definitely going to talk about more in this video is color palette choices so right now we have larger values getting mapped to this lighter color but if you do want to draw attention to that positive correlation you might choose a different color palette for these data [Applause] so choosing an appropriate color palette is actually super important for building an effective heat map it turns out that if i'm presented with both a lighter and a darker color the human eye is more likely to be drawn toward that darker color first in seabourn the default color palette actually maps larger values to lighter colors and smaller values to darker colors if instead i'd like to highlight those larger values i actually need a palette that's just the reverse so let's take a look at the seaborne code on how you can change that color palette and what color palette options you have seaborne offers 170 different color palettes so you have many options when choosing the aesthetics of your heat map the way to change the color palette of your heat map is to reference an argument called cmap or color map now you'll just pass in the string that represents the palette you'd like to choose so here i'm using a sequential palette that just means that i have one color that varies from its lightest shade to its darkest shade for other cases it might be more appropriate to choose a diverging color palette so here we're going to choose a palette that starts with red at one end and goes to blue on the other end this choice might be more appropriate if you want to highlight both small values and large values such as in this correlation map you'll notice in this correlation heat map that my center point is not exactly at zero if i'd like to change that and center my color map to be exactly a certain value i can just reference the center argument now we have the center set to exactly white and i'm judging the positive and negative correlations on equal footing another way to judge positive and negative correlations here is to actually specify what values will start and end my color map so let's actually set our minimum value to be negative one and our positive value to be positive one we'll see now that negative one which is the lower bound for correlations is mapping to this darkest red color and positive one is mapping to this darkest blue color which is the maximum value our correlations could be [Applause] so remember that the heat map is just a visual way to display a table of numbers one really effective way to do this is to not only display those colors but to also annotate those numbers directly on your heat map let's take a look at the seaborn code that will allow you to do this adding annotations to your seaboard heat map is actually pretty simple all you need to do is access this argument called anot and switch that to true that signals a seaborn that you actually want it to annotate the values of every single square in your heat map one thing that happens though that i often find is a little bit undesirable the default behavior is actually specified in numbers of significant digits so you'll end up with this scientific notation for some values so you can actually switch the format of these annotations as well so that's actually just with a property called format or fmt and here we're just going to specify a string that lets seaborn know exactly how many decimal places we would like so i would actually not like any decimal places here i'm going to have a floating point number with no decimal places that's what this means so i end up with integers here so i have 103 instead of that scientific notation of course if i did want decimals i could just switch this to let's say point one which would let c board know that i do want one decimal place you can also style the annotations through this argument called another keywords so announce keywords and this accepts a dictionary so you can actually build up many many different types of properties if you would like so let's say you want to change the font size and we'll increase that to 16. now we see much larger annotations on that heat map or perhaps we would like to change this to be bold that's font weight or we can even change the font family these are really just any properties that you know about for styling text within matplotlib you can use here you can of course change the color of these annotations as well with just referencing color but i actually often choose not to do this because you can see what happens if i set the color to be all black i'm kind of losing out of some of these values particularly in these really large values at my darkest part of my color map so i often choose not to do that because seaborne is actually trying to figure out for you if a value should be written as black or white [Applause] so the heat map is a fairly straightforward visual that can display a lot of information but you really have to get the styling right in order for your heat map to be effective let's take a look at some final advanced styling tips that will have you coding up your own heat maps like a pro one way you can style your heat map is to put a little line between each of the rectangles so you can do this by accessing this property called line width and you'll just set this to a numerical value for however wide you'd like those lines to be now you'll see we have a little white line that separates every single rectangle the default color here of course is white but if you'd like to change that you can with this property called line color and that just switches the color in between now we've got this nice kind of retro feel for our heat map another common styling tip is to change the tick labels in your heat map so right now we're using the correlation matrix from pandas so this is actually a pandas data frame and these tick labels are pulled in from the data frame itself but you can change these if you would like to create your own list of labels you can just pass that to x tick labels labels we actually have the same values on the y-axis so let's change that to y-tick labels as well now you'll see we have these nice capitalized values for our tick labels and those are coming from this list so these properties x tick labels and y tick labels will actually accept a couple of different things you can either pass your own list of values or you can potentially turn these labels off by passing in a boolean value here i've passed in false which signifies that i do not want x tick labels my last styling tip here is about the size of those rectangles so right now seymour is actually styling those rectangles based on the overall figure size of your heat map but you can specify if you would like each of those rectangles to actually be perfect squares you can turn on this property called square equals true and now you'll see no matter how many values you have for the y and x you will end up with perfect squares throughout your heat map so i hope you've enjoyed learning all about the seaborne heat map if you have any additional questions about the heat map or about seaborne in general feel free to leave me a comment below or check out another video in this intro to seaborne series thanks so much and i will see you in the next one
Info
Channel: Kimberly Fessel
Views: 14,389
Rating: 4.9532709 out of 5
Keywords: seaborn heatmap, heatmap seaborn, seaborn heatmap correlation, seaborn heatmap tutorial, seaborn heatmap explained, seaborn heatmap tick labels, seaborn heatmap pandas, seaborn heatmap color, seaborn heatmap annotations, seaborn heatmap style, how to make a heatmap in seaborn, how to make a heatmap with python, seaborn python heatmap tutorial, sns heatmap python, sns heatmap tutorial, sns heatmap correlation, seaborn heatmap interpretation, heatmap style, python heatmap
Id: 0U9cs2V-Mqc
Channel Id: undefined
Length: 13min 3sec (783 seconds)
Published: Fri Sep 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.