Adjusting Non-Graphical Elements Video Lecture Transcript This transcript was automatically generated by Zoom, so there may be discrepancies between the video and the text. 12:48:03 Hi, everybody! Welcome back in this video we continue to learn more about Matt. 12:48:07 Plot, lib. Specifically, we're going to learn how to adjust the non graphical elements of our plot like the axes, the appearance of the axes, the appearance of the figure, and so forth. 12:48:18 The figure object, like the background of the figure. Not necessarily the data elements, like scatter plots or anything like that. 12:48:26 So let's go ahead and get started. So up to this point, we've really focused on making different types of charts. 12:48:34 And I've told you a lot that oh, don't worry about various things like the titles or the tick marks, or anything like that, cause we'll focus on them later. 12:48:43 That's the point of this plot. So we're gonna imagine that we're we're happy with a plot that we have. 12:48:48 So let's say that we were studying the results of an experiment that looked at the impact of sleep hours on worker productivity. 12:48:57 And our goal is, we're not necessarily preparing this for an academic publication, but maybe for a publication and a Pop science magazine. 12:49:05 So something we expect the general public to read so here's an example. 12:49:10 So here is just me like simulating, getting this data. 12:49:14 Then fitting the regression and getting the regression fit line 12:49:19 So here's what it looks like. This is just plotting the data, using scatter and plot. 12:49:25 Okay, so how can we change the different aspects of our plot? 12:49:29 For instance, if we're going to plot this in some sort of magazine, we want to make sure that the readers understand well what is the horizontal axis doing? 12:49:39 What is the vertical axis doing? What is this? 12:49:41 Even a plot of. Is there a way I can describe it to them? 12:49:44 So they get the takeaway message and then, depending on where you're publishing and your own aesthetic preferences. 12:49:49 There are various things you might want to change about the way the axes look like the tick marks. 12:49:54 Do you want to add a grid line? Grid lines in the back? 12:49:57 Do I want to remove the border of the axes, object, etc. 12:50:02 So the first thing we can do is, how do we add a title to the plot? 12:50:08 So titles are often used when presenting graphics. So an academic work sometimes they're left out. 12:50:14 But other times they're put in is sort of like an explanation. 12:50:18 A quick snapshot of what the figures about, and then in popular science ones they're more oh, actually, let me pause because we're gonna get to that in a second. 12:50:26 So how do I set the title? There are 2 different ways. 12:50:29 If you have a specific axis, object that you'd like to place a title for you do ax set title if you're happy with doing the most recently added axes object. 12:50:40 You can just call plt dot title. So this includes a lot of our standard font arguments, text arguments like font, size, color, as well as an argument that allows us to specify the alignment with respect to the axes, meaning should it be aligned on the left should it be aligned in the middle should it 12:50:59 Be aligned on the right, etc. This argument is called Llc. 12:51:03 Just like with legend standing for location. So I'm gonna add a title of plot title. 12:51:10 And this is just gonna be a placeholder title until we can talk about like a placeholder. 12:51:17 Actually don't remember if placeholders, one word or 2 words, so we'll just leave it as as it is, for now it's just a placeholder. 12:51:23 It could be anything, a placeholder, title. And there we go, and then in the next you can see that it's aligned on the center, and then in the next line we show sort of like, hey? 12:51:36 Here it is with ax that set title. Oh, I also forgot that I wanted to increase the font size. 12:51:42 Let me go back and do that. So this was a plot title, and the next example we do with ax dot set title. Okay? So both work 12:51:52 So I wanted to give some title convention. So we started to talk about this a little bit ago. 12:51:57 But now this is the section that I wanted to talk about it. 12:52:01 So I jumped the gun earlier. So here's some title convention. 12:52:04 So your title should be large and easy to read, and in general should be the largest piece of text on your figure. 12:52:10 So it should. It shouldn't be smaller than the access labels or any text on the plot itself, unless the plot itself is consisting of text, in which case, maybe you do want it to be, you know, just like I said in an earlier notebook this the general these are like rules. 12:52:26 Of thumb and every rule of thumb has some exceptions to it. 12:52:29 It just really depends upon the data project or the visualization project. 12:52:34 You're working on so like I was saying before, like academic titles. 12:52:38 There are a little bit more either non existent, or maybe sterile, so they aren't necessarily telling us what to feel about it. 12:52:46 Maybe more. So just saying like, this is what's being plotted. 12:52:50 So maybe like an example might be the impact of sleep on worker productivity could be an example. 12:52:58 Academic title and non academic works. We might have, like a bit more of a headline title, so it's called the headline title, because it's like this is the headline, A Catchy headline of a newspaper article to get people interested in and looking at our visualization and to 12:53:15 Let them know what the visualization takeaway message is. 12:53:19 So in this example we can see as people get more sleep, they tend to also whatever this productivity measure is, they tend to be more productive. 12:53:28 So the productivity scale has higher values. When our workers are getting more sleep. 12:53:33 So maybe our title for a headline title like a Pop Science Magazine, would be more. 12:53:39 Sleep leads to greater worker productivity, and then they read that title, and then it kind of gives them the takeaway message of the plot. 12:53:47 So these are more appropriate, as I said, in popular publications, or industry and government reports. 12:53:56 So something to think about, think about the venue and the audience. 12:54:00 So one of the key things in presentations and in project write ups or posters is, who is your audience? 12:54:07 You should be catering these sorts of things to them. 12:54:10 Okay. So we're going with the example of a Pop science magazine. 12:54:14 So we'll use that more headline catching headline title. 12:54:18 So once we have a title for the entire plot, we might be interested in adding access labels. 12:54:24 So we've seen these before, and different notebooks. 12:54:27 But just as a reminder, we can alter the most recently added axes, objects, accesses, axes, with X label and y, label, plot, dot X label plt, dot y label, and then, if we want a specific axes labels to be set we can use set X label a 12:54:46 X set underscore. X label or ax set underscore wide label. 12:54:53 These again, have the similar arguments of a text. So they've got like font, size, color, etc. 12:54:59 So here we're gonna go ahead and add some with plt, so plot X label, and we can put 12:55:09 Something like sleep, for now and then we'll come back to this, and maybe we'll increase the font size to 16. 12:55:18 And then why label us should be productivity? 12:55:24 Maybe just for now we'll come back to this in a second font size. 12:55:31 Equals. 16. Okay, so here we've set this. Oh, nice. 12:55:43 Yes, sir, what what did I do before? I guess I did 14. 12:55:46 So let's do 14. So if this is 14, the x and access and y-axis labels shouldn't be bigger than that. 12:55:54 So why don't we go ahead and make these like 13? 12:55:56 Okay. So that was plot X label. And then here is set X label set while able where I just left it at X and Y again, we're gonna come back and talk about some conventions for like how you should decide what your labels are. 12:56:12 So it's a very short convention. If you're going to have access labels, they need to be informative. 12:56:18 So your audience actually knows what's being plotted. 12:56:21 And if it's something that has units on it, you should include the unit. 12:56:24 So, for instance, with us in this imaginary setting, it's going to be hours swept for the horizontal axis. 12:56:31 So hours of sleep, and then for the vertical axis, it's going to be units produced. 12:56:38 And then these. I'm going to imagine that these are like tens of units. 12:56:42 Whatever a unit is. So, for instance, when it's actually 2, it would be 20 units for would be 40 units. 12:56:48 So these are going to be productivity measured in units produced. 12:56:53 Okay. So we've got hours of sleep and tens of units produced. 12:56:59 Okay, so the next bit of text would be like legends and text annotations. 12:57:06 So we've already seen an example of how to add a legend to a plot, and how to add text directly to the plot. 12:57:11 So we've previously talked about in that example of sign and cosine. 12:57:17 How. And we can actually pop, pull that up real quick 12:57:23 How, when it's possible, it's preferable to directly label the pieces in your figure as opposed to having a legend. 12:57:33 So this is an example that one that we're working on where it's a little bit more difficult to quickly label the elements directly. 12:57:40 So for simplicity, here we're just going to add a legend, and I believe the upper left hand corner. 12:57:47 And then, you know, maybe if you wanted to, you could work to add like labels directly to the line and an example point to like this is an offication. 12:57:59 This is the regression, fit, etc. But for us we're just going to add a legend just to save time, because we've already discussed the convention of directly labeling versus just having a legend. 12:58:11 And I would say that this is an example where directly labeling the data is a little bit more difficult 12:58:18 So something else you might want to change is the appearance of your tick marks. 12:58:23 So the first thing about that is, maybe you want to change which tick marks are placed where and so the ones that are currently so when we see these longer tick marks the ones that are drowned by default, these are known as the major tick marks so think of this, on your ruler if you have a ruler. 12:58:39 These are like the inch markers would be the major tick marks if we're looking at inches. 12:58:46 Inch markers would be the major tick marks, and then the miners won't be like half an inch quarter of an inch, etc. 12:58:50 If you're more familiar with metric centimeters, I believe, are the major tick marks, and then the miners are like millimeters, etc. 12:58:59 So what we can do is we if we're if we're happy with the most recent axes, like we are in this example, we would use Plt. 12:59:09 X ticks or plot y ticks. If we are working with, we need to select a specific axis object we use Ax set X ticks or ax set Y ticks. 12:59:22 So these functions allow us to input a list or a tuple or a an array, numbers that are going to represent the major tick mark. 12:59:30 So for us, the default in this example, for the horizontal would be 2, 4, 6, 8, 1012, and then, if we wanted to continue with the ax example, we also have to use ax dot set quit labels. 12:59:46 But I believe there's more about that later. So here's an example where I change the X ticks and the Y ticks using ax. 12:59:55 Another argument to these functions that you can use is a minor. 12:59:59 So if you see minor equal to True, it's going to draw like smaller tick marks in between and the way that this works is, if you set minor equal to true, it's going to take your first argument and then use those as the minor tick marks. 13:00:16 So remember, this was our original plot. It had major tick marks at 2, 4, 6, 8, 10, and 12 on the horizontal axis, and it still does. 13:00:24 But now you can faintly see if we zoom in. 13:00:27 There is a smaller tick mark at 1, 3, 7, 9, 11. 13:00:34 Okay. And it knew to do this because I set minor equals to true 13:00:42 So alternatively. If you want to set on some minor tick marks, you can use the function Plt, dot minor ticks underscore on. 13:00:52 So if I do plt dot minor ticks minor ticks underscore on. 13:01:01 Now these have been turned on on automatically, but I had less control, like I did not control. 13:01:09 We can zoom in before I controlled that the minor ticks would be at 1, 3, 5, 1, 3, 5, etc. 13:01:17 But now they're being drawn automatically by Matt Potlib. 13:01:21 So I don't have as much control over like what they actually represent. 13:01:25 I can also change the appearance of these ticks, so I can make them longer or wider, or change the color of them with an art function. 13:01:34 Called Aax tick underscore params. So I can make them longer with the length arguments I can make them wider with the width arguments I can decide whether and which part of the axes I want to draw ticks on, so if I set you know. Bottom. 13:01:51 Equal to true, they'll be drawing the bottom if I set right equal to be true, they'd be drawn on the right. 13:01:58 I can decide whether my ticks are drawn out of the axes or into the axes, with the direction. 13:02:04 So let's look at an example. 13:02:08 So here I'm gonna go axe, tick params. 13:02:13 Then the first thing I'm gonna do is I'm gonna call both meaning, I wanna change both the horizontal and the vertical tick marks then I'm gonna go ahead and why don't I make them longer? 13:02:25 So I'm gonna say, length is equal to let's try 10. 13:02:30 And I'm gonna make them wider. So let's say with is equal to 5. Okay? 13:02:35 And now we can see that I've got a longer tick, and it's much wider. 13:02:39 I could also do right 13:02:44 Is equal to true. Now they're drawn on the right, and I could do left is equal to false. 13:02:54 Hey? Now, the interesting thing! Here is the labels. Don't go with it. 13:02:58 Alright! So that's something to keep in mind. Okay? 13:03:02 So. 13:03:06 I can change the tick labels. So if I'm using Plt, I can just use Plt dot xx plt dot y ticks. 13:03:13 If again, as I said earlier, if you're using the ax version, you have to do, set X tick labels, set y tick labels. 13:03:23 So here's the example. Where I've set my y takes to be 0 through 16, and orders of 2, just like I currently have. 13:03:32 But now instead of let's say, instead of doing tens of units I just want it to be units well, then, I can replace the labels to be 0 2040, etc. 13:03:43 Okay, so that's another way, we can change the labels so that's basically, what else can we do? 13:03:52 So we can change the font size of our ticks as well. 13:03:54 So here we can go back 13:03:58 And change the font size so I could do. Font size equals 20 as an example. 13:04:02 Now, they're much larger. And basically any of the other text arguments we like, we can do that with y-tick labels and ecstick labels. 13:04:13 So that's basically all the appearance changes we can make with the ticks. 13:04:18 Let's talk about grid line. So maybe you like to have a a set of grid lines in the background to help you a more easily compare, like what the actual values of these observations are. 13:04:30 So we can do that with the grid command. So I would call Plt dot grid, and then I can make it so that both the horizontal or vertical axes with the witch command, and then yeah, so when I go ahead, let's just start so actually I just seems 13:04:51 to draw it automatically on both. So that's great. And then 13:04:57 I do, which equals both, and I could also do which equals X, maybe, or 13:05:07 Let's actually go ahead and click the documentation 13:05:12 Okay, axes controls both. So if I do, access equals X. 13:05:20 Now I only have the X-axis axis equals y. Now I have the y-axis, and then I could do which 13:05:31 Is major or minor. So if I do, Major and then I'll turn off. 13:05:38 I could do minor, and now you can see, because I don't have any minor tick. 13:05:43 Marks. None of the grid lines show up. So let's go back to Major 13:05:50 And both. I can also impact the appearance of the grid. 13:05:55 So I can do like say, caller equals red. Now it's a red grid. 13:06:02 But typically what you want to do is you want to set it to be black or gray and have them be very se-through. 13:06:10 So let's say point 2 13:06:13 Or maybe even more, see through like Point One. The whole point of the gridlines, and any data visualization is these extra features are really important to have and help style your plot. 13:06:26 But at the same time the goal of your plot right is to convey what the data is telling us, or tell the story of the data. 13:06:34 So these extra features, we add, should not detract from the data. 13:06:38 So for instance, if I went back to having an alpha of one and make this like neon pink, maybe just pink. 13:06:48 Make this like pink, or that's actually not offensive. 13:06:51 What if I did orange, maybe like. Now my eye is drawn to the grid instead of to the data, and I can even make it worse. 13:07:02 I can change the line width to be like 5 13:07:08 Now the grid is the most important part, and on top of that it's drawn over the data. 13:07:14 So this is like a big no, no. So the the non graphical features that we add to our plot should not detract from our data, it should enhance it. So let's go back 13:07:28 To what we had before, and then actually, let's also add a Z order element, so that it is the one in the way back. 13:07:36 Okay. So I don't want my grid to ever be drawn on top of my data. 13:07:40 It should always be drawn behind my data. Okay? So we already just talked about this. 13:07:46 And I think maybe the last thing we learned to talk about and fix or alter is the axes spine. 13:07:51 So let's go back to our plot. You see this black outline all around the outside of our axes. 13:07:58 Object these 4 lines are known as the spines of the axes, and so we can toggle these on or off, and then change various features of them as well. 13:08:08 So the 4 spines, as I said, correspond to the 4 sides of the rectangle, and then I can get access to each one individually, sort of like a dictionary sequence. 13:08:19 Call ax for your axis. Object, dot spine, and then you input the name that you want. 13:08:24 So if you want to affect the top spine, you would do ax dot spine at top. 13:08:29 So one nice thing we can do is turn things off with the set, visible argument. 13:08:34 So here, let's say I want to turn off the top, and the right, so I would do. 13:08:39 Dot set visible. And then you input false. So this is saying, look at this spine. 13:08:45 The top spine, and I want you to not make it not visible. 13:08:51 Okay. So some people prefer this aesthetic over having the spine around the entirety of the of the axes. 13:08:58 If we wanted to turn off all of the spines@onceyoucanjustcallplt.box on equals false, which is what I've done here. 13:09:09 So now I have no spines. Some people prefer this. 13:09:12 It's just an aesthetic choice, whichever you think you like better, or whoever you're publishing for likes better 13:09:19 You can also set the position. So currently, when I do have a spine, it's drawn at the very bottom and the very far left, or the very far right, the very top of the axes. 13:09:33 We can also change it. So sometimes it's desirable to have an axes drawn at the 0 lines instead, or have a spine drawn at the 0 lines instead. 13:09:42 So here's an example where I can go through and I can center it so that both the bottom and the left axes are in the center our spines are in the center of the axes, so here's an example where now my spines are drawn at the 0. 13:09:56 Values of the axes, which maybe is desirable for this kind of plot 13:10:03 Another thing you can do is currently the axes are drawn for the entire range of the spines. 13:10:10 Sorry I keep confusing them. The spines are drawn for the entire range of the axes. 13:10:15 Object. But you can actually control it so that the screen is drawn from, say, 2 to 8, or 4 to 6. 13:10:21 And so you might be asking yourself, why would I ever want to do that? 13:10:24 Well, there's a very large school of thought and data visualization kind of maybe most famously stated by Edward Tuft, who is a very famous data visualizer, is that the axes should also currently you basically want to maximize the amount of ink think of back in the days where 13:10:42 things were printed with Inc instead of on the computer. The amount of ink for your visualization should be maximized so that the amount of ink used for just the data is the majority of the ink you use so basically their school of thought is that the axes are the spines should be used 13:11:00 to convey information about the data and not just there for decoration. 13:11:05 So one thing they might do is instead of drawing the entirety of the instead of drawing the entirety of the axes or the spines, you'll limit the spine so that they only show the range of the variable, so we would go from the minimum observed hours slept to the maximum 13:11:25 observed hours, slept for the horizontal access and the minimum tens of units produced to the maximum productivity measure. 13:11:33 And so you can do that with the argument set bounds. 13:11:38 So when you do this, you call ax at spines, you specify the spines you want, which is for us the bottom and the left, and then you do set bounds, and then I'm just setting the balance to be the minimum and the maximum okay, so now now my spines. 13:11:56 Convey, like visually, what is the range of the hours of sleep? 13:12:00 So it looks like from 2 to below 12, a little bit above 10. 13:12:05 So somewhere between 11 and 12 and then the tens of units goes from probably a little bit above one all the way up to about maybe around 15. 13:12:15 So this is something that some people like, because it does convey information it's a it's something you have to decide like with your data visualization aesthetic. 13:12:24 If this is what you like, or what the person you're trying the group you're publishing for, if that's what they like. 13:12:30 So there are other ways that you can adjust your spines. 13:12:34 But I'll leave it to you to check out the documentation on spines on your own time. 13:12:38 So I would also say in the wrap-up, we've learned a lot of different ways to customize our plots where it comes from, like adding text to help our audience understand what's being plotted, changing the colors of our ticks and the aesthetics, of our tick marks 13:12:55 Adding, or altering grid lines, as well as changing the axes. 13:13:00 Not the axes, objects, but the axes of the figure as well. 13:13:03 So these are a lot of things to think about. We now have a pretty wide array of tools and our toolbox for making excellent graphics, both by choosing different charts, as well as altering the non graphical elements. 13:13:16 So the non, like the not the scatter points, or the pie chart, or the bars, but the actual bones of the figure itself. 13:13:23 This is actually quite a bit so we should be proud of ourselves and look, we accomplished all of it with just map, plot, web. 13:13:30 So now that we know how to maintain such wonderful plots, it's important to understand. 13:13:33 Well, how do I save these plots? So I don't have to keep making them over and over and over again. 13:13:37 So that's what we'll talk about in the next. 13:13:39 Jupiter Notebook. I hope you enjoyed watching this video.