-: Hi Guys, and welcome back.
In this video, we're going to learn
how to produce an image as output
instead of showing up the window
that allows us to interact with the plot dynamically.
So, let's get started.
Of course, creating an image is going to allow us
to save things that the user wants to keep for later.
So, it's great,
but there's a few things that we have to know
about saving images,
and a few configuration options that are available to us.
And o, let's get started by going down here,
and instead of plt dot show,
we're gonna do figure dot save fig graph dot png.
Notice that I'm no longer using plt
because the plt dot show method
is used for creating the window.
It essentially handles the event loop
that is necessary in a graphical application
so that you can drag your mouse
and interact with buttons, and so on.
But when we're not creating one of those windows,
we can use the figure directly.
So, here we're gonna save this figure specifically
as a file.
And matplotlib is smart enough to know that
because you've put dot png in there,
you wanna create a PNG file,
and it will take care of everything for you.
So, let's go ahead and run this.
And you can see that nothing seems to happen,
but of course, we've got our file now in here.
We can open it, and this is the output produced.
So, you've got the white background, you've got your graph,
the subplot adjustment
that we defined at the bottom equals zero point 35.
You've got your legend, you know,
everything looks good in here.
Let's talk about some things that you can adjust.
The first thing is, of course,
if you go ahead and remove this subplot adjustments,
I'm gonna set it to the default of 0.1,
and you run this again, you create a new image,
it overwrites the old one.
And what you've got here is, the labels are cut off.
So, this is not great.
But you can see that the figure size
and the subplot adjustment
is used whenever you are saving the figure.
But we can do something in addition to this,
which allows us to recalculate the bounding box
of the figure before exporting the file.
So, we're going to do bbox_inches equal tight.
I hate this, because bbox_inches,
should surely be a number, not a string.
And, but when you pass in a string that equals tight,
this is going to recalculate the bounding box
and make sure that there's enough room
for everything in the figure to be saved to the file.
That is what the tight bounding box method uses.
So, now we're going to run this again.
And you'll see that when we do that,
our graph now is as close as possible to the edges.
There's a very small padding there,
that wasn't there before.
In fact, I'm gonna get rid of this,
and save graph two in there,
just to show you the difference between the two.
There we go.
I'll bring it back.
This is graph one with the tight bounding box.
This is graph two without a tight bounding box,
and the incorrect subplot adjustment.
So, you can see that in here,
the subplot adjustment didn't matter as much,
and because we still have enough room for our contents.
However, let me bring back app dot py,
and I'm gonna reset the subplot adjustment to 0.35.
I'm gonna create graph three.
I promise this is going to be the last number to craft
that we're gonna create before it gets too confusing.
What I'm gonna show you right now with tight set,
and a subplot is just 0.1.
So, that's what we had earlier.
It looks like this.
Now, if we have just this subplot, it looks like that.
So, you can see that we've got the exact same amount of room
at the bottom in terms of the size of the labels.
But clearly, the graph was bigger than before.
That's because when we used subplot adjustment of 0.1,
that means that the figure was six by six
or 600 pixels by 600 pixels,
and 10% of it was reserved for the bottom.
So really, the graph size was 540 in terms of height,
then when we exported the image
and we set bbox to tight,
essentially that added space.
It added enough space to display the entire content,
whereas in graph three we did that before exporting.
We limited the height of the graph,
we added 35% of the height into the bottom labels.
And then when we set to the tight method of exporting,
it didn't have to add as much information
because it was already almost fully visible.
So, I hope that makes a little bit of sense.
That's something that you have to take into account.
If you are going to produce images,
you normally don't have to adjust your subplots that much
because you can use the tight bounding box setting
to give you enough room.
Alright, when you use bbox_inches equal tight,
you can also add some extra padding,
because right now you can see that the graph
is very close to the borders, maybe you don't like that.
So, what you can do is you can say,
pad_inches, let's say two.
And now, when we produce this,
I'm going to go back to graph dot png,
by the way, then you will get your graph with a bit of room.
A lot of room, in this case, around the size.
You're gonna adjust this to whatever you want.
The default is 0.1 but you can give it a bit more room
if you, for example, wanted to print this out,
or you wanted to write some notes near it
or something like that, you can do it.
And so, that's something you can use
if bbox_inches is tight.
If it's not tight,
then you're using the bounding box of the figure,
and this will not take effect.
So, that's something to take into account.
I'm going to remove it for now
because we don't really need it here.
And I'm going to set the support adjustment to 0.35,
because I think that's sort of something
that we need if we ever want to show the figure ourselves.
And, but let's take a look
at a couple other configuration options that we can use
when saving a figure,
although we can use them also in other places as well.
And that is the face colour and the edge colour.
You can set the face colour to any colour you want.
For example, face colour equal,
let's say 5c44fd, for example.
And now when we run this,
we get a beautiful blue in the background there,
that totally makes the text super readable.
And yeah, you can use that
if you want to set a background colour for your graph.
Notice that the axes have their own face colour,
but you can set yourself separately.
This is the face colour of the background image.
So, that sets that.
You can also use edge colour
if you want to set the colour of the border,
I'm gonna run this and open graph.png,
and you'll see that, you can't see that blue anywhere.
And that's because by default, figures don't have an edge.
So, here we're setting the edge colour, but there is no edge.
So, there's nothing to be seen.
We have to manually change that when we create the figure.
We can see line width, equal five, for example.
And now when we run this and recreate our image,
we can open it and you can see the border there.
Alright, so I just wanted to teach you a little bit
about these export options.
I think that bbox_inches equal tight is really cool.
It means that you don't really have to worry
about the subplot adjustment whether your labels
are exactly gonna fit, you know,
whether your poll title is going to be super long or not.
After all, when you're dealing with dynamic data,
you may not necessarily know exactly
how much space you need in the subplots,
This is particularly useful to give defaults to the user
when you're doing plt dot show.
Because they can then change that if they want.
So, just as an example there,
I'll bring back plt dot show, and run this.
And remember that here we've got point three five
in the adjustment, but you can change that
if it turns out it's too much or too little,
you can just go ahead and add more room or remove room
if you don't need it as the user.
But when you're creating an image,
you of course don't have that capability,
you only have the image that is produced.
That's why I like this.
It allows you to give the user everything
rather than just relying on them
to change the settings as they need.
So, thank you guys for joining me in this video.
Thank you for watching, and I'll see you in the next one.