Transcript:
Hi, everyone, I’m Ellie from Code of the Future. And today I’m going to be showing you how to plot graphs in Python. So as always. I’m going to put my glasses on and move you onto the screen. Okay, so we’ve jumped straight into PyCharm. I have created a file called graphs in python ready for us to do a bit of coding. So the first thing I’m going to say is being able to plot in Python draw. Different types of graphs in Python is vital for if you want to pursue a career in data analysis or if you are just really interested in creating graphs and observing data. So again that is one of the most important things we can use plots for is observing data. Obviously you’ll see at the moment, especially in England when they are holding coronaviru’s briefings and they show the data. Everything is done on a plot. So you can see the impact of coronavirus for an example, but we’re not going to talk about coronavirus today. We’re going to move away from that. We’re going to start by plotting graphs in python. They are as I said. They are incredibly useful and being able to do so. I think is probably one of the most rewarding things you can do as a beginner, Especially if you are wanting to pursue a career in data analysis. So enough of me talking, I’m going to jump straight in, and we’re going to do plots in Python. So the first thing that we’re going to say is Obviously I’ve just written this hashtag at the top just to say plotting graphs in Python. The first thing that I’m going to do is I’m going to assign X as a list and we’re going to make it a list from. Let’s say 1 3, 5 and then 10 Just a random list. Okay, So that’s our X. Okay, So say we have this list and say, uh, well. I want to plot this on a graph. I want to see how the x-axis changes. So what I’m going to say is before we can do any type of plotting. We need to import a specific module and what we’re going to do is just at the top of here. We’re going to drop this down. I’m going to put importing the relevant modules. Okay, and what we’re going to say is import Matplot and it’s there Plotlib dot pyplot as PLT. Now, if you do a lot of graph plotting, you will get used to remembering this off the top of your head. To start with, we might think. Well, that’s a really long import. What is it doing? It’s essentially importing the relevant module for us to be able to plot and we are calling it plt because when we go to plot instead of writing matplotlibpyplot we just say PLT, and then it will plot it for us. Okay, so I’m going to jump straight in and show you exactly what will happen If say we plot this X? We say okay well. Let’s say plt, which is what we’ve defined up here. We say Pltplot and plot X. Okay, now if we run this, what I’ll show you is if we run this, nothing happens and the reason that is is because you need to say plt dot show and just open brackets. What this does is it will show this plot. So we’ve asked it to show. So if we run this python file again, wahey there we go. We’ve got our first plot in python. Obviously it’s a very, very basic plot. You know, the axes aren’t labeled and we don’t really have a lot going on. So this is a very, very basic plot. This isn’t something that we want to produce in python;. We want to label the axis. We want to get a corresponding y-axi’s. Get a title, make it look a lot neater, but this is just showing you the basic way of plotting a very, very simple graph. So have a go at this. See if you can get something similar. So we’re going to cross off that for now and crossing off. It will subsequently stop the python file from running Equally. If you click run and it pops up here when you go back on to here, what you’ll notice is because this is still up and running when you go back onto your python file. The python file is still running. So another way you can get rid of this? Plot is by pressing this. Stop button! So we’re going to put here very basic plot. We want something better, okay. I’m just going to put in here. This is what we are plotting. Okay, and then we’re going to put here. This will show the plot we are plotting. Okay, so that’s a very, very basic plot. We don’t want something like that. We want something very, very nice. You know, different colours -. Let’s explore a little bit further. First before we go a bit crazy and start changing colors and and getting titles. I’m just going to show you how you can do it with a y-axis as well. So say we have X again and X is obviously already called here. Let’s say X is 4 students. Um, shoe size, Obviously. A size one is a little bit extreme, but let’s say it’s four students shoe size and then let’s say let y equal their age. So a size one. I think maybe, oh, I don’t know. I’m not entirely sure to be honest. I can’t remember how old I was when I got a size one shoe. Haha, but let’s say you were seven and then a size three shoe. Let’s say you’re around 12. And then a size five shoe well. I’m a size five shoe now, so let’s say, we’re 21 and then a size 10 let’s say something similar, Usually by the time you’re an adult, you have grown into your shoe size, So let’s say 22. Okay, so what we can do is we can plot these two things against each other and what we’ll notice is as age increases shoe size increases. Again, this is where data analysis comes in really handy, but for now we’re just going to show you a very, very basic example. Okay, so let’s say we have y and let’s say we want to plot y against X Again. We can do what we did above and remember. This is a very, very basic plot;. There’s nothing labeled. This is not the type of plot that you want to be producing, If say, you’re doing it for a university kind of level, but for the time being. I’m just going to show you you can plot two different lists against each other. So we’re going to just say plt dot plot X comma y and again, We’ll say, plot show. Now what’s worth noting is that this will show before this one, so the way that we can overcome that is we can either run this, and it will show this one first, and then this one or we can just comment this out, but I’ll just for the ease of it. I’ll show you what will happen. So I’ll click run and notice. This is the very first one that we had and then. If we cross off this there, we go -. There’s the next one. Okay, so perfect. We can plot two lists against each other. Now I’m going to jump straight in and show you exactly how you can create two different lines and plot them against each other nice plots, your own colours, axis titles and titles. So a very, very kind of formal plot that you would expect to see when doing some sort of data analysis. So this is obviously a very basic plot and again we’re just going to say here plotting X and Y against each other and what I forgot to mention. Although it did seem slightly intuitive. When I said if you are size one, you are age seven. What this will do. It will plot 1 and 7, 3, and 12. 5 and 21 and 10 and 22. That’s how it works in Python. So if I was to write here 23 and click run, We will get an error. The first one runs because the first one’s fine. We go across and we have an error because it says the list must be the same shape. Here we have a list of four and here. We have a list of five. So that’s just something to bear in mind. If something like this pops up, it just means your lists. Don’t have the same type of entries (number of elements). Okay, so we have these. And for the ease of this, We’re just going to comment. The plot show out because every time we run, we don’t want to be plotting those really poorly plotted graphs. I’m going to move down and this is where the fun starts. So we’re going to say, let’s plot a lovely looking plot, and obviously I’m not sure that’s how you’d describe it in data analysis, But I’d say let’s plot something that looks nice. Okay, so the first thing we’re going to do is we’re going to say, okay, line one, and these are gonna be points. Okay, so we’re going to assign a new X and y variable, just because we want something new. So, okay, let’s say 3, 9, 14 and then we’re going to say Y equals 2 7, 11 Okay, so they’re line one points -. This is the first line that we’re going to draw Again. This is exactly what we did last time we have two lists and they’re both the same shape. Which is what we want. So we have the points. Now let’s plot these. Okay, so as before, let’s just put a comment and say plotting X and Y So we’re going to say plt plot, Okay, so this is what we did before, but now we’re going to say X comma y comma, And this is where you can start putting some information about how you want the plot to look. So last time, we got a plot that was blue. Say, you don’t like the color blue, and you want the color red the way that you’ll do that is you will. Say c equals red. Okay, and for red to work, It has to be a string So. It has to be in these quotation marks Similarly. You can say well, let’s have line width. It’s automatically set to one. I believe in Python, So let’s say we want it to be a little bit bigger. Let’s say we’ll do two, which will make it a little bit chunkier. Python likes you to keep these. Pycharm sorry likes you to keep these close together, which is absolutely fine. Then finally, we’re just going to say label. Now what a label means is once we finish writing the appropriate code. At the end, we can ask Python to produce a legend. Now, if you’re not familiar with what a legend is, it basically means you have a plot and say you have two different lines. A legend will tell you exactly what color corresponds to which line (like a key). So in this case? This is our line one. So let’s say our label is going to equal line one. I’ll show you what this does to start with, so let’s say plot show, and as before, we have defined these things here, so we’re just going to comment these out just for ease because what will happen is if you ask Python to plot something here and plot it here, and then you plot it here. All of these will end up on the same graph. There is a way that you can overcome that by saying plot figure We’re not going to touch upon that today that will be in a later video. This is just showing you a very, very simple plot. Okay, so we just commented everything else out, just because we don’t want any more overlapping of of anything that we’ve done previously, and also I’m just going to change this 11 to a relatively large number just so we can see the difference in points. Just because I think those numbers were getting a little bit too close to each other. We’re going to click run. Aha, perfect! So we’ve got a red line and it’s thicker than it was before. Okay, now notice, no legend has popped up, and that’s because we haven’t asked Python to recall a legend just yet, but we’ve managed to get it red and we’ve managed to get the line width bigger. Okay, so that’s the first thing we’ve done Now. We’re just going to stop it up here and we’re going to move on to plotting. We’re just going to get rid of this plot show because we don’t want to plot it just yet, because we’re going to add some few things onto the graph before we eventually show it. So we’ve got rid of that. Now we’re going to say line two points. Okay, so we’re going to do line two points, Okay, So instead of X, we’re going to have x2 because this is a different list to this one. So let’s have 1 15 18 and y2 is going to equal 0 3, 12. Okay, and again plotting x2 and y2 and we’ll say plt dot plot x2 y2 We’re going to let the colour. What colour should we have it this time? Um, let’s say green. Getting in the Christmas spirit. It’s almost Chris. I mean, it’s not almost Christmas. We’re in November. But anyway, I’m getting a bit ahead of myself, haha. Then line width. And we’re gonna do a different line. Width this time. Just so you can see how line widths change. So we’re going to say 0.5. Now this will produce quite a thin line. Now we’re going to say label, and this is going to be line 2 Okay, perfect So again. If I put a little plot show here and we run this python file, there we go. We’ve got two different lines. And if I just expand this, you can see that. This line is a lot thicker than this line and this line here is also thinner. Okay, so we’ve done that we’ve got two lines. We’ve got different kind of colors and stuff. For the ease of the video just for for the niceness of the plot, we’re going to take the line width back to 2 so it’s in line with this one, and we’re going to get rid of this plot show because we want to add a few extra things on. So the axis labelling and everything like that? Okay, so now we’re going to say label the axes. Okay, so we’re gonna say Plt Dot X label, and we’re gonna label it So. In this case, we could go back with the shoe size thing, but I’m not sure a three-year-old will have size 15 feet. I think that might be a bit excessive haha. So, for the ease of this video, we’re just going to say, This is the x-axis and similarly we’ll say Plt Dot y label and we’re going to say y axis. Okay, so that’s the labelling the axes there. That’s how we do it. You can put anything in here that you want, according to what it is So. If it was shoe size, we would say X was. I think it was shoe size and y was age so this would be shoe size and that would be age. Okay, so we’re done labelling the axes. Now I’m just going to put up here and give the plot a title because obviously we always want a title on a plot. So again we’re going to put plt dot title very simple and the title is going to be two lines Simple, So we’ve labelled the axes and we’ve labelled the title. Now what we’re going to do is show the legend on the plot and what this will do is it will show these labels we’ve given to each of the plots. Okay, so to show legend on the plot. All you need to do is say plt dot legend. Very, very simple. Then finally, we’re going to say, get Python to show the plot and we’re going to put Plt Dot show as before, and we’re going to click run. Wahey There, we go Perfect. So we’ve got title. It says two lines. We’ve got our little legend here, so we know that this is our line one. And this was our line two. We have y axis labeled and we have x-axi’s labeled. Okay, perfect So. We have plotted a very, very nice two lines and we can extend this a little bit further, which is what? I’m going to talk about now So. If you are content with plotting graphs now, you know, you’ve had a little go. Then you don’t have to watch the rest of the video. The rest of the video is kind of showing you how you can change to scatter plots and have kind of cool little lines in between. So that’s what I’m going to talk about now. It’s not going to take as long. I promise We’re going to get into it straight away now. So let’s just cross off this python file here, sorry. I meant graph. Okay, so I’m going to just extend on what we’ve done already. Instead of creating a brand new graph we’re going to extend on what we’ve done already. So we’re going to keep line one as it is, and we’re going to adjust line two. Just to show you how you can change the lines and make them look a little bit nicer. So here we’ve got all these kind of extra factors for this line. So we have the colour is green. We’ve got the line width is two and the label is line two. Now we can add some extra things into here to make it Look a little bit funkier as it were. So we can add some extra things into here other than just line width and color and things. So the first thing we’re going to say is okay. Let’s have the line style as dashed and what that does instead of a line, It does a dash, which is going to look pretty cool already. I’m going to put a comma in here and we’re just going to enter Now. If you click enter and we still have these two brackets set up, then we can still type in here. It just saves me having to go all the way over here. Now, at each point, obviously, all these points are being joined together. Now, at each point, let’s say we want to have a marker at each point. So what we’re going to do is we’re going to say marker equals O, and we’re just going to put those back together because that’s what Pycharm likes. So I’m going to run this. I’m just going to show you exactly what this is going to do. So we’ll run in python and there. We go look at that. We’ve got a nice marker in between. We’ve got a nice little dashed line notice how the legend has also adapted to that as well. Okay, cool, we’ve got something like that. Let’s say we want to change the colour of the marker. So let’s say marker face color and we’re going to let it equal to. Let’s say orange. Orange and green, lets. See how that looks? We stop the python file running and we run again. Ah, error and do you know why? And this is perfect because I’ve been meaning to try and get errors in in my python code so that I can talk you through exactly what’s happened, Okay, and I know straight away what I’ve done because I do this all the time, and you’ll probably end up doing it at some point. You need to remember that colour is the American spelling and obviously here. I’ve put a u in, So we just get rid of the U. And then we’ll click run and perfect. There we go So. Now we have our little markers with orange, which I think looks quite cute to be honest. It looks quite nice, and now I’m just going to show you one last thing. You can say, let’s say marker size equals and let’s make this really big. Let’s say it’s 10 and you can guess what this does because we are plotting markers at the moment. It makes these markers really, really big, and that’s quite nice if you’ve got like a lot of of data, and you want to see exactly where the points are. Doing that will help as well. What’s worth noting is markers Don’t just have to be like this. You can put s and stop running and go again and it draws squares this time. This is just to show you how you can change line graphs in python. There is a whole list of all the different markers. You can use; the different colours. So after this video after you’ve watched this video, then just experiment. See what you can do? See what kind different plots you can make in python and have a play around with the different colors and everything. The final thing that I’m going to say is you can actually give the axis limits as well So limits of the axis, and we’re just going to say Plt Dot y lim, So we can give the axis limit. Say we only want it between 1 and 10 and similarly for X. Obviously it’s going to cut a lot of our data off because our data was within quite a large value. So let’s say 0 and let’s say this is 30 Let’s say we want to keep a large scale for the x-axis. So let’s stop running this and we’ll run it again and notice here that it has only done between 0 and 30 for the X, which is what we asked it to do and 1 and 10 for the Y. So this is a way of being able to kind of hone in a little bit and also another thing to say with PyCharm/Python as well. Is you get this little toolbar down here So? If you have this, you can move like this. So this is showing you the plot and it will go all the way up to 30 it was yeah. So this is kind of like a scroll button so you can move around. This here is a zoom button, so I could zoom in like this and it will zoom in and again. You can save the plot here and you can save it to your desktop and I can put two lines exclamation mark. We can save it. I will go on to my desktop and here it is – I can open it and it’s there. That’s how you save in PyCharm/Python. That is the video I am hoping to do shorter videos on very specific types of graphs, so say a pie chart histogram all the different types of graphs. I hope you liked the video. If you did then please like subscribe and comment and I will see you all in the next video:).