plt.imshow Video Lecture Transcript This transcript was automatically generated by Zoom, so there may be discrepancies between the video and the text. 17:14:00 Hi! Everybody! Welcome back in this video. We're gonna continue to learn about what you could do with Matt Pot Lib with the function. 17:14:07 Plot, IM show this is gonna allow us some show images directly with Matt Potlib as well as create things like heat maps and raster plots along the way. 17:14:19 We're also gonna learn about color bars and how they work in that platform 17:14:24 So the m-show command, whose documentation is linked here, takes in a 2D. Or 3D. 17:14:30 Array grid, and treats each grid point as a pixel intensity level the minimum grid value denotes a completely empty pixel, and the maximum grid value represents a completely fill. 17:14:42 The pixel in the case of a 2D. Grid m-show will assume the data is gray scale, and then in the case of a 3D. 17:14:49 Grid. You need to consist of 3, 2D. Grid. 17:14:54 So like a width times length times 3. So imagine basically that you just have 3 of the same size with times length. 17:15:03 So each grid point in a threed array would be the first layer would be the red intensity, the second layer would be the blue intensity, and the third layer would be the green intensity. 17:15:14 So we're gonna show first what you can do with him. 17:15:18 Show is just display images. So there's a file in this notebook called Can't pick 17:15:27 Can't pick. So here's the picture we can use. 17:15:30 Em show to show this picture. So the first thing we have to do is load it so you can load it with this image. 17:15:38 I/O to IM read, and you can see that's a 2,788 by 2,788 by 3 array. And, for instance, we can look at the first 10 entries of all 3 17:15:54 Okay, so this is sort of what this array is looking like. 17:15:57 This threed array so what we can then do is if we want to do m show. 17:16:05 Remember, we stored this in a variable called IM. So all you have to do is put this grid directly into m show. 17:16:11 So plot IM show I am. And now, when I call plot, show it shows the image. 17:16:18 Now let's do a quick aside. Maybe it looks weird for our image to have these axi lines on it. 17:16:25 Right, these tick marks. So one way that we can turn them off is just by setting the arguments and PET dot X ticks and y ticks, or alternatively, Ax set Xxxx dot set Y ticks to have no tick mark so to do that you would just input empty lists and it that 17:16:45 makes it so that there's no tick marks drawn. 17:16:49 However, I will point out that there are still axes here. Right. 17:16:54 These black lines indicate that they're still axes being drawn. 17:16:58 So you can turn the arrows off altogether with either the plot axis, or the ax axis arguments, and just put in the, or the string off. 17:17:08 Okay. So now, there's no access which you can tell, because the black lines aren't outlining the picture anymore. 17:17:16 Here's the example where I use ax dot access instead. 17:17:20 Okay, alright. So now that we've seen him show live up to its name by showing an image, let's talk about heat maps or raster plots. 17:17:32 So in addition to displaying images, we can also use, M showed to plot a heat map or a raspberry pi. 17:17:37 So heat map is a plot in which you display a grid of values. 17:17:41 Where the intensity of the pixel color in the grid point is representing the largeness or smallness of the value in the grid, so a raster plot is just a heat map where one of the axes either the horizontal or the vertical represents a 17:18:01 temporal, variable. So a time variable. So here's a random array, a random 10 by 10. Array. 17:18:09 Okay. 17:18:11 And we're gonna go ahead and use em show to plot this as a heat map 17:18:18 Okay, so this is what it looks like. So this is this caller map that we saw in an earlier notebook called Veritas. 17:18:28 I believe, and so we're going to look about something called cmap and color maps. 17:18:35 So we can control what color the grid is using. 17:18:39 The C map argument which stands for color map. So, as I said before, color maps are mappings that take in a continuous or categorical variable and return a color so for a continuous variable, you can put in that variable into a color map and return a color, that represents 17:18:56 Its magnitude in relation to the variables, maximum and minimum. 17:19:00 So. For instance, if the minimum here it's a unit, it's a random. 17:19:06 So the minimum here is probably something around negative 3 or something like that. 17:19:10 So of the observed values. That minimum here would probably be mapped to the solid blue, whereas the maximum would probably be mapped to the solid yellow, and these values in between which are greenish, is that transition from blue to yellow that mapping occurs that mapping from the number into the color is 17:19:32 Linear meaning that there's some linear function that sends the numbers to these colors. 17:19:38 So we can first demonstrate, like, how this works for this particular color. 17:19:46 Matt Verdus. So like the minimum. As I said, gets mapped to that solid blue up to the maximum which mapped to the the yellow. 17:19:55 And then this is how all the values in between get maps. So like, if you're halfway between, you get mapped to this color. And this mapping is linear, so like 17:20:07 Yeah, I don't know how to explain that. So there are other color maps that you can choose. 17:20:13 For example, we can make the color gray scale by using the C-map argument to m-show and setting it equal to the string, graze. 17:20:22 And so now it's a gray scale, and we can go ahead and look at the map plot lib documentation to see all of the different color grid, a color maps that are available. 17:20:32 So here's that Veritus and if we go down just a little bit, this is what it looks like for Gray. 17:20:38 's. So, for Grey's the smallest value gets mapped to white. 17:20:42 The largest value gets mapped to black, and then they get mapped somewhere in between. 17:20:48 For the rest. So these are all the color maps that are available with Matt plot lib. 17:20:55 I'll leave it to you to explore more. So these are nice, but unless someone like me comes along and says, Oh, okay, this goes to White that goes to black, or you're looking at this. 17:21:06 It it would be almost impossible for your audience to look at this and be able to decipher what like black and white need. So you're gonna want to add a color bar. 17:21:15 So a color bar is essentially just this. But instead of saying Min and Max, it's going to offer grid, it's going to offer tick marks that give you values that show you like. 17:21:27 All right. This would be negative. 3. This is 3, and then grid marks in between tick marks in between that help you, you know. 17:21:33 See, so we can add a color bar with the color bar command we call plt dot color bar 17:21:43 And so now, next to my M. Show, there's a color bar that shows you the different values and what colors they get mapped to. 17:21:52 So a completely white is a little bit less than negative. 2. 17:21:56 A completely black is more than 2, maybe around 2.5. 17:22:00 It seems like. So we can customize these in a number of ways, and I'll leave it to you to explore the color bar documentation, to see everything but some of the things that are most important are the location of the color bar so you can either set it to be left right top or bottom 17:22:18 So currently, it's on the right. The orientation. 17:22:23 So this is a vertical orientation, so if you were to put the color bar in the on the top or the bottom, maybe you'd want it to be horizontal, and then there's also an argument called shrink which can be between 0, and one, so this determines the scale of the color. 17:22:39 Bar, in relation to the size of the axes. So currently, I believe it's at one. 17:22:44 But if we sent it to be 0 point 5, then the color bar would be shrunk to be about half as large as the axes object. 17:22:52 So here's the example where I've set the location to now be on the bottom. 17:22:56 It's going to be a horizontal color bar, and it's only going to be 30% as wide as the axes is okay. 17:23:07 And we can see what if I had changed this to be 50%? 17:23:12 What if I change it to be 80% and so forth. 17:23:16 So let's go back to that point 3. We could also see what would happen if I change this to be vertical. 17:23:24 And we would get an error. So if it's on the bottom, it has to be horizontal. 17:23:33 Okay. 17:23:36 So let's take a minute or a second to talk about what are the best practices. 17:23:41 When using color maps and color bars. So it's hard for humans to sort of give a hierarchical meaning to colored transitions that go through more than one color. 17:23:53 So like blue, to yellow, blue to red, to yellow. These are hard for our brains to sort of like. 17:24:03 Say, okay. Yellow, what must be higher than blue? Typically what's recommended with colors like this is that you stick to a single shade. 17:24:11 So sort of like black, and then increase the intensity of the of the shade, so that's sort of what's recommended, because we can in view a hierarchy of this is blacker than that one then this one then that one and so forth. 17:24:26 Whereas if we're transitioning from blue to green to yellow, it's hard for us as humans to sort of imbue that with the meaning of okay, yellow is bigger than green is bigger than slightly less green, which is bigger than blue, so that's sort of the best practices. 17:24:41 That being said, there are always going to be instances in which it makes more sense to choose one that has multiple colors. 17:24:48 So sometimes the needs of the project that you're working on necessitate. 17:24:53 Choosing an option that goes against the other otherwise recommended. 17:24:56 Best practices. So one example I can think of is with weather and temperature, so oftentimes there's this mapping of timber where hotter things are mapped to red and cooler things are mapped to blue. 17:25:09 So if you're making some sort of plot involving temperature among different regions, perhaps you would want to choose one that goes from blue to red, because that makes more sense, or if you're doing something like a a chart looking at political leanings in America there are colors for that where 17:25:29 You know Republicans are Conservatives tend to be represented by red colors. 17:25:33 Democrats or Liberals tend to be represented by Blue. 17:25:36 So maybe you'd want a color map that goes from blue to red for those things. 17:25:40 So it's always important. Knowing these best practices is good it's also important to understand that the needs of your data project may be different than what the general standard best practices recommend. 17:25:53 Okay, so on this notebook, we learned about IM show. We also took some time to learn about color maps and how to use them in that plot. 17:26:00 Lib as well as what color maps are available to us in that platform. 17:26:05 So this is really useful, because remember, in scatter, we can color our points according to a continuous variable using a caller map. 17:26:13 So what are we gonna do in the next notebook? 17:26:16 We're gonna learn how to make a different chart type called a pie chart. 17:26:19 Just building upon our abilities with Matt Plot, lib.