Saving a Figure to File Video Lecture Transcript This transcript was automatically generated by Zoom, so there may be discrepancies between the video and the text. 13:18:33 Hi! Everybody! Welcome back in this video. We're gonna learn how we can save all the wonderful Matt plot. 13:18:39 Lib plots that we can now make to file. So let me go ahead and get to our Jupiter. 13:18:43 Notebook, and we can get started so we're gonna learn about the save fig function, which will allow us to save our figure to a variety, a variety of different file types. 13:18:55 We'll also talk about various types of layout features which you might want to use to save those figures and then we'll mention a couple of different features. 13:19:04 We might want to toggle for our saved feature figures. 13:19:07 So let's say that we're really happy with the following figure that we made at the end of our last our last notebook, on adjusting non graphic plot elements. 13:19:20 So our regression on a example. Looking at a study of how hours of sleep for our workers impacted their ability to produce whatever the units they were producing. 13:19:31 So running the plt dot save fig command, would allow us to save this figure to file, and if we call Plt save a fig immediately after we're done creating the figure we want, then we can. 13:19:47 And but before plt show, then it's saved to file. 13:19:51 So all we have to do is put in our our file name as well as the format we want. 13:19:56 So our good, our default that we're gonna go with is the Png format, and we'll talk more about different formats. 13:20:03 You can, or a list out all the different formats you can save a figure, as in the next bit of the notebook, so let's go ahead, and we're gonna remake this figure. 13:20:13 And then, right before I call plt dot show I'm going to call plt dot save fig I'm gonna put in the name. 13:20:21 So for me, it's gonna be sleep study, dot Png, and then I'm gonna make sure my, so for Png, it's actually the default. 13:20:29 So you don't have to do this, but just to show you how it works, it's going to be format equals. 13:20:34 Png, okay, so now, when I run this code, the figure will get saved. 13:20:41 As the file sleep, study, dot png, which is a png for okay? 13:20:45 So it's still displays because I called plot show. 13:20:50 But now, if you go back here and scroll down, you should be able to see sleep, study, dot Png changed a few seconds ago, so now we can see this is what our plot looks like alright. 13:21:05 So it's saved as a file. So there's a variety of different file types that you can save. 13:21:12 Save your figure as, and I've instead of going through each and every one, I'm gonna leave it to you to go through and see what all these mean. 13:21:19 But here's what all the different file types are. So Png and Jpeg are used quite a bit. 13:21:27 Other file types that you might encounter and have to use our Eps, or then Svg. 13:21:35 Or Pdf, so sometimes you might use these. So Eps, pdf, and Svg. 13:21:42 Allow you to get a higher resolution image. And sometimes that's desirable from whoever like. 13:21:47 Let's say you're publishing in an academic journal. 13:21:49 They might require that you submit photos in that format because it's a higher resolution that then they can compress into whatever lower resolution they want. 13:21:58 So, sometimes these are what you're gonna want to use for static files. 13:22:02 But a good a default, for what you're doing is probably just Png if you're just going to save it internally and look at it again later again, the file format you use is just going to depend upon what like where are you trying to display this image and what kind of memory limitations are 13:22:19 there, on where you're trying to display it. So let's go back to our figure, and you might notice that there's some white space on both the left and top, and maybe it's desirable like you're going to put this somewhere where you want the figure to literally go to the edges of 13:22:34 the figure object. So one way to do this automatically as with a function called tight layout. 13:22:41 So when you call plt tight layout, it's gonna make it so that the white space basically like there is no white space on the exterior. 13:22:50 It's the figure object itself is going to hug the what you've pulled and put on there as closely as it can. 13:22:58 So in order to run this just right before you call, save fig. 13:23:01 You call plt dot tight, underscore layout, and then it's going to adjust the white space automatically. 13:23:09 For you, and we're going to save it and sleep study underscore tight, underscore layout. 13:23:15 Okay. So you might be able to tell there's a slight difference within your Jupiter. 13:23:19 Notebook. But let's go ahead and just open up the file 13:23:25 And so now we can compare the 2. You can see how the this one scales much more closely to the edge of the figure. 13:23:33 Then the original one. Okay. 13:23:37 So again, it just depends upon what your use case is what you're trying to do. 13:23:42 Sometimes you might want to do this, and tight layout is a quick and easy way to get this sort of thing done other times. 13:23:50 Tight layout might give you a somewhat distorted image, and like, cut off your unit, your lit, your axis, labels so you might want to fall back on what we learned in a previous. 13:23:59 Our subplots. Notebook where you just use subplots suggest so subplots adjust, remember, allows you to specify the spacing. 13:24:08 The white space on the left, to the top, the bottom, the right, and if you have various subplots, various axes in one, the white space between columns and rows of axes, so all you have to do is just like before we call plot subplots adjust we adjusted. 13:24:27 How we might want and then I'll save it to a different one. 13:24:30 So we can kind of compare and contrast the 3. 13:24:34 Okay, so let me, I'm just waiting for the little zoom. 13:24:37 Popup thing to go away so I can get back to my files. 13:24:40 There we go. And so now, if I go back here 13:24:45 We can compare. So this is the one that was produced with subplots. 13:24:49 Suggest, and you can kind of tell how it's more tightly hugging the better, or going towards the boundaries of the figure than the original. 13:24:58 But not as stretched out as the tight layout. 13:25:02 And that's because I controlled it with the subplots. 13:25:06 Suggest, okay, so one thing you might also be interested in is like, maybe I want to take this figure and then place it onto place it onto a Powerpoint or a poster. 13:25:18 But I don't want the white background of the figure to interfere with the layout of the poster. 13:25:25 So one way to make your figure transparent, so sort of see through. 13:25:28 And the only thing that's on there are these elements like the the anything that's not white will essentially or anything that's not white will be there. 13:25:37 And then anything that's white will be turned into a see-through image. 13:25:41 So you can do that by setting when you call, save figure. 13:25:45 You can also call transparent equals true. So again, you're not gonna see any difference here. 13:25:54 And you're even not gonna see a difference here. But if you actually open the file, open the file on your on your computer, this will now be, see through kind of like a a Png image of a logo or something. 13:26:13 So sometimes that's desirable. There are other features and other ways to edit your figure. 13:26:20 So maybe just the white space, or outside of the axes is see through. 13:26:24 But the inside plot of it is still white and solid. 13:26:29 I'll leave it to you to explore on your own, both by looking at documentation and performing web searches as needed. 13:26:34 But now you know how to make a lot of wonderful figures, and you know how to save them. 13:26:38 So you don't have to keep remaking them every time they want to be looked at, and you can look at them outside of a Jupiter notebook environment. 13:26:45 So that's it for this notebook, and it's it for all of our mat plot, lib coverage and the next notebook will give you some next steps if you would like to continue to learn more about Matt. Plot.