Adjusting Non-Graphical Elements in seaborn Video Lecture Transcript This transcript was automatically generated by Zoom, so there may be discrepancies between the video and the text. 14:44:02 Hi! Everybody! Welcome back in this video, we continue to learn about Seborn by talking about how you can use Seborn to adjust non graphical elements. 14:44:11 Remember those elements that are not the graph items like scatterplot markers or lines, but rather the axes, the tick marks, and the labels. 14:44:19 So let's go ahead and get started 14:44:24 So the first thing we're gonna learn about are the various styles that Seborn provides. 14:44:30 So these are sort of prepackaged settings for both. 14:44:32 The background, and the grid marks and the internal text options for your seborn plots. 14:44:40 So the first way that we can set these is with the set theme argument. 14:44:45 So again, we're gonna use this penguins data set. 14:44:48 This is what our seborn plot looks like without. 14:44:52 Set theme. But if we call Sns, set theme, it's going to go ahead and change this to Seborn's default. 14:45:02 Default style, which is sort of a style that looks similar. 14:45:06 If you're familiar with R. With R. Is gg, plot, it's got a sort of darker, a light gray background, and then white grid marks, and then no ticket marks. 14:45:15 So this is sort of the nice. I could see why you might prefer people might prefer this to the Matt Plot live style. 14:45:23 So it's got a darker background which can be a little easier on your eyes. 14:45:26 And then the grid marks at the major tick values do help you make comparisons of you know this is about 50 versus. 14:45:36 This is like 49 makes that a little bit easier to do so. 14:45:40 There are 5 preset styles that you can choose from. 14:45:44 The first is, you can just set it to have a white background with no grid or tick marks you can set it to have a white background with a black grid or darker gray or light gray grid. You can set it to have a white background with no grid but tick marks you 14:46:00 Can said to have a dark background, with no grid lines or no tick marks, and then you can also set to have a dark background with grid markers. 14:46:09 This is dark. Grid is what we're currently seeing up above. 14:46:14 So the way to do this is, you can add the style argument. 14:46:18 So, for instance, I can change this to the white grid, and we can see what that looks like, or I could change it to ticks which I believe was what happened was this original chart type before we started calling. 14:46:32 Set theme. There are some other arguments for set theme that we won't talk about. 14:46:38 But you can always check the documentation to see what those arguments are and what they do. 14:46:44 So we can also point out that once we set the theme or the style using, set theme. 14:46:50 That stays, that internally becomes the defaults. Whenever you call a plot. 14:46:55 So now that I've called my plot to have the ticks, style every time I make a new plot, even if it's with Matt. 14:47:03 Plot lib, or if it's a seborn, it's gonna have that style. 14:47:08 And then we can sort of see like if I come back up here. And then I set the theme maybe to style equals dark 14:47:19 Now they're both gonna be dark. So whatever style you set with set theme maintains stays the style of your figure and tell you call a command to change the style again. 14:47:33 So, as I said, there are other arguments to set theme like context, palette, font, font scale. 14:47:40 I'm not going to directly discuss these, but if you read through the documentation, you can see what they do. 14:47:44 So, if you wanna do like what we just did where we set the style using, set theme, that's all you want to do. 14:47:53 Typically, it's more common to call set style. So we can do this. 14:47:59 Sns dot set style, and then you don't have to write out style. 14:48:03 Equals. You could just put in, for instance, what we want to do. 14:48:08 According to my comments, is white grid. Okay. 14:48:13 So now, once again, this is gonna be the default style. 14:48:17 So, for instance, let's we can copy and paste this, and if I get rid of this set style argument, it's still going to be white grid. 14:48:26 So this will stay whenever you set the style using. Set theme or a set style, it stays as the default style until you make another call to change the style. 14:48:37 So we can get more. Find grid, fine grained axes, control. 14:48:43 So about the style of our elements by seeing what the axes looks like using axes, underscore style, so this isn't going to be setting anything. 14:48:55 But it's gonna show us all of the aesthetic options of our axes. 14:49:00 Object in Matt. Plot live. So when I call axes that set or axi the sns dot axes underscore style what gets returned is addictionary and within that dictionary it tells me all of the aesthetic options that my axes objects currently have for instance, the face. 14:49:20 Color is white, the edge color is point 8, which is like, I think, an alpha of point 8. 14:49:26 I have a grid, etc. 14:49:31 Okay, so now, in addition to being able to call this the default strings, like white grid, dark grid, etc. 14:49:41 I can any of these arguments I can change and enter as a dictionary is an Rc. 14:49:46 Argument. So an Rc. Argument takes a dictionary, where I can go through, and I can say, All right. 14:49:54 I want my axes dot face color to be in this example. 14:49:59 I wanted to have a black background, and I want my grid to be white. 14:50:03 So we can go up and see, okay, axis. Dot grid is true, and I can see maybe axis dot edge color. 14:50:13 I'm gonna set that to black. So Axi or white axes dot edge color is white. 14:50:22 So now I've got a black background with a white grid. 14:50:27 Okay. And now I'm gonna just go back to regular white grid. 14:50:31 This was just to demonstrate that any of these options or keys that you see in the Axes Style dictionary. 14:50:39 These can be set by passing an Rc. Command, which contains a dictionary with what you would like. 14:50:46 It's a set style 14:50:50 So another thing we can do is just like with Matt plot lib. 14:50:55 We can control what spines. So remember, spines are the lines all along the out of your plotting area. 14:51:07 So we can control which of these lines are present with the D spine function. 14:51:12 So if I call sns dot d spine, with no additional arguments, it will remove the top and the right spine. 14:51:22 So this should look familiar as sort of like the results of a figure level function call. 14:51:27 But in addition, I should point out, this is not a permanent change, so anytime you make a new figure with a new axis. 14:51:35 It's gonna have. Whatever the default for that function is so if it's an axis level figure, it's going to have whatever this. 14:51:46 The default style is so if I want to get rid of a spine without changing the style of my plot, I'm gonna have to call D spine. 14:51:57 So d spine also has an argument arguments that allow you to specify each individual spine. 14:52:04 So, for instance, I can turn off the the left 14:52:08 And the bottom spines while keeping on the right and the top spine. 14:52:17 So how do you do this? You just do left equals false bottom equals false, so that turns those off. 14:52:24 And then right equals true and top equals true keeps those on. 14:52:31 Oh, I flipped it. Sorry about that, so you should. The ones you want to turn off you should turn equal to true why do you want to turn it to? True? 14:52:38 Because you're. It's true that you want to desine them. 14:52:43 I've just made a mistake. So let's flip those. 14:52:45 Now we'll have a spine on the right in the top, but not on the left, in the bottom, and again you can just like we mentioned right. 14:52:54 Remember what we talked about set style with this Rc. Argument. 14:52:59 Notice that in the Axi style there's an axe spine left axe, spine's bottom, and so forth. 14:53:08 We could also turn off the spines with an a pass of false to all of these. 14:53:14 When we put in an Rc. Argument 14:53:17 Okay. 14:53:22 We can control. I'm just reading what I wrote, how we can control a couple of other things. 14:53:27 So, for instance, we can trim the axes, which means that it will only be drawn. 14:53:33 So in this example, the minimum tick mark on the horizontal is 35, and the maximum is 60. 14:53:38 So if I set trim equals to true, it will only draw it from 35 to 60, and I can also use the audio and set it equal to an an integer, and it will move the spines either away or towards the same distance, so let's demonstrate offset. 14:53:57 First so here's what the regular plot looks like. 14:54:00 Here's what it looks like with a call to d spine. 14:54:03 And here's what it looks like if I set offset equal to 10. 14:54:07 So I've moved the spines away. I can also set it equal to like 50. 14:54:11 And now I'll be really far away, right? I could maybe we could even try and see what happens if I set to negative 10. 14:54:18 Now it moves it inwards, and maybe it's not obvious so let's do negative 50. 14:54:23 Okay, so that's the offset. 14:54:28 And when I set trim equal to True, you can see now that we're only going from 35 to 60. 14:54:36 So trim equals to true, goes from the smallest major tick marker to the largest major tick marker. 14:54:44 This is different than what we did with Matt. Plot. 14:54:47 Lib. Where we went from the minimum data value to the maximum data value. 14:54:53 That is, that we can still do with Matt. Plot lib. 14:54:56 I don't know that we can do it with D spine, but possibly we can, but I would probably just just use the mat plot. 14:55:05 Live functionality. We learned in earlier notebooks 14:55:09 Okay. 14:55:11 So let's talk about some arguments to figure level functions that we haven't talked about yet. 14:55:17 So remember with axes, level functions, we can just control the size of a figure with a fig size argument. 14:55:25 When we create the figure. But in figure-level functions the figures created for us. 14:55:30 So how do we control the size? Well, here's the default. 14:55:35 So what it looks like. So you can control the size with something called the height in the aspect arguments. 14:55:42 So the height argument gives, the height of the plot. 14:55:46 So let's say it's 5. And then the aspect argument gives a multiplier. 14:55:51 So the aspect sets the high, the width of the figure to be aspect times, the height. 14:55:58 So if I set my aspect to be one aspect equal to one, this is what I get. 14:56:07 And now notice what happens when I set aspect equal to 2. 14:56:09 It is now 2 times as wide as it is tall. So that's how you can control figure size with a figure level function. 14:56:19 So what else can we do? We can change access labels. 14:56:24 So how can we do that with a figure level function? We already know how to do it with an axis level. 14:56:30 We just use ax set, X label or Plt. Dot X label, for instance, so axes, labels. 14:56:37 Remember we talked about before in a previous notebook. We talked about that you can access the individual axes with like storage. 14:56:48 Your figure level, function into a variable which will return the facet grid. 14:56:54 Then you can access the axes by doing the variable G, which is what we stored it in dot ax, and now we have the individual axes. 14:57:05 So now that we have that, we can use this to set the X label with. 14:57:09 Ax set X label to be bill length. 14:57:15 And we can also do ax dot set y label to be Bill, or what was it? 14:57:23 Body, mass, body, mass, body, mass, grams, and we can. 14:57:30 Let's choose like font. Size equals 14 14:57:35 Similarly, we can change with set axes, labels. 14:57:41 This is when we do this, we don't have to call the individual axes themselves, so we can do set axes, labels which will take in a list or we'll take in the horizontal label first, and then the vertical label second so we call G dot set access label so this is 14:58:01 A function of the facet grid that doesn't require us to access the individual axes, objects on their own 14:58:10 So finally, I've said it a couple of times as we've gone through the notebook, but remember, Seborn is built upon Matt Plot lib, which means all the stuff that's getting plotted is actually just a map plot. 14:58:21 Lib. Object behind the scenes. So this means that almost any of the aesthetic adjustments be made with Matt Plot lib can be used here. 14:58:30 So remember, that's really all we did here was once we had the map plot lib axes, object with G dot X. 14:58:38 We use the mat plot, live function, set y label. Okay? 14:58:42 So all of those functions still work for us before we close out this notebook, I want to make a note on the Seborn defaults. 14:58:51 So a lot of times. Seborn has its own preferred set of defaults for various graph objects, and non graphical objects. 14:59:00 But a lot of times those defaults don't align with what the data visualization standards are for applyots that you might want to display to an audience. 14:59:09 So there's sort of 2 noteworthy examples that might be worth it talking about. 14:59:14 So text labels, so remember by defaults, text labels will use the text from either the column name. 14:59:22 So here's an example, Bill, under score length, underscore, or there will use for legends and other labels. 14:59:30 They'll use the values from the data frame values. So for species, it's not so bad because these are like the human readable names that you would see at a zoo or at an aquarium. 14:59:45 So that's for this particular example. It's not bad, but sometimes it might be something like P. E. N. G. Underscore a underscore E, or something like that. 14:59:54 So in situations like that, we want to change those labels to be. 14:59:58 What is the more appropriate human readable label so that way, it's easier for our audience to understand another thing that Seaborn does a lot is they will unnecessarily add colors to our plots and a big example of that are categorical plots. 15:00:15 So let's have in the example of doing the box in plot for the bill lengths by species. 15:00:21 So here are the Seborn defaults, so species isn't so bad, but it's usually proper form to capitalize your access labels, and Bill Length is a little bit worse, because it's got the underscores, and they're all lowercase further, we've got different. 15:00:39 Colors for each of our box plots, which we don't need. 15:00:43 These different colors to specify the species of penguin, because the X-axis already provides that information for us by labeling each of the box plots on its own. 15:00:57 So we can go through and change these defaults. So in the box in plot, we can set the color so that all of them are light blue instead of colored by species which is repetitive information, and we can change the access labels as well to be more human readable and now that we've 15:01:17 All altered this plot. This matches a little bit better with some of the data visually, the data visualization standards for plots that you're planning to present to an audience 15:01:28 So these are things to keep in mind when working with Seborn is that some of the preferences of the Creator don't always align are not necessarily preferences of the Creator. 15:01:39 But you know what is what is Seaborn going to do when you give it the column name? 15:01:43 If you don't also tell it what the display name should be. 15:01:46 So you know some of the defaults that has to do both as preferences of the Creator, but also as like the facts of this is a programming language that can't read your mind and what you want it to be. 15:02:01 These are not what you want to keep when you're presenting plots to an outside audience that aren't working internally with you, and even internal. 15:02:08 You know internally, we should put some thought and care into these these little tiny aspects of our plots that can take it from an okay plot to an even better plot. 15:02:16 So we now have a really good understanding of through all of these notebooks. 15:02:22 How we can use Seborne to create a wide variety of plots, and after this notebook we now see how to alter a number of non-graphical elements of our seborn plots. 15:02:31 Have we combined this with all the knowledge we've already built up about Matt. 15:02:35 Plot. Lib. There's really a lot of tools in our python data of data, visualization tool chess that we have moving forward. 15:02:44 So in the next notebook, which is our last seborn notebook, we're gonna give a wrap up and give you some steps of what you can do next. 15:02:51 If you'd like to continue to learn, Seborn, I hope you enjoyed learning about adjusting the non graphical elements. 15:02:58 I enjoyed teaching them to you, and I hope you have a great rest of your day.