Creating our First Scatter Plot

Mammoth Interactive
A free video tutorial from Mammoth Interactive
Top-Rated Instructor, 700,000+ Students
4.3 instructor rating • 279 courses • 254,421 students

Learn more from the full course

Software Libraries Explained - Python Programming for All

Learn to program and use Matplotlib and NumPy! Make 2D and 3D graphs with math. Make predictions with data with analysis

32:12:07 of on-demand video • Updated May 2018

  • Code in the web's most popular programming language: Python
  • Create basic line and scatter plots with Matplotlib 1.5
  • Read finance data directly from Yahoo
  • Customise our graphs with visuals, a title, labels, text and a legend
  • Organize data into histograms, pie charts, and box and whisker plots
  • Perform calculations, functions and statistics with NumPy
  • And so much more in this massive life-changing course
English [Auto] Everyone and welcome in this it's for real. We're going to look at Scouter clocks and we're going to see how we can create. It's got our thoughts. We're also going to learn how we can visualize plots. And finally we're going to look at how we can change the attributes of our scout a lots of for example changing the color of her data points or Also changing the markers that we use. Hey everyone. OK. So let's get started in with making our first plot with my potluck. So the first thing that we're going to need to do to be able to use Map floodlit is we're going to need to import it. So in our Python environment we're going to type an import map plot them. So this allows us to import them upload the package and specifically right now we want to use the pipe plot section of the map plot loop so we want to import map plot lead dog hide plot and since matplotlib dot plot is quite a long name we're going to abbreviate it in our code as TLT. So this is the kind of standard form of importing. And so we're importing the map Clodd lip Pipelet and or library and we're going to import it SPL too so that when we want to reference the data later on where we want when we want to reference the map plot lived up high plot we only have to type in GLT rather than the whole map plot lived up hopla. OK so let's get started. The first thing that we're going to need for plotting is we're actually going to need some data to plot. So what we're going to do is we're just going to create some linear kind of data. So we're just going to create numbers between negative 100 and a hundred and we're just going to store these two lists for x and y data and then we'll just plot them against each other. So we're going to create a variable x which is going to be an empty list and we're going to create a variable Y which is also going to be an empty list and these are going to hold our x and y data respectively. So to just kind of create our data we're going to use a for loop. So we're going to go for pi which is going to be your temporary variable. We're to go in range and we're going to start at negative 100 and we're going to go up to 101 because our upper limit if we remember is up to but not including. So if we stop our range at one hundred and one that means the final number that we're actually going to get is going to be 100. And if we're not adding any other parameters here that just means we'll go in steps of 1. Now what we're going to do inside this for loop is we're going to go into X and we're just scared to go in here and say tend to X we're just going to append. And we're going to do the same thing for Y's son why we're going to go why not a pen and just like this. Now what this is going to give us this is going to give us lists that are going to look like this. So if I just put a comment here or X is eventually going to look like negative 100 negative 99 negative ninety eight and so on and then we go all the way and we got 99 and then 100. So that's how we're going to look like. And our wireless is going to be very similar because we're just adding We're just pretty much doing the same thing. But here to X and here to Y. So we're also going to see negative 100 then negative 9:9 negative 98 and so on until we get up to 99 and 100. So we're going to have all the numbers between negative 100 to 100 insteps of one. And this is where x and y lists are going to look like they're pretty much identical. They're just stored in two different variables and this is just kind of for reference for us to that you know how we should plot it. OK so let's go ahead and make our first pot. Now our first plot is going to be a scout plot. So how would we go about that. Well what's really great with matplotlib is that a lot of the kind of initiating figures years and all that stuff is taken care of behind the scenes. So unless you want to make really intricate graphs which we can look at later to we don't actually need to start declaring figures and all that stuff. We just need to say what we want to play. All right. So let's go ahead and get started with that. So what we don't want to do is we want to go to map clock lived up high plot and we can access it by just typing in TLT and in the plot we want to access the scatter method. And so we're just going to open and close parentheses and then we're going to play around with a little bit. Now the first things that we're going to need to put in is we're going to need to put in our extra variable that we want to plot and our Y variable. So we're going to plot x versus Y like this. So this is how we make a basic scatterplot of X versus Y. Now to indicate to map plot lib that we're done creating this plot and we want to see it. We actually have to type in afterwards. TLT Daube show like this. And so what this does is that everything that we've created for this plot we're now going to see now you can kind of think of it so we don't actually we're not that concerned Gach of having to take care of the figure kind of in the backhanders stuff and manage that. But once we type peel the dots show everything that we've kind of done above now gets put together into the graph and we can see the graph on our screen. So if we run this we're going to see and we have this scatterplot created here. Now we can't really see that. Well if these are just individual points or if this is just kind of a line connecting it it's kind of hard to make out like we also aren't really sure about the exercises yet and stuff but that's all stuff that we'll look at later. So let's just look at some of the properties and maybe let's also reduce our stat or rather increase or substance upsized that we actually reduce the amount of data that we have here. And let's go on steps of. So now we're going to have negative 100 then we should have negative 95 and negative 90 and here we'll go from 95 to 100. So we're always going into steps of 5. And similarly it will have us for the y data so we can actually print out maybe our X data before so X and Y date is going to look like we can just print them both out. And this is just to kind of reduce the amount of points that we'll see here. So if we run this again now we see so we've got our X data which is here we've got our y data which is here. So they're identical. And that's just because we're doing the same thing with different variable names here. Now we also see that we're getting plaudits now we don't have them much done stayed on. So we can actually see the individual points. Now we can also go ahead and edit how this how this scatterplot is kind of portrayed to us. So one thing that we can do for example is change the size of these circles. Now the way that we can do that is by changing the characteristic assets and we can set this to a new value. Now the default value that we see here is 20. So what we can do for example to make them smaller is set the size equal to 10. And if we run this we see compared to about here that our circles have actually become a lot smaller and we can also make it 15 for example to have something in between these where we can make it 5 to have it even smaller. It really depends on us. So if we have a lot of data like we had in the very top Schir then maybe it be nice to have smaller sizes so that we can actually distinguish the data here wasn't that big of a deal because our our data was already distinguishable so our original thought we can still distinguish the individual points. So we don't really need to reduce the size but we can if we want to. So maybe let's just put this like a 15 just so that we can see it a little bit better. And let's run this again. So this is what we're looking at now. We can also do is we can change the color. So for example if we want the color to be red we can say see which is going to stand for color so that I would change the color. And here we can put in red for example. So if we run this now what we see is that our circles are now red rather than blue which we had up here and also change it to green for example. We can run it again. We have green. So you can really just change it however you like. So this is how we can set the color property. So what else can we do. Well rather than having circles here we can also put in kind of different symbols. And the way that we can do that is we can change the marker property. So I'm just going to put this on a new line just so that we can kind of see both things at the same time. So to change the marker we can make it equal to. And then we want to put in here what we want our market to be. Now there are many different things that we can set our marker up I'll just show you a couple examples if you have anything specific in mind. You can also you know look it up on the map plot Law Web site. They'll give you all the information of all the markers they have. They have you know dozens of them. I'll just show you a couple. So one thing that we can do for example is rather than having circles we can have squares and we get that by saying marker is equal to S.. So if we do that we see now we got squares instead of the circles that we had up here. Something else that we can do is use an asterix symbol and if we do this this is going to give us stars is going to give us these asterisks like symbols. We can also make Plus's by having a plus here for example. And so now we're going to see a bunch of pluses here. Maybe we can make them a little bit bigger 25 so that we can see them a little bit better maybe 45 or something just really get that definition. All right. So that's how we kind of see the. Then. Another thing we could do for example are triangles. And so that's going to be this kind of power to some more this kind of hat. And so those are going to give us triangles. So there's just a bunch of you know different things that we can do. So the standard one is the O which gives us the circles the right now the circles are huge. So let's reduce the size back to 15. But yes so this is how we can kind of manipulate the markers too. Now it may you know not be that kind of necessary now if we only have you know one plot here just because well we we only have this one plot so we don't really need distinguisher. But if we add an A second scatterplot for example. So you would ask how would we go about that. Well if we make a second y value for example so why is now going to be or will have a Y to like this which is going to be in other empty list. And what we're going to do here in creating or data we're going to do Y two dots a Penge and what we're going to do here is our why it's who is going to be two times. So we've got our Y value which is just I. So that's what we see here and our Y two data is going to be two times. And the reason that we're doing that is because if we're going to add a second Plaut to our scatterplot So we're going to add a second line. And so how we're going to do that is we're going to go up before we do Peale's he does show we're going to go here and look at go and make go into a paper map plot lived up high plot and we're going to access these Scouter method again and now we're going to plot x versus Y to. And now maybe to just distinguish them will set the color equal to read for y tooth and will make the marker be a plus like this. And so if we run our code now where we're going to see is here we have our wide two data which are the red. And here we have our standard y data on which are the green circles. And so what's really cool is now we have different ways of distinguishing our data. So we are distinguishing In one sense through the red color and the green color but we're also distinguishing through the plus markers and the circular markers. And what we also kind of learn is that we can create multiple plots within one figure here and that's just before we call this map up high plot that show method. We actually just put in different types of plots so we put in a scatterplot with x and y data and then we create a second scatterplot with x and y to data. And this gets all you know saved in the same figure and the same plot. And then when we actually show our data we see that everything is together in this plot and we see it all as one. Yeah. So that's it for the basics of scatterplot am. We'll look at also adjusting the the limits here and adding figure labels and all that stuff will look at that later. This is just the basics of scatterplot how we can take our data values and how we can plot them against each other and how we can make basic scatterplot using Map plot Phillip. All right so let's recap what we just learned. We saw how it can create scatterplot. We also saw how we can kind of plot two different types of data against each other in a scale. Scott scatterplot we saw how we can visualize our plots and we also saw how we can change the attributes of the scatterplot So we saw how we can change the size of our data points how we can change the color and how we can change the marker to from being circles to squares or pluss for example.