Hello everyone and welcome to the categorical plot's lecture for the seaborne S. discourse.
Now we're going to shift our focus to plugging categorical data.
Let's go ahead and jump to Jupiter notebook to get started.
OK here I am at the Jupiter notebook and I've already put in a couple of lines of code here.
I said import Seaborn and Cessna's I went ahead and said map plot in line so I can see the visualizations
inside of the Jupiter notebook.
And then I went ahead and loaded the tips data set and then I checked out the head of the tips data
set OK for categorical plots were mainly going to be concerned about seeing the distributions of a categorical
column such as this sex gender or smoker daytime in reference either to one of the numerical columns
or another categorical column.
Let's go ahead and start off with the most basic categorical plot which is the bar plots and you can
do that by calling Asness bar plots and a bar plot is just a general plot that allows you to aggregate
the categorical data based off of some function and by default that's the mean you can almost think
of this as a visualization of a group by action.
Let me go ahead and show you what I mean by that.
We're going to go in and set an x y and data arguments into bar plot all sort of data sets the easiest
tips and then what I'm going to do and do is set the x axis to a categorical column.
In this case I'll go ahead and choose the sex column here.
And then for the Y column or yeah for the y axes we can choose a column that's numeric for instance
total bill.
And this will create a bar plots and by default what it's showing you here is the average or mean of
the total bill.
Her categorical column value.
So the average total bill for males is going to be slightly higher than the average total bill for females
.
And you can go ahead and change what's known as the estimator object if you want to basically affect
what the actual grouping or aggregation here is.
And we can go ahead and do that by adding another argument here estimator.
And here we're going to do shift tabs.
You can actually see the Jupiter notebooks documentation for this.
The estimator is a function that is an aggregate function.
So it's some sort of statistical function to estimate within each categorical bin and by default that's
going to be average or mean.
But I can also put in my own functions if you have some sort of aggregate function you can go ahead
and put that out.
Right now we're looking at the mean total bill.
But let's say we wanted to figure out the standard deviation her gender for instance.
Is there more standard deviation on males and their total bill.
So more variance or do females have more variance.
Well we're going to get into say important some pie as we go to copy and paste this and put it in a
cell above this.
Go ahead and insert the cell above and was say important up-I as NPE here.
And then in the estimator I'm going to go ahead and passen the pie t d function for standard deviation
and this will tell me when I run this what the standard deviation of the total bill column is per category
.
And you can pass in whatever estimator function you want.
You can even create your own.
And you can pass it into bar plot where x is a category column and Y is some sort of numerical column
.
Then you passed your data and whatever estimator.
And this is just an aggregate or essentially just a visualization of some sort of group by mechanism
here.
All right let's go ahead and build off of this by doing a count plot a count plots in seaborne is essentially
the same as a bar plot except the estimator is explicitly counting the number of occurrences.
And because of that we only set the x value and then we sit in the data equals tips.
And this is essentially the same thing as saying like Pandas Thumb counts off of this tips column for
gender.
All right.
Now that's the basics of the counterplot is essentially the same as a bar plot except the y axis has
already been chosen for you.
It's going to just count the occurrences.
Here we can see there's more males in the dataset than females.
Let's move on and talk about box plots and violent plots.
These type of plots are used to show that distribution of categorical data a box plot is also sometimes
known as a box and whisker plot.
And it shows the distribution of quantitative data in a way that hopefully facilitates comparisons between
the variables.
Me going to show you what I mean by that.
I'm going to create a box plot by say S and S that box plot then we're going to pass an x and y in this
case I will choose X to be a categorical data points or column.
And then why is going to be numerical.
It's going to go total bill.
So here we're going to try to see the distribution of total bill per day.
And then lastly I have to pass that data as tips.
Let's go ahead and run this.
And here we get our box plot out.
OK.
This box plot shows the core tiles of the data set while the whiskers extend to show the rest of the
distribution except for these points that are outside of these whiskers.
These are determined to be outliers and you can essentially just interpret this as from for instance
if we look at Thursday as a day by itself we have on the y axis total bill.
And then on the X-axis it's just separated by category or day.
So if I look at this blue one right here all the way on the left for Thursday it's essentially telling
me that about 25 percent or a quarter.
Which is why these are called core tiles are the data points go from the center of the whisper to the
bottom here and then another quarter go from the bottom of the box to the middle here.
This line and then etc for the top.
So but now about another quarter the points are here and then the other quarter of the points are here
.
And these dots are outliers and we can begin to get really fancy with seaborne by just adding a few
more words of code here and specify a hue.
And that's going to allow us to kind of split up these box plots even further by another categorical
categorical column.
In this case we'll go ahead and say Heugh equals smoker.
If I run this now I see the box plots split per day and also split by smoker status and smoker status
is colored here for you.
And here you can now compare not only.
Day by day behavior for total bill but also by whether or not this person is a smoker.
And that's really the true power behind seaborne.
It quickly allows you to add this hue indicator which essentially gives you a whole another layer of
data very quickly and very smoothly.
And so you can compare that basically people on Fridays tend to have a larger bill if they're not a
smoker versus if they are a smoker and then you can begin to actually see the variation for smokers
or that distribution for smokers and non-smokers.
All right let's go ahead and talk about violent plots now going to go ahead and remove this hue here
so we can see our original box plot and then I'm going to call a violent plot and a violent plot plays
a very similar role to a box plot.
And you can do it just because in essence that violent plot.
And what's going to do is it's also going to show the distribution of the data across some sort of category
.
So it's going to take the exact same arguments as a box plot is going to take an x for the category
.
Your wife is going to be some sort of numerical column is going to stick with total bill and then it
will pass in our data.
CBS is going to go ahead and run this and see what these plots look like and let me zoom out so we can
actually compare these two.
All right.
So we have our box on top and our violin on the bottom.
Now the violin plot unlike the box plot allows us to actually plot all the components that correspond
to actual data points and it's essentially showing the kernel density estimation of the underlying distribution
.
And this is essentially just showing if we kind of split this in half the distribution of the points
on its side and this allows you to reveal even more information about the data points themselves the
cost comes at the fact that it's a little harder to interpret or read versus a box plop a box plot is
very easy to just take a look at and you can quickly tell where roughly the data compares on a category
in violent plots.
We'll just give you a lot more information as far as the distribution of all the points themselves.
But it kind of comes at the cost they have to spend a little more time looking at it and understanding
it.
But these two plots are definitely very related to each other.
It's just that the violent plot shows a little more information.
Sometimes this information can be hidden from you if you're just using a box plot and a violent plot
just like the box plot.
Also has the option to do Whew.
So I could say you buy sex and Ill show me violent plots next to each other.
Well what's interesting about the violent plot versus the box plot is as another argument instead of
putting two violent plots next to each other since the violin plot information essentially doubled up
.
I could have one half be one here and the other half be an other and you can do that by saying split
equals true as an argument and then it will kind of split the distributions.
And here you have a lot more information.
Now you can directly compare the distributions based off in another category and see how the categories
themselves on the x axis compare to each other.
Granted this comes again at the cost that you're going to spend more time looking at this visualization
.
So you really have to know your audience and know what's appropriate when you're using seaboard and
these advanced visualizations if you're dealing with some more technical data is people who are data
scientists themselves and have seen violent plots before these sort of plots are a great choice.
However for dealing with more kind of CEO executive types management people maybe just a simple box
plot which is more popular may be the correct choice unless you find something very interesting in the
violent plot.
All right let's go ahead and talk about a few more advanced methods for visualizing this sort of categorical
data.
We'll start off with the strip plot and the stript plot is actually probably the simplest but we're
going to build off of it and we're going to zoom in here.
We're going to get and say essence strip plot and the strip plot is going to draw a scatterplot where
one variable is categorical.
So I'm going to say strip plot and we're going to do the exact same thing was that before X is going
to be categorical y is going to be numeric total bill.
And the data is going to be equal to tips.
And this is just a essentially just a scatterplot based off of the category one issue with this strip
plot is that you can't really tell how many points are stacked on top of each other which you can do
is at another argument called jitter and what jitter will do if we go in and say is true here.
Is this going to add a little bit of random noise to separate some of these stacked points.
And you can see the thicker and more points there are a lot more clear versus just the normal strip
plot.
So the normal stript plot if he had a bunch of points stacked on top of each other it was kind of hard
to tell.
But now if you can see the density a little better in with the strip plot just like a violent plot you
can begin adding some of those extra few arguments.
So again we could say hewe is equal to something like sex.
Run this and now I have color based cues and we can also do a split on this just like we could have
fake violent plot and this will basically split up these colors based off of your schewe arguments.
OK.
Now a plot like this again you got to be careful of it it may be hard to interpret versus a simple box
plot or even a violent plot.
Some people like to actually combine the idea of a strip plot versus a violent plot and create something
that's known as a swarm plot and Seaborn allows you to actually do this.
A swarm plot.
We go out and start typing.
It is very similar to strip plot except the points are adjusted so that they don't overlap.
And this actually gives a better representation of the distribution of values.
It's kind of like combining a scatterplot strip plot with a violent plot.
Let me going to show you what I mean by that.
There will probably be a little more obvious when you see it.
We're going to go ahead and pass in the exact same arguments day for x.
We'll say why is total bill.
Whoops
total underscore bill and then we'll say data is tips.
And here we can see the swarm plot.
And hopefully it's kind of more obvious how the swarm plot is essentially a combination of a strip plot
and a violent plot.
It shows the same distribution except it's actually showing all the points themselves.
One drawback to using a swarm plot even though they're pretty nifty and cool and the fact that they
show a lot of information sometimes they don't actually scale that well to very large numbers both in
terms of the ability to show all the points.
These points just go way too wide since you don't want to stack anything on top of each other.
And it also takes a lot of computation to begin to arrange them.
So I would advise not to use swarm plots for very large data sets.
And in fact I would say just in general probably stay away from just showing swarm plots since most
people probably haven't encountered them before.
Do quick know if you do really like the swarm plot you can actually combine it with a violent plot to
show both pieces of information stacked on top of each other.
You can say in this same cell will go ahead and say SNL violin plot and will copy and paste the exact
same information here into our violent plot and we'll go ahead and specify the color of the swarm plot
to be black and this is actually going to plot both right on top of each other and here you can see
every single point and the corresponding KDE or kernel density estimation plot on top of it.
Now there's a lot of information here so be careful of this.
This is probably more useful for presentation but just for exploratory data analysis Hopefully these
kind of plots help you just immediately understand your data but not so much on the side of explaining
to an executive.
Those kind of things are better done with either box plots or they sort of count plots or bar plots
these kind of plots these more advanced violent plots strip plots swarm plots are just more suited for
a data scientists themselves and had actually read in or explore your data information.
Finally I want to talk about Factor plot factor plot is actually just the most general form of all these
plots and what it does is if we do shift tab here it takes in an argument a wargame why arguments can
take human data but then it also just takes in a kind's arguments and the kind argument is essentially
what's going to describe the facts or plot itself.
For instance we can say fact or plot will say we're going to copy and paste that sequel's day why it
was total bill was to Tipps excuse me and then we can go ahead and say Kines is equal to.
Let's say one of the bar plot you can just say bar.
And it creates a bar plot based off those categories.
So all the plots we've been looking at before the most general method is called Sonesta factor plot
.
And then actually specify the kind.
So for instance if we wanted to recreate the violent plot you can call kind violin and this begins to
recreate that violent plot.
Personally I prefer to actually call the plot itself specifically so as an S that violent plot stript
plot box plot etc. versus calling the more general fact or plot method but know that this is available
for you in case you want to call it.
All right.
That's it for categorical plots.
Hopefully you saw a lot of the power that's available to you and use this power wisely especially depending
on who you're presenting to for instance like I mentioned before these box plots.
Easy to read a lot of management people understand them well these more advanced plots those are more
for you to explore that data.
OK.
Hope you enjoyed this lecture and I hope you've enjoyed seeing how easy seaborne makes these visualizations
for you.
Everything you've been doing is essentially just one line of code which is pretty awesome.
Okay thanks and I'll see you at the next lecture