Transcript:
Okay, in this video. I want to show you how to, uh, plot something in Python. For example, how to plot a function in python. It’s pretty straightforward, so what we want to do. First is we want to import the libraries? We’re going to use okay, so if we’re dealing with numbers and math, uh, one of the typical ones is to import numpy. Uh, as np is kind of common notation. And, uh, we want to import the plotting library into Python and this is import Mat plot Lib Dot Pi plot. Okay, so this is the function that we’re going to call, uh, to plot things and to call this library, We’ll just use, uh, plt. It’s kind of the common notation. Okay, So these are the the two you need. Um, for this example. Uh, you know, if your function calls other things, you might need other things. Um, but but the first step is to define the function. You want to plot, okay, So, in this case, we have a function that we need to get a list of data points for, um, so, uh, let’s say you have a parabola that you want to plot, so let’s define, and then you have to call your function and so we’ll call it. Um, f it’s like a classic, you know, F of X But, uh, this in parentheses, this X will be our variable and then we can also have arguments. Okay, so in a parabola we can have, um, ax squared plus bx plus C. Okay, so let’s do some constants A B and C. Um, and then you’ll see this is indented, and then we need to define what to return from this function. Okay, so, um, we’ll do a times X squared plus B Times X Plus C. Okay, so this is just a simple parabola, and, uh, it’ll everything defined within these parentheses will be defined locally in this definition. Okay, so it doesn’t matter what I put here I could put, uh, H as long as this is the value H that you want. Um, when you input it here, it’ll put it into this return. Okay, uh, so I’ll explain this in a second, but the idea is, this is how we set up a function. Okay, so now to plot something, you need a list of data points, You need, uh, X values and y values. Okay, so let’s make an X list. Um, an easy way to do. This is with a numpy function, So we’ll call Numpy. Um, you can use linspace. So this is a linear space. Uh, so it’ll make a linear spaced list between the start. Point the stop point, and then you can put your number of points and then, uh, you know, all sorts of other things you can, of course add, so we’ll say we’ll start at minus 10 We’ll go to 10 and we’ll put. Um, let’s say you want a thousand data points. Okay, so we can evaluate this and what you see here in your variable explorer is going to be, um, a list that pops up X list and it’s from -10 all the way to 10 and it’s a thousand points, Okay, So this is the size There are, of course, many ways to make lists. Um, another example is you could do. X list equals np a range and this will let you do a, um, a starting point and a stop point and a spacing. Okay, so you can go minus 10 to 10 with a spacing of 0.1 for example, Um, and so if you look at this now, you’re so if I have this defined after this, you know, this function is going to overwrite it, or you could just comment it out with the parentheses. Um, you see now. We have a list for minus 10 and the spacing is 0.1 Uh, but you know, one thing with a range is at the end. Uh, it doesn’t. Include the last point. Okay, it’s a 9.9 So what you need to do is is, you know, add an extra spacing to the end, so we make this 10.1 and now you see your list goes, uh, from minus 10 to 10 Um, so it’s not inclusive of the last point, which is 10.1 okay, so it doesn’t matter what you use. Um, you know, you can get this several different ways, so this is our X values, and now we have our Y values, so our Y list is just going to be the function evaluated at X list, and then we have the parameters a B and C so we can say a is 3 B is 1 and C is four. Okay, and that’s it. Look, now you have an X list, you pass this X list into the function and you have a y list and so this is our values, so we have an XY data set. Um, there’s another video where you can show how to plot an xy data set. Um, this is, uh, where that video would start you already? Have your experimental data set, for example, here you need to generate you. Know your list of points, okay, but after you have a list of points, the plotting is all the same. Um, so we can use, uh, matplotlib. Okay, And so the the easiest thing to do is just to define your figure first. So you you call this plt library and we’ll say figure, and, um, we need to define the number equals zero. You can always start your accounting at zero. If you have several figures, you can do zero one. Two python starts at zero. So it’s a good practice to start that, but you can call it. Whatever number you want, um, and personally, I like to say, uh. The DPI for the figure is excuse me. DPI equals 120 just so you have some good resolution and to actually plot it. You do Plt Dot plot and then you give it your X list and you give it your Y list. And that is it. Run this function and we’re using spyder. And so you’ve got this plots tab down here. Here’s your plot, so all you have to do is generate an X list, generate a Y list and pass it to the plot function. And you get your plot. You can right, click. You can save this. You can do whatever, um, so so this is. Um, very basic plotting now. What I’ll show you is we can. Um, make this a little more. Uh, interesting by by adding some defining features into, uh, the plot. Okay, So, for example, let’s say you want to add a plot title. Uh, we can add a title and we’ll just say plotting example, and if it’s in quotation marks, this is a string. And so if you run this, you can get, you know, title on your plot. You can also add an X label. Um, so your x label we can say, imagine. This is a pendulum swinging, okay. This is a an example Parabola. So you could say X is distance in units of, uh, you know, feet and we can add a y label, which is the same thing we do. Plt dot y label. And we could say this is the, uh, height. Oh, in feet. Okay, so now you can have X and y labels which pop up, um, some other useful ones is you can add a, uh, a label to this. Okay, let’s say you want to add another plot, okay, so if you’ve got this defined, you can add as many data sets as you want to this figure, so let’s say, for example, that you want to add another data set and let’s say, uh, so you don’t define a new figure. You just call the plot function again and we’ll say X list and, uh, y list to the, uh, the square root one half power. Okay, so this is X list and then, uh, the parabola and then the square root of the parabola and so we can. We can plot this here, you see now? We have two plots, uh, or two data sets on this plot. When you have two things, it’s very useful to put a label so you can add another marker. We’ll say label equals, and then this would be F X and for the second one we say label equals, and this is f of X to the one half, right, so so now we need to add a legend and, uh, boom. Now you have a nice label. And, of course, there’s so many different things you can do to control where this goes, Another useful feature for making a plot like this is to, um, to make this a subscript or superscript, so for example. If you don’t want to write this, um, you know, to asterisk marks, you can use what’s called math text, okay, and so for math text. Um, I’ll pull up the browser here. You can go to matplotlib ill. Put the link in the in the description. This is called. Uh, uh, math text. It’s writing mathematical expression, so you can use instead of saying Alpha you can use, you know, Greek notation you can do fractions, You can add superscripts and subscripts and special fonts. Um, so you can have all these different things. I mean, all the details are in here. Um, and so how you would do That is, uh, for example, you call, uh, before the, um, quotation marks, you put an r and this means it’s a raw string, so it’s going to take every, um, marker as it’s or every, uh, character as its raw value, and then you can add these special notations, so for example. Um, we want to start doing the math text here, so I’ll add a dollar sign to start the math text, and then I’ll say, um, carrot will make this, you know, go raised up to a certain power and we’ll say 0.5 and then I’ll end the dollar sign here. This will say everything within this use math text, so we can put it to a superscript and for example. This is what you see. I run this and now you get f x to the 0.5 the square root of the function. Um, and of course, if you want to make this one half, you can also make this. Um, a fraction. Just go to your just go to that webpage and you can see how to call everything. Um, so these are kind of the basics. You can also modify the, uh, how the plot looks so say you want this? Um, square root function to be dashed. You know, after you put your y list in, you can add, uh, special commands to control. What the, uh, plot looks like so here. I’ll add a double line and you’ll see now. It’s dashed, you could call specific colors, For example, uh, double line and G will be dashed green. Um, so that’s generally it now there’s. This is kind of the basics of what I would use plotting functions for. Uh, you know to start off with, but, um, don’t take this video as you know, the gospel. Take a look at the, um, here’s Matplotlib website. This is everything! If you ever want to know more about a function, for example, Linspace a range matplotlib you know, Google it and you’re going to find the, uh, the function with detailed descriptions on how to use all of it If you type, um, you know Matplotlibpi plot into Google, you can pull up this Pi plot function specifically, and it’s going to tell you. Look, it tells you this is all you need in X list A Y list, and then you call your X and Y values, that’s all you need to do to plot and, um, so this, I mean, these are the four five lines you need to plot a function, You know, we’ve added a little more to make it interesting, but these are all the different. Uh, you know, functions you can call into your matplotlib okay, so, um, so for example, we’re we’re using plot, so I’ll look at this and this will tell you Matplotlib pipot and then plot and then we call plot and so this will tell you all the arguments, which can, uh, be called how you can control the formatting of the curves and adding labels and different. Uh, you can add markers instead of lines, line styles and there’s tons of examples So anyway, all the resources are out there. Um, and another thing I should note. Is that your function here? Just be very clear about this function you’re calling. Um, you don’t have to so so here. We put the ABC inside, but you can also say, you know, A equals three and B equals one and C equals four. Okay, and then you can call, uh, past those A B and C values to the function. That’s another way you can do it. So if you quickly want to change your a, you can do that, and you know, it’ll shift your curve. Um, but just know that whatever you’re doing with these values, it’s totally fine to call a B and C here and pass a b and C here. It’s going to pass these values that are defined. Um, but if you define them here, they’re going to be defined for the rest of this script. Um, but they have no bearing on functions. Okay, so for example. If I define another function, uh, below this and I I use, you know, function two, and I also pass an A B and C. It’s not gonna pass these values to the the function when you’re doing a definition, the definition, um, is all inclusive of what’s within the indentation. Okay, so whatever you’re putting inside the parentheses when you define a function, these are just referring to the different, um, notation of what’s inside. So for example, you could make this a naught and a naught, but it’s just referring to this. It doesn’t refer to parameters outside of the function. Okay, so it won’t refer to parameters outside of the function until you actually type it outside the function like this. Now, when it’s outside of a definition now it’s actually going to call the the definition of these. Okay, so don’t be confused about how we define these functions. Um, these parameters are only defined locally, so you can also define them below and it won’t affect this Or you don’t have to use ABC. You can use any notation you want as long as the position that you want it to pass. The value to is is clear in the return. Okay, so that’s it for plotting again. This is way more than you need to do. Do you need to add labels? Do you need to add titles? Do you need to add legends and special formatting? No, probably not, um, but if you want to. This is how you do it, okay.