matplotlib Multiple Plots on One Axes Video Lecture Transcript This transcript was automatically generated by Zoom, so there may be discrepancies between the video and the text. 16:11:11 Hi! Everybody! Welcome back! We're continuing to learn about Matt. 16:11:14 Plot lib by talking about what happens when we place multiple plots on a single axis. 16:11:21 Let's go ahead and get those Jupiter notebooks open so I'm gonna go ahead and open my version of the notebook. 16:11:26 You're gonna want the one that doesn't have any words. I'm gonna have the complete one, because by the end it will be complete for you to look at 16:11:32 So, so far we've learned plot and scanner. 16:11:37 Sometimes we're gonna want to put more than 1 one of these on a single axis, right? 16:11:42 So maybe we were gonna plot some observations and then plot a trend line or a regression line on top of it. 16:11:48 So what happens when we try and put more than one plot onto a single axis? 16:11:53 O'clock. Let's learn about it. So let's look at this example. Where we plot portions of the sign and cosine waves 16:12:01 So the sine wave is going to be blue. The cosine wave will be dashed. 16:12:07 Okay, so let me zoom in, and you can see we've got our orange dash line, which is what I told you was the cosine wave and the blue solid line which is the sine wave and we plotted the sine wave, first and then the cosine wave second so that was the order. That the functions. 16:12:25 Were called, and if I zoom in here you can see that it's a little bit blurry, because we're zooming in on an image. 16:12:33 But the blue line is underneath the orange dotted line, which means in Matt plot, lib. 16:12:39 Typically unless we change some settings when we call one plotting function after the other. 16:12:47 The plotting function that was called later gets pasted on top. 16:12:50 So we can kind of think of this as imagine we're gluing some something to a piece of paper. 16:12:55 So the first call is like gluing one layer of construction paper on to a piece of paper, and then the second call is like gluing a second layer on top of that. 16:13:09 So to to do so I wanna point out, another thing is, you can notice in this call to the functions that nowhere and either did I specify the color. 16:13:19 So in the first one the line was plotted blue. 16:13:22 This is the default color for Matt plot lib. 16:13:26 Then the second one was colored orange, now in the second call to plot plot. 16:13:30 I did not put in color equals Orange anywhere. This is something that Matt Plot Lib does by default. 16:13:38 So when you call plot plot multiple times onto the same axes, it notices when there is more than one plot call to a single axes, object, and then it changes the color accordingly. 16:13:51 So if I were to put a third call on here, it would change the color again. 16:13:55 This is a nice feature, but it does have a limit to how much it can work, so here's an example where I plot various shifts of the sine wave 11 times the so I'm gonna zoom in again. 16:14:11 And you can see how we went from blue, orange, green, red, purple, brown, pink, gray, yellow, a different blue, and then back to the original blue. 16:14:21 So eventually, which is makes sense eventually, Matt, plot, lib runs out of colors for it to use, and then it will start the cycle over again. 16:14:33 So after 10 unique colors, it starts back at that, beginning with the original default, blue color. 16:14:41 So if you planned on plotting more than 10 unique items with plot plot, and you were relying on the color alone to distinguish them, you're going to have a problem because eventually, after the tenth time it's going to start repeating those colors. 16:14:58 Again, okay, so this is something that that's why the I guess the best practice is what I'm trying to say. 16:15:04 The best practice so when you're doing something like this is for you to manually set your colors to make sure that they are different colors. 16:15:11 Let's look at a different reason why you want to do this. 16:15:15 So here I'm going to plot, plot, plot plot to plot a sine wave, and then I'm going to call, scatter, to plot my cosine wave. 16:15:23 Now you can see both of these are blue, both of these are the default blue. 16:15:27 So this behavior only works when we're calling the same function. 16:15:32 So, for instance, if I change this plot to scatter, we would then have the behavior of blue than orange. 16:15:40 Okay. But when it's plot, and then scatter both of these sort of have their own default. 16:15:48 Counters of okay. Plot has already been called on these axes. 16:15:52 So let's make it the different next color, and so forth. 16:15:56 So this is another reason why we should set our own colors by default. 16:16:00 If we would like to use color to distinguish between our lines another reason that we might want to think about other ways, to distinguish lines is sort of being mindful of our audience. 16:16:13 So sometimes we'll people in our audience don't have the ability to distinguish between 2 colors. 16:16:17 This, you know, has different severity. So there are people who only see in Grayscale. 16:16:23 There are people who have a hard time distinguishing between things like red, green, those sorts of things so, being mindful of the colors and other ways, we use to distinguish our plot elements is important when working in data visualization. 16:16:36 Anyway, we're gonna keep what I said in mind about this, and then just manually set the color. So our sine wave, we're now going to manually set to Dodger blue our cosine waiver and manually set to orange okay, and in addition, thinking of those people that might be 16:16:54 Colorbind. The cosine wave is also distinguished by the dash pattern 16:16:59 Okay. So you know, many of us probably remember what a cosine and assign wave looks like. 16:17:06 Do you trigonometry whenever we took it? 16:17:10 High school, college whenever. So remember, cosine of 0 is one sine of 0 is 0. 16:17:16 So we could use that to distinguish these 2, and remember which ones cosine, which one signed, but not everybody in your audience, I guess, depending on where? 16:17:26 Who your audience? What? Who your audience consists of is gonna remember something like that. 16:17:31 So it may be useful to label this plot, so they know what function the blue line is and what function the orange dash line is. 16:17:40 So we can do that by providing label arguments to our plot function, and then calling P. 16:17:46 Lt. Or ax dot legend. So here's a link to the documentation for legend. 16:17:52 So when you want to add a legend, you need to add labels to the plot elements that you've added. 16:17:59 So this is, plot, scatter whatever function you use. So for this we'll put label equals sign of X. 16:18:06 You'll notice these dollar signs that's telling us that we're gonna type something in math mode. 16:18:12 But we're gonna talk more about that in our text notebook. 16:18:15 Later in this folder, then for my cosine I put label Equals cosine of X. 16:18:21 And then when I call plt dot legend, it's going to look at the figures. 16:18:25 Know what color and dash pattern it is. Plot that line. 16:18:30 And then the word, the string from the label next to the line. 16:18:33 So I'm going to call plt dot legend. 16:18:35 And now my plot has the legend, where there's a blue solid line next to sine of X, and an orange dash line. 16:18:42 Next time to cosine of X. So legend, as I said, draws the relevant graphic corresponding to the label that we've provided it. 16:18:52 It takes in a few different arguments. So you can change the position of the election of the le legend. 16:18:58 Manually using the Lok or location argument. So, loc, there's various different numeric values that this takes in. 16:19:08 I believe one through 9, and you know you can go to the documentation link to see where each of those numbers takes it. 16:19:17 There's also the font size, which increases the font of the string, and there's more. 16:19:23 But I'm not gonna you know. Delve too deep. 16:19:26 If you're interested in learning what else you can do to customize your legend, go to this documentation link here 16:19:33 So let's talk about this. So we mentioned earlier that the order of things is dependent upon when you call them. 16:19:42 But you are able to change the order of things with something called the Z order. 16:19:48 So think of Z as the third dimension of these 2 dimension h mat, flat lid plots where think of Z as the deck? 16:19:56 Basically telling us what layer of the plot am I placing this on? 16:20:00 So I can change where something is plotted by cheaper. 16:20:04 The Z order argument, and the way it works is things with a lower z order argument, say, 0 1, 2. Those are drawn. 16:20:12 First things with higher Z. Order. Arguments 5, 6, 7. Those are drawn later. 16:20:18 So we can go ahead and plot something where I'm going to still plot this sine of X cosine of X. 16:20:24 But I want to plot a black line that is above sine of X, but below cosine of X and I'm going to do it without changing the order of the plotting. 16:20:34 So I'm gonna set the Z order the sign wave equal to one. 16:20:40 I'm gonna set the Z order of the cosine wave equal to 3 and then I'm going to set the Z order of my horizontal line equal to 2. 16:20:51 And so what this is going to do is it's going to plot the sine of X first plot. 16:20:58 The horizontal line second, and then plot the cosine wave. 16:21:02 Third. 16:21:04 Okay. And now we can zoom in again. So here we can see the black line is over. 16:21:11 The sine wave, but underneath the cosine wave, and importantly, the black line was plotted from the code was plotted. 16:21:21 Third, okay, so this doesn't have to be 1, 2, 3, as long as the sign was lower than the horizontal line, and the horizontal line was lower than the cosine. 16:21:32 This would work, so I could change my Z order to 100 and my horizontal lines Z. 16:21:39 Order to 50, and this would still work 16:21:42 See? Okay, let's go back to what I originally had it, as is 1, 2, and 3. 16:21:49 So you can set the Z order. With these arguments. There's also a function called Set Z order. 16:21:57 So we'll use this to demonstrate what you can do. 16:22:00 So my plt dot legend after I call plt dot legend I can do dot set z order, and then I can make this to demonstrate how it's working. 16:22:14 I can set this equal to 0, and when the Z order is set to 0, what does that mean? 16:22:19 Well, let's look. The sign wave has is the order of one. 16:22:23 The cosine wave has a Z order of 3. The horizontal line is is the order of 2, which must mean that the legend is going to be placed underneath all of that 16:22:33 And now we can see well, that is true. The legend is underneath all 3 well, it's hard to tell us the cosine, but it's underneath the sign and the horizontal line. 16:22:42 Okay, so set, Z, order is another function. You can use to change the order in which an element is added to the plot. 16:22:51 It doesn't. And again, it doesn't just work for the graphical elements. 16:22:55 It also works for layout elements, like the legend. 16:23:00 So now we have a pretty good idea of how adding multiple elements to an single axis object works in that plot. 16:23:09 Lib. We learned about Z order. We also introduced legends into our toolbox, so we can help those people help people distinguish which graphical element corresponds to what item alright.