plt.plot Video Lecture Transcript This transcript was automatically generated by Zoom, so there may be discrepancies between the video and the text. 15:13:25 Hi! Everybody! Welcome back. And in this video, we're gonna continue on learning about Matt plot lib by learning about the plot plot function. 15:13:34 Now, if you have used Matt, plot, live, this is a function that should be familiar to you. 15:13:39 We're gonna dive into it a little bit more, and depth, and give a sense of what can be accomplished with Matt plot. 15:13:44 Lib's plot plot. If this is something that you're intimately familiar with, feel free to skip onto the next video or whatever video is most relevant to you. 15:13:56 So the plot command we've used in the past 2. 15:13:58 Notebooks to plot a line. So why don't we go ahead and talk about how it works? 15:14:03 You can call plot, plot, or ax dot plot. 15:14:08 If you have an axis object stored in a X. 15:14:10 Both work. You need at least 2 arguments for this function. 15:14:14 First, you need X, which is a list of horizontal coordinates, and then why? 15:14:19 Which is a list of vertical coordinates. 15:14:22 Plot goes and works in the following way. It goes through those horizontal and vertical coordinates, forms the Xy pairs plots those pairs as points on the axes object, and then connects the pairs with straight lines in the sequence in which they were fed into the command so 15:14:40 We're going to look at 2 examples. So first, I'm going to make my figure, and I'm going to call plot to plot these 4 points. 15:14:46 And then connect them with a line. So plot. So X goes first 1, 2, 3, 4, and then looking at my points again, I want to for negative 6 and 3. 15:15:02 So what this will do is it will plot the point. 15:15:04 One comma. 2, the point 2 comma. 4. The point 3 comma negative, 6, and then the point 4 comma 3, and then connect them in the order in which they were entered. 15:15:14 So one comma 2 will be connected to 2 comma, 4 will be connected to 3, negative 6, and so forth. 15:15:20 Okay. Now, to demonstrate the fact that the connections are made in the order in which the points were entered. 15:15:28 We're gonna do basically the same thing. But we're going to switch the order. 15:15:32 So that for comma 3 is the second point entered. So we're going to do plot plot for our X. 15:15:39 We want 4 or one comma, 4 comma, 3 comma, 2 for the why we're gonna do 2 comma, 3 comma negative, 6. 15:15:48 Contact 4. And I'm just getting these points by looking at my comments at the top of the code. 15:15:53 Chunk. So these are the same points that we entered here, but the only difference between example one and example 2, was the order. 15:16:03 So in this one the order was 1, 2, then the point 4 3, then the point 3 negative, 6, then the point 2 4. 15:16:11 So by switching the order the line that we get is dragastically different. 15:16:14 So this is actually a key point that I want to illustrate. 15:16:17 This is a very common mistake people make when they're using that plot. 15:16:21 Lib plot function is that they enter the points in the incorrect order, so if there is a particular order in which you want your points entered, make sure that your list or your array, or your tuple or your data frame whatever is sorted in the order that you would like them, plotted, so 15:16:42 There's a number of ways that we can customize these lines that get plotted by plot plot. 15:16:48 If you are so excited that you don't want to wait for this video to see the ones we'll cover. 15:16:53 You can check out the documentation for pi plot plot. 15:16:56 Here it's the same as ax dot plot, plot, function is the same for both, and you can go through all the different arguments that are possible just by checking out the documentation so the first thing we can do is change the line color there's a couple of ways to do this the first is we can 15:17:13 remember, after our ex and Y arguments just put in a string, and in that string we can put in an argument for the color. 15:17:21 So Matt plot lib is a few default colors that takes in just a single letter. 15:17:26 So, for instance, if I put in an R, it's gonna be red. 15:17:31 And so, for instance, here are all the colors that are available, so blue can be put in as be green can be put in as G red, cyan magenta as m yellow as y black as K. 15:17:44 Because blue is already using the B, and then white as W. 15:17:49 I've also included a graphic from the Matt Platlib documentation, with all of the spelled out colors that are available. 15:17:55 There's quite a few one that I like to use is Dodger Blue. 15:18:00 I'm not a dodger fan. I just think it's a fun color have included. 15:18:07 So another thing you can do is let's say that there's a color you want, but it's not exactly spelled out here. 15:18:13 You can go to a website like this and then choose the color that you want. 15:18:18 So like, just move it around, and you can move it around here, and then you see this thing with the little pound or hashtag symbol. 15:18:27 You can copy and paste this, and then put it in as your color argument, and then this will be the color of the line. 15:18:34 So, as an example, I'm going to make the line. 15:18:37 This hex. Argument. 15:18:41 And then it will plot that color. Okay? And then I think if I went here I could 15:18:49 Let's copy and paste it so we can verify that. 15:18:53 It's the color I wanted, hey? So I go back here. 15:18:58 See, and that's the caller. So you can come to a website like this. 15:19:02 You just type in, if you don't remember this website, you can always just type in hex code color picker, something like that in a web search bar and then you'll find a website like this. 15:19:14 And then this code the Hex code, is the one with the pound sign or the hashtag for Matt Plot lib. 15:19:18 You need to include that pound sign or hashtag. 15:19:22 Other software. You may not have to. So this works well, if we only want to specify the color or if we're okay with using the mat plot lib default, one letter colors. 15:19:34 Sometimes, though, we are going to want to do something like this. 15:19:38 And then, in addition, do extra customization to our lines. So if that's the case, we have to use either the C or color argument. 15:19:46 So instead of just putting the string, you have to type out the word color and then set in equals or alternatively, you can just type out the letter C, so the letter C and the word color are interchangeable arguments. 15:20:00 So this works. And then I can comment that code out where I'm using the word color. 15:20:06 And then uncommon to where I'm using the letter C, and that works as well. 15:20:12 Okay. So in addition to the color of the line, you can also change the style of the line, meaning the dash dot pattern that gets used 15:20:24 So once again there's a couple of ways you can do this if you're happy with the defaults from that plot. Lib. 15:20:32 So a solid, a dashed, a dotted, or a dash dot. 15:20:34 You can just enter it after the X. And Y. Arguments. 15:20:38 So if I do a string, and then I do dash that's going to make a dashed line. Okay? 15:20:43 So I guess hyphen, hyphen, and so now I have a dash line here. 15:20:50 Alternatively, I says, I said, your options are a solid line, which is just a single hyphen, a dotted line, which is a colon, and then a dash dot line, which is a hyphen followed by a period. 15:21:02 If you again like, let's say you want to make your color this in your line style, something else, or you want to use a line style that is not one of the 4 defaults. 15:21:13 You can use the line style, argument. So the line style argument does take in the default. 15:21:19 So here I have line stop equal to hyphen hyphen. 15:21:23 Okay, I can also enter in a tuple, and then that tuple I'll have the offset and the on off. 15:21:34 Seek. So what does this mean? So the offset is a number. 15:21:37 It's a float that just determines the starting point of the line. 15:21:40 So for us like I have a hard time telling the difference between one offset and the other, so I usually just set this equal to 0. 15:21:48 If I'm going to use it, and then on off sequence is going to be a list or tuple an array type that takes in alternating between the length of the solid line. 15:21:59 So the part here that's blue, and then the length of the empty part. 15:22:02 So here's an example. Where I'm going to set my offset is 0. 15:22:06 And then in the next thing I'm going to set a a pattern where I have a line of length, 10 in the first entry. 15:22:13 So this solid blue part will be of length 10, and then my empty space. 15:22:19 I'm going to make it a line or a of length. 15:22:20 4, so when I hit enter, this will make a dashed line where the dashes have length 10, and the space between dashes has length 4. 15:22:29 Okay, so you can compare and contrast the 2. The dashes on this one are certainly longer as well as the empty spaces, and if you're interested, I encourage you to sort of play around with this code. 15:22:39 Maybe pause the video, see what happens when you have different offsets and different values for this. 15:22:45 In addition to having a single dashed length and then space length, you can just keep going. 15:22:52 So here's another example where this is gonna have a dash length of 5, followed by an empty space of 4, followed by a dash length of 20, followed by an empty space of 6, and you can tell I've got my short dash, followed by my long dash and if I wanted to make the empty 15:23:09 Space, difference more obvious. I could do something like 15, and so now we can see that we've got small dash, small space, big big space. 15:23:17 Small small space, big big space. So I could even keep going if I wanted to like. 15:23:25 Maybe I'll get really extreme, and I could do 40, followed by 25. 15:23:31 Okay, so you can see small, bigger. Biggest. Okay. 15:23:38 So the dashes. Argument is another way to change the line style. 15:23:42 So there was the line styles argument, but the dashes. 15:23:45 Argument is basically the same thing, except you cannot enter an offset. 15:23:49 So dashes like us like is just said. Normally I would set the offset to 0 because I can't really tell the difference when it's not zoom so I would just use maybe the dashes argument, and I could take if we go all the way back to the original example here. 15:24:05 I could take this example, and then set it equal to the dashes, is equivalent to the on off sequence. 15:24:14 Argument from line style, so dashes is just going to be equal to a tuple or an array where it alternates between the length of the solid line and the length of the empty space. 15:24:24 Okay. 15:24:27 Adding markers. So these are just lines in addition, you can add markers. 15:24:33 So if I just like with the line style and the call, if I don't want to change too much, and I just want to add markers, I can add in a marker argument immediately following the X and the Y, so for this, I'm gonna do my hyphen which means I want a solid line, followed 15:24:50 By a little O, which is going to put in points. Okay? 15:24:54 So now you can see that each of the 4 points that we've plotted, and then we're connected with solid lines. 15:25:01 These are now marked with a blue marker. I want to also point out that we have to include the heightened first. 15:25:08 So if we don't include the hyphen what's going to happen is we're only going to plot the points. 15:25:14 So here is an example where I only put it in the oh, the little O lowercase! 15:25:17 O, and no hyphen. So what's gonna happen in this example is that I'm gonna be left with just the 4 points. 15:25:25 Okay. So if I don't have that hyphen, you're just gonna get the markers 15:25:33 So the default markers that you can use can be found at this link so you can get a point with just a little like a very small point with a period, a pixel with a comma which you can't even see a circle with the little O and so forth. 15:25:48 So there's variety of examples. And the one that we're going to use actually in the later example is the Pentagon. 15:25:54 With is a lowercase piece, so we'll see that later. 15:25:58 So you can automatically instead of doing this. So there's gonna be examples when we're gonna want to do things that are a little bit more complicated than just a single marker. 15:26:09 You can also set the marker argument. So here I can set the marker argument equal to my circle. 15:26:15 My little O, and it's gonna be exactly the same plot. 15:26:20 Okay, so markers, in addition to line style and line color, you can change the appearance of your marker. 15:26:26 So you can increase the size of your marker with marker size and set it equal to a number that's not negative. 15:26:34 You can change the color of the inside of your marker with marker face, color, you can change the color of the edge of your marker with marker edge color and you can change how wide the edge of your marker is with marker edge width so here's an example where I have these 15:26:49 circles on the points they're gonna be of size 15. 15:26:53 They're going to have a green phase with a black outside, and the edge is going to be 2 units thick. 15:27:01 Okay, so now you can see, we have these bigger circles that are green on the inside, black on the outside. 15:27:08 And they're big 15:27:09 So sometimes it's desirable to change all of these things at once. 15:27:15 So if you're really happy with the defaults, and what do I mean by the defaults here? 15:27:20 So the defaults are these 4 options for your line style solid, regular, dashed regular dotted, regular, dotted, as well as these colors that are just single letters, and then these markers that we alluded to at the Matt plot lib documentation if we're happy with all 15:27:39 Of those you can specify. All of that in a single. 15:27:42 Argument immediately following the ex and the y. So in this example, after my X and my Y, I'm gonna put in a string that has the letter R, which will turn my line red hyphen, followed by a period which will give me a dash dot line followed by the letter p which will put 15:28:03 Pentagons on each of my points. So by doing that I was able to change all 4 of or all 3 of those things at once. 15:28:11 And I have a red dash dot line where Pentagons are covering my points, and I could increase the marker size if we wanted to show that these are indeed Pentagons 15:28:26 So, if you want to do something beyond these defaults, however, you are going to have to use the C or color argument. 15:28:32 The marker argument, the line, style or dash is arguments. 15:28:39 Directly you can change. How see through your line is so right now my line is solid Weenie. 15:28:45 I can't see through it at all. You can change how much you can see through it with an alpha argument. 15:28:50 So Alpha takes the value between 0 and one and if alpha is 0, your line is completely see through meaning. It wouldn't even look like it's there. 15:28:59 And if your Alpha is set equal to one, it would be completely solid, like the default. 15:29:04 So here I'm going to set Alpha equal to one. This is the default. 15:29:07 So I'm going to have a black solid line and then, shifting my line one unit down, I'm going to set my alpha equal to point 5, so you can see the difference. 15:29:18 So the line up top was Alpha equal to one, and now you can see the line on the bottom is Alpha equal to 0 point 5 so you might be wondering why I'd ever want to set my line to be see through sometimes your line. 15:29:30 May be over other things, you'd want to see. So we could imagine doing a regression where I have my observed points, and then, on top of it, I plot my regression line and maybe I'd like to be able to see through my line to see the observation like the little points as a 15:29:45 Scatter plot. So that's an instance. So Alpha gets used all the time to be able to show data points that are maybe below other data points. 15:29:57 Addition, I can make my lines wider with the line with argument, so the default is aligned with the one, and below that default I'm gonna do the same thing is with the Alpha plot, or the lower one. I'm gonna make a wider. Line. 15:30:10 So I'm gonna make this 1 3 times as wide. 15:30:12 So line with equal to 3 okay, so we can see up top. 15:30:18 This is the typical line. Width, and then this is a line with that is bigger. 15:30:23 I think the last thing that we're gonna talk about is plotting time series. 15:30:27 So line plots are often used for time series plots. 15:30:32 So observing the value of a variable that changes over time, or a different steps that are, one step comes after another like a sequence so oftentimes time series have date, time, objects as the X variable. 15:30:47 So that's why. Here I import the date time functions and methods and classes. 15:30:52 So what I'm gonna do is I'm gonna generate a fake time series. 15:30:57 So my time series. I've got this variable T, which is a series of dates starting at January the first 2,018, and then I'm going to make a random walk here, using some numpy functions and what I'm gonna do is I'm gonna plot my time. 15:31:13 On the x-axis, or horizontal axis, and then my random walk on the vertical axis. 15:31:18 So t we comma y, and let's see what happens when I do this. 15:31:22 So it plots this random walk, and you'll notice that immediately on the horizontal axis, my! 15:31:34 It it accommodated the date time, so all these dates that were on T, and so maybe it makes sense to print out. 15:31:41 T, okay? And maybe we'll just look at the first 5 to make it digestible. 15:31:47 All these dates immediately. We're accepted by Matt. 15:31:49 Plot, Lib and the horizontal axis was changed into into a date time access. Okay? 15:31:57 So January of 38 July of twenty-eight-eight, and so so Matt plot. 15:32:02 Lib plays really nicely with date times, Us. Or so, if you're horizontal or vertical, variable, we're in date time format. 15:32:13 Matt Plot lib is able to plot that in a sensible way. 15:32:17 Okay, I will point out, though you still have to feed the data incorrectly. 15:32:23 So you couldn't do something where the data was not in the correct time. 15:32:26 Order, and still expect the points to be plotted correctly. 15:32:30 Okay. So we've now introduced the Matt Plotlib plot function. 15:32:35 Both plot, plot, and ax plot. I know we haven't. 15:32:40 We didn't do any ax dot plot functions here, but it works the same way. 15:32:44 You have an axes, and then you would just put ax here instead of plt. 15:32:49 Okay, so this is one of the biggest functions you're gonna use when you're using Matt Potlib, probably. 15:32:56 So it's a good starting function in the next. 15:32:59 Not journal. The next notebook. We're gonna go ahead and learn about. 15:33:04 Then probably the next most common plotting function scatter, which is used to make scatter plots. 15:33:10 I hope you enjoyed this video. I enjoyed having you watch this video and I hope to see you next time.