Welcome back.
So in the last video we saw two different ways of plotting from a panda's data frame.
One with the PI plot method.
And we created this plot here and then the other with the object orientated method.
I'll be honest with you this is not directly the O method we've kind of passed it off here but we're
still kind of using the PI plot method here.
But we've tricked it a little in combining it with our method.
So what we're going to do is recreate this but using the object orientated method from scratch and we're
going to customize it to make it look a bit more prettier.
Let's start we'll create a new figure and a new axes will put in here.
Our method from scratch and up here we might go our method mixed with pi.
Plot method so then we'll go plot dots subplots and we know our fig size.
This is a pretty good size so we're going to we're going to reuse that 10 6.
That was correct.
Yep.
And then we want to do plot the data because that's part of our map plot workflow.
We'll call our figure we'll rename it to scatter we'll go scatter equals x dots scatter.
Because we want to scatter plot and we're going to go X which is our age column.
So we want over 50 because remember we're working with our over 50 data frame now age and then our y
column is going to be over 50 and we're going to have Shiloh call for cholesterol and then we want to
color it with over 50 which is the C..
Here we want to color it with the target column.
What I mean by color it is just literally change the color of a dot depending on what the target column
value is and now let's see what this looks like.
Okay groovy we're starting to get a few different colors here.
It's looking similar but it's still missing a few things like We don't know what the x axis is and we
don't know what the y axis is.
But that's okay.
We've got a very flexible method here because we're using the object orientated method from scratch.
So now what we're going to do is customize and we're going to go ax dot set.
We want to add in a title beautiful.
So we got heart disease and cholesterol levels.
That's right.
Keep it nice and concise.
Now we want X label equals age and we'll go we'll add a y label which is cholesterol now you could put
the units here.
I'm not actually sure what the units are of cholesterol that would be something we'd have to look up.
So we'll just keep that is just cholesterol nice and simple for the time being.
Let's see what this looks like.
Starting to get better.
All right.
So we've got a title now we know what the y axis is and we know what the x axis is but we're not sure
what these dots are.
Right.
These are just purple and yellow dots so what color is that to you.
It's a purplish to me so maybe we.
See that's what happens when you run it without the semicolon.
You get this code output so maybe we add a legend.
So how can we do that we got ax we call that the legend method legend and then we're going to use this
little star which is kind of like just unpack all the values
of Tide or vehicles target let's add a legend to our axis which is this here and this little star is
saying Hey grab the values from scatter and unpack the legend elements which is going to query this
C parameter here and look back at this data frame and go what values are in this in the target column.
So we've got ones and zeros and then it's going to create the legend built off those elements.
So let's see it in action path collection has no attribute legend elements.
Mm hmm.
Oh there we go.
Typedef beautiful.
So if you type it right.
That's what it's going to do.
It just goes into the scatter variable which is what we've created up here and then it's gonna find
a legend elements which it gets from this C parameter which it then looks back up into our we'll put
it here so we can see over 50 target values it's going to look at this target column and then find that
this ones and zeros and then it's going to color the legend the same as the values on here.
So now this is looking a bit better.
The object orientated method from scratch is looking far better than what we've built here.
This is how we've done a few more lines of code here but now you're starting to see how customizable
this object orientated API is.
What if we wanted to add a few more things.
Well the beautiful thing is we can do that.
What if we wanted something like a a horizontal line across here to indicate the average cholesterol.
Let's look at the documentation how to add a horizontal line and map plot lib plot map what lived on
axis that X Hage line.
Okay what will this do add a horizontal line across the axis.
Well I guess that's what we want to do.
So let's try it out.
We want to get a medevac up in here add a horizontal line.
So this is the kind of workflow you'll go through like if you wanted to do something that's perfectly
fine you look it up you go to the documentation and now we're going to try this out axes dot ax Hage
line.
Let's see what happens X because this is our axes dot ax Hage line.
Wonderful.
And now we want to give it the value.
So we want over 50.
So if we go shift tab and see what it takes why.
So we need a Y axis.
So we want to give it the same y here.
So what the cholesterol column yeah beautiful.
And now we want the line to be the main of that column beautiful.
Now we've got a straight line but maybe we wanted a dash line because that looks a bit better.
We've got line style.
So this is this is another one of those keyword arguments so this one here keyword args.
That's not really going to appear in the docs string.
Maybe if we scrolled right down there would go line style so you could also just look up how to style
map plot line line style reference.
You come across something like this but in our case we save a little time.
I've used this dash line before and that that usually looks pretty good.
So there we go.
We've added a horizontal line now that's starting to look much much better than what we had before.
Don't you agree.
Well I certainly do but I'm kind of biased because I I created this plot.
Now this is just another example right of using a different dataset of creating your own custom plot
and then working with different kinds of data.
So you've got cholesterol and then really starting to use the map plot layer API to create whatever
kind of plot you want now you could imagine you could create a subplot of this.
We might actually do that in the next video is create this but we'll work it in with a subplot so we
might add another variable from the data frame and put it into a subplot of this.
So let's see that in action.
But in the meantime have a little bit of a play around maybe you can take a step ahead and see if you
can create a subplot of heart disease and cholesterol levels with maybe heart disease and and what's
another column that you could use.
And this one fail act which is max heart rate.
See what you can do creating a subplot with cholesterol and max heart rate.
If you want me to go along with you we'll do that next video.