‫Welcome back.
‫So in the last video, we saw two different ways of plotting from a pandas data frame, one with the
‫pie 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 our 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 the O method.
‫So what we're going to do is recreate this, but using the object orientated method from scratch, and
‫then 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.
‫We'll put in here our own method from scratch.
‫And up here, we might go.
‫Oh, method mixed with pie.
‫Plot method.
‫So then we'll go plot subplots and we know our fig size.
‫This is a pretty good size.
‫So we're going to we're going to reuse that.
‫1006 That was correct.
‫Yep.
‫And then we want to do plot the data because that's part of our map plot lib workflow.
‫We'll call our figure, we'll rename it to scatter.
‫We'll go scatter equals X 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.
‫She'll call cholesterol.
‫And then we want to color it with over 50, which is the see here.
‫We want to cover it with the Target column.
‫And 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 set.
‫We want to add in a title, beautiful.
‫So where you got heart disease and cholesterol levels, etc. Keep it nice and concise then we want x
‫label equals age and we'll go 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 as just cholesterol nice and simple for the time being.
‫And let's see what this looks like.
‫Are 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 or what color is that to you?
‫That's a purplish to me.
‫So maybe we.
‫Oops.
‫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 Title equals target.
‫Let's add a legend to our axes, 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 up 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.
‫Hmm.
‫Oh, there we go.
‫Typo.
‫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 going to find the 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 say over 50 target values.
‫He's going to look at this Target column and then find that there's 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 customisable 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 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 plot labeled Axis AX Line OC.
‫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 go maybe back 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 dart ax line.
‫Let's see what happens.
‫X Because this is our axes dot x h 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 y.
‫So we need a y axis.
‫So we want to give it the same y here.
‫So we want the cholesterol column.
‫Yeah.
‫Beautiful.
‫And now we want the line to be the mean of that column.
‫Beautiful.
‫Now we've got a straight line, but maybe we want a dash line because that looks a bit better.
‫We 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 docstring.
‫Maybe if we scroll right down, there we go line style.
‫So you could also just look up how to style map plot layer line.
‫Law and 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.
‫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 created this plot.
‫Now, this is just another example, right, of using a different data set of creating your own custom
‫plot and then working with different kinds of data.
‫So we've got cholesterol and then really starting to use the map plot lib 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 fell ash, 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 in the next video.