Patches Video Lecture Transcript This transcript was automatically generated by Zoom, so there may be discrepancies between the video and the text. 12:10:35 Hi! Everybody! Welcome back in this video. We're going to continue our work on Matt. 12:10:40 Plot lib by learning about patches. So patches are going to be a way for us to add additional shapes that are not necessarily representing data. So it's a little bit different than scatter or plot. So why don't we go ahead and get started? 12:10:56 So in this notebook I said, we're gonna learn about the patch class. 12:10:59 And then we'll use that to add circles, right rectangles, and then at the end I'll give you some hints as to you can how you can add additional shapes to your axes, using the patch class so the patch class and Matt plot list as I've been saying 12:11:14 Allows us to add a suite of standard geometric objects. 12:11:18 So polygons shapes, etc. And an actuality every time we've called something like scatter or hissed, or bar, or bar h. 12:11:27 What's being what's going on in the background is that specific patch objects that have the features of the data that you're trying to represent are added to the plot in the background. 12:11:38 So in this, we're going to show you how you can add patches on their own without necessarily needing the data elements. 12:11:43 So sometimes these are useful if you'd like to annotate your plot or give it a different aesthetic qualities, or maybe highlight portions of the data and then at the end we're actually going to see an example of why we might want to use a patch to make a different type. 12:11:56 Of plot. So the patch class is stored in the patches. 12:12:02 Module of Matt plot lib. So we're going to import it, and it stands from the documentation to import this as M. 12:12:09 Patches, and so the first thing we're gonna learn how to do is see how we can make a patch object. 12:12:14 That is a circle so the way we're gonna do this is, we're gonna use the Circle command which creates a circle object. 12:12:21 So the documentation to this is linked here so you can go through and learn more. 12:12:25 Basically what we need to do when we call circle is we're gonna have to put in a tuple or a list or an array with the X and Y position of the center of the circle, and then the radius of the circle so let's go ahead and we're gonna make a circle patch. 12:12:42 Centered at point 5.5, with the radius of point 2 5, so I call dot circle and patches dot circle, and then not X comma y, but a tuple of X comma y, so 0 point 5 comma 0 point 5 that's our center and then a radius of point 12:13:01 2, 5. Okay, so now you might be wondering, why did I get a figure? Object? 12:13:07 With 0 axes if I decided to add a circle. 12:13:11 So let's talk about what's actually going on here. 12:13:14 If I make an additional code chunk and just put the circle part in we'll see that this created the mat plot lip circle object. 12:13:21 But unlike when I call things like scatter or plot, it doesn't actually append it to any axes. 12:13:28 So we have to manually add these to the axes ourself so what I'm gonna do in this next code, chunk is first, create. 12:13:34 A figure with some axes on top, and then I'm going to go ahead and add the patch, using a X from my axis dot. 12:13:44 Add underscore patch and then input, the circle object I created. 12:13:48 So now that we do this, the command add, Underscore Patch is going to take our patch out and place it onto the axes and so now we can see the circle centered at point 5.5. 12:14:00 You can trust me that the radius is point 2 5, and it's colored. 12:14:05 The default blue that most of our mat plot lib objects are colored as so circle has aesthetic commands, just like most of our other mat plot, lib commands things like color, face, color, edge, color, Alpha and line with you can see more at the circle documentation so here's an example. 12:14:24 Where I make a red circle with a black edge that is a little bit. 12:14:28 See through, and then a very thick edge. Okay. 12:14:34 So another patch that you might be interested in creating our rectangles. 12:14:40 These are a another common one, and then there we go. I've accidentally clicked on documentation for the rectangle. 12:14:46 So when you create a rectangle, you're gonna call M. 12:14:50 Patches dot rectangle again. You input a twouple of x y, which is here. 12:14:54 If you can see through the diagram that I'm highlighting with my cursor. 12:14:59 Xy gives you the position of the bottom left hand corner of your rectangle. 12:15:06 Then you enter the width, and then the height, and we'll get back to some of these additional arguments in a second. 12:15:14 So let's just add a simple rectangle first. 12:15:16 So I'm going to call. Add Patch, M. Patches, rectangle the first thing I need to put in is my bottom left hand corner position, which, according to my notes, should be point 1.1 so point one comma 0 point 1 now, I need to enter my width and I want that 12:15:33 To be point 7. So that's next, and then finally, my height, which I want to be point 3. 12:15:39 So I will add and finish the parentheses to add pads. 12:15:43 And now, when I run this, I'll see a rectangle up here. 12:15:47 According to these specifications. A nice thing you can, you know, customize the caller, face caller, edge, color, etcetera. 12:15:56 But another nice feature is there's the angle argument which we didn't have for circle, right? 12:16:00 Because, no matter how you rotate, a circle, it looks like a circle as long as you're rotating around the the center. 12:16:07 So what you can do is, there's this argument called angle, that takes in a number of degrees. 12:16:15 So not Radians, but degrees. And that's how far the rectangle is rotated. 12:16:19 Counterclockwise, and then are you also input the argument rotation point X, y, so it will rotate it. 12:16:25 That many degrees around the point X comma y. So here I've input some aesthetic features that make it a black, outlined red rectangle. 12:16:37 And then I'm going to rotate at 300 degrees counterclockwise along the point point 2.2 12:16:45 So with this example, we now notice in addition, just like with plot text, our axes do not change the bounds of their their axes themselves do not change the bounds in order to accommodate the patches that we're adding it's much like plot text so if we 12:17:04 would like the rectangle to be fully encompassed by the axes we're gonna have to manually adjust the limits of the X and the Y-axis ourselves. 12:17:12 So here I do this, making a larger range for the Y access. 12:17:16 The vertical axis. And now, my rectangle is fully within my axes, so there are a lot of other patches we've seen the circle in the rectangle, but you might want to make something else like a polygon. 12:17:30 An annulus. A wedge, an arrow. And so, if you go to this patches, Api documentation page, you can see a list which kind of a breakdown of all the different patches you can make. 12:17:43 So you can make an ellipse an annulus, and so forth, and then they have different links to the documentations for all of those specific shapes that you can get to. 12:17:52 Okay. So before we sign off, I want to give you an example of when you might want to use a patch object. 12:18:00 So there are these things called donut charts, and the only difference between a pie chart and a donut chart is that a donut chart has the center removed so like they basically take a smaller circle and then cut it out like a donut hole. 12:18:14 And so it looks like a doughnut, but it still is the same, representing the same information as a pie chart. 12:18:20 Just that we have removed like a small circle from the middle of the larger pie. 12:18:27 So that's why it's called the donut chart, because it looks like a donut. 12:18:29 So these have become kind of quite popular with a lot of dashboards like Google dashboards, things like that. 12:18:34 So if you want to make a donut chart in that plot, lib, you just make a pie chart and then put a white circle patch in the middle. 12:18:42 So here we make a pie chart, and that's where we do. 12:18:47 The pie part. Now, the next thing we have to do is take the whole out of the donut. 12:18:52 So ax, dot, add patch, and then M. Patches dot circle, and then what do we have to do? 12:19:00 Well with you. X comma y. So for us it's gonna be 0 comma 0 because that's the center of the pie. 12:19:06 And then why don't we do a radius of point 5, and then see how it looks? 12:19:11 And then we want our face color to be white and our edge color to also be white, hey? 12:19:19 And so we can adjust it. If let's say we want a bigger circle taking out, we can increase the radius to be point 8. 12:19:27 Say, or if we want a smaller circle cut out, we could make it point 2 5. 12:19:34 Okay, so I actually kinda like the look of point 5, the best. 12:19:38 So that's what I'm gonna stick with. 12:19:41 But you're welcome to do whatever you want. And so now, you see, we have a donut chart, which is maybe a little bit more invoke right now than than the classic pie chart. 12:19:49 So now we've seen how to append the process of appending different patches, and we've given you a link to the documentation where you can see all the different patches that are available to you, and we've kind of justified one at least one example where you might want to add a 12:20:04 patch. There are plenty more, so that's gonna be it for this video. 12:20:09 I hope you enjoyed learning about patches in the next notebook. 12:20:12 We're gonna come back and show you how you can take all these wonderful charts that we've learned how to make and put them into a single graph with subplots. 12:20:21 So things like subplots are very popular and doing exploratory data analysis. 12:20:26 And in making sort of dashboard ask figures. 12:20:31 Okay, I hope you enjoyed this video. And I hope to see you next time.