WEBVTT

1
00:00:00.090 --> 00:00:01.440
<v ->Hi Guys, and welcome back.</v>

2
00:00:01.440 --> 00:00:02.880
In this video, we're going to learn

3
00:00:02.880 --> 00:00:04.960
how to produce an image as output

4
00:00:04.960 --> 00:00:07.470
instead of showing up the window

5
00:00:07.470 --> 00:00:10.280
that allows us to interact with the plot dynamically.

6
00:00:10.280 --> 00:00:11.960
So, let's get started.

7
00:00:11.960 --> 00:00:14.010
Of course, creating an image is going to allow us

8
00:00:14.010 --> 00:00:17.250
to save things that the user wants to keep for later.

9
00:00:17.250 --> 00:00:18.210
So, it's great,

10
00:00:18.210 --> 00:00:19.820
but there's a few things that we have to know

11
00:00:19.820 --> 00:00:21.120
about saving images,

12
00:00:21.120 --> 00:00:24.840
and a few configuration options that are available to us.

13
00:00:24.840 --> 00:00:26.740
And o, let's get started by going down here,

14
00:00:26.740 --> 00:00:28.580
and instead of plt dot show,

15
00:00:28.580 --> 00:00:33.130
we're gonna do figure dot save fig graph dot png.

16
00:00:33.130 --> 00:00:35.510
Notice that I'm no longer using plt

17
00:00:35.510 --> 00:00:38.320
because the plt dot show method

18
00:00:38.320 --> 00:00:40.190
is used for creating the window.

19
00:00:40.190 --> 00:00:43.230
It essentially handles the event loop

20
00:00:43.230 --> 00:00:45.670
that is necessary in a graphical application

21
00:00:45.670 --> 00:00:47.470
so that you can drag your mouse

22
00:00:47.470 --> 00:00:49.300
and interact with buttons, and so on.

23
00:00:49.300 --> 00:00:51.900
But when we're not creating one of those windows,

24
00:00:51.900 --> 00:00:53.800
we can use the figure directly.

25
00:00:53.800 --> 00:00:56.380
So, here we're gonna save this figure specifically

26
00:00:56.380 --> 00:00:57.500
as a file.

27
00:00:57.500 --> 00:00:59.980
And matplotlib is smart enough to know that

28
00:00:59.980 --> 00:01:01.840
because you've put dot png in there,

29
00:01:01.840 --> 00:01:03.410
you wanna create a PNG file,

30
00:01:03.410 --> 00:01:05.430
and it will take care of everything for you.

31
00:01:05.430 --> 00:01:07.550
So, let's go ahead and run this.

32
00:01:07.550 --> 00:01:09.400
And you can see that nothing seems to happen,

33
00:01:09.400 --> 00:01:12.100
but of course, we've got our file now in here.

34
00:01:12.100 --> 00:01:15.310
We can open it, and this is the output produced.

35
00:01:15.310 --> 00:01:18.210
So, you've got the white background, you've got your graph,

36
00:01:18.210 --> 00:01:19.980
the subplot adjustment

37
00:01:19.980 --> 00:01:23.150
that we defined at the bottom equals zero point 35.

38
00:01:23.150 --> 00:01:24.330
You've got your legend, you know,

39
00:01:24.330 --> 00:01:25.580
everything looks good in here.

40
00:01:25.580 --> 00:01:28.170
Let's talk about some things that you can adjust.

41
00:01:28.170 --> 00:01:29.210
The first thing is, of course,

42
00:01:29.210 --> 00:01:32.330
if you go ahead and remove this subplot adjustments,

43
00:01:32.330 --> 00:01:34.547
I'm gonna set it to the default of 0.1,

44
00:01:34.547 --> 00:01:36.550
and you run this again, you create a new image,

45
00:01:36.550 --> 00:01:38.020
it overwrites the old one.

46
00:01:38.020 --> 00:01:40.830
And what you've got here is, the labels are cut off.

47
00:01:40.830 --> 00:01:41.900
So, this is not great.

48
00:01:41.900 --> 00:01:45.130
But you can see that the figure size

49
00:01:45.130 --> 00:01:47.050
and the subplot adjustment

50
00:01:47.050 --> 00:01:50.120
is used whenever you are saving the figure.

51
00:01:50.120 --> 00:01:53.580
But we can do something in addition to this,

52
00:01:53.580 --> 00:01:57.080
which allows us to recalculate the bounding box

53
00:01:57.080 --> 00:01:59.880
of the figure before exporting the file.

54
00:01:59.880 --> 00:02:04.790
So, we're going to do bbox_inches equal tight.

55
00:02:04.790 --> 00:02:07.410
I hate this, because bbox_inches,

56
00:02:07.410 --> 00:02:10.550
should surely be a number, not a string.

57
00:02:10.550 --> 00:02:14.260
And, but when you pass in a string that equals tight,

58
00:02:14.260 --> 00:02:16.580
this is going to recalculate the bounding box

59
00:02:16.580 --> 00:02:17.960
and make sure that there's enough room

60
00:02:17.960 --> 00:02:21.230
for everything in the figure to be saved to the file.

61
00:02:21.230 --> 00:02:24.640
That is what the tight bounding box method uses.

62
00:02:24.640 --> 00:02:26.560
So, now we're going to run this again.

63
00:02:26.560 --> 00:02:27.990
And you'll see that when we do that,

64
00:02:27.990 --> 00:02:31.030
our graph now is as close as possible to the edges.

65
00:02:31.030 --> 00:02:32.560
There's a very small padding there,

66
00:02:32.560 --> 00:02:34.220
that wasn't there before.

67
00:02:34.220 --> 00:02:35.860
In fact, I'm gonna get rid of this,

68
00:02:35.860 --> 00:02:37.320
and save graph two in there,

69
00:02:37.320 --> 00:02:39.450
just to show you the difference between the two.

70
00:02:39.450 --> 00:02:40.283
There we go.

71
00:02:40.283 --> 00:02:41.116
I'll bring it back.

72
00:02:41.116 --> 00:02:43.479
This is graph one with the tight bounding box.

73
00:02:43.479 --> 00:02:46.400
This is graph two without a tight bounding box,

74
00:02:46.400 --> 00:02:50.400
and the incorrect subplot adjustment.

75
00:02:50.400 --> 00:02:51.410
So, you can see that in here,

76
00:02:51.410 --> 00:02:53.960
the subplot adjustment didn't matter as much,

77
00:02:53.960 --> 00:02:57.650
and because we still have enough room for our contents.

78
00:02:57.650 --> 00:02:59.840
However, let me bring back app dot py,

79
00:02:59.840 --> 00:03:03.540
and I'm gonna reset the subplot adjustment to 0.35.

80
00:03:03.540 --> 00:03:05.320
I'm gonna create graph three.

81
00:03:05.320 --> 00:03:07.660
I promise this is going to be the last number to craft

82
00:03:07.660 --> 00:03:09.570
that we're gonna create before it gets too confusing.

83
00:03:09.570 --> 00:03:11.987
What I'm gonna show you right now with tight set,

84
00:03:11.987 --> 00:03:14.470
and a subplot is just 0.1.

85
00:03:14.470 --> 00:03:16.090
So, that's what we had earlier.

86
00:03:16.090 --> 00:03:17.138
It looks like this.

87
00:03:17.138 --> 00:03:21.190
Now, if we have just this subplot, it looks like that.

88
00:03:21.190 --> 00:03:23.420
So, you can see that we've got the exact same amount of room

89
00:03:23.420 --> 00:03:27.120
at the bottom in terms of the size of the labels.

90
00:03:27.120 --> 00:03:30.940
But clearly, the graph was bigger than before.

91
00:03:30.940 --> 00:03:35.490
That's because when we used subplot adjustment of 0.1,

92
00:03:35.490 --> 00:03:38.530
that means that the figure was six by six

93
00:03:38.530 --> 00:03:41.480
or 600 pixels by 600 pixels,

94
00:03:41.480 --> 00:03:44.970
and 10% of it was reserved for the bottom.

95
00:03:44.970 --> 00:03:49.690
So really, the graph size was 540 in terms of height,

96
00:03:49.690 --> 00:03:51.740
then when we exported the image

97
00:03:51.740 --> 00:03:54.150
and we set bbox to tight,

98
00:03:54.150 --> 00:03:55.940
essentially that added space.

99
00:03:55.940 --> 00:03:58.730
It added enough space to display the entire content,

100
00:03:58.730 --> 00:04:02.730
whereas in graph three we did that before exporting.

101
00:04:02.730 --> 00:04:04.300
We limited the height of the graph,

102
00:04:04.300 --> 00:04:09.066
we added 35% of the height into the bottom labels.

103
00:04:09.066 --> 00:04:13.210
And then when we set to the tight method of exporting,

104
00:04:13.210 --> 00:04:15.030
it didn't have to add as much information

105
00:04:15.030 --> 00:04:17.900
because it was already almost fully visible.

106
00:04:17.900 --> 00:04:19.640
So, I hope that makes a little bit of sense.

107
00:04:19.640 --> 00:04:21.810
That's something that you have to take into account.

108
00:04:21.810 --> 00:04:23.716
If you are going to produce images,

109
00:04:23.716 --> 00:04:26.960
you normally don't have to adjust your subplots that much

110
00:04:26.960 --> 00:04:30.010
because you can use the tight bounding box setting

111
00:04:30.010 --> 00:04:32.100
to give you enough room.

112
00:04:32.100 --> 00:04:35.226
Alright, when you use bbox_inches equal tight,

113
00:04:35.226 --> 00:04:37.840
you can also add some extra padding,

114
00:04:37.840 --> 00:04:39.280
because right now you can see that the graph

115
00:04:39.280 --> 00:04:41.720
is very close to the borders, maybe you don't like that.

116
00:04:41.720 --> 00:04:43.886
So, what you can do is you can say,

117
00:04:43.886 --> 00:04:46.930
pad_inches, let's say two.

118
00:04:46.930 --> 00:04:48.490
And now, when we produce this,

119
00:04:48.490 --> 00:04:50.650
I'm going to go back to graph dot png,

120
00:04:50.650 --> 00:04:54.680
by the way, then you will get your graph with a bit of room.

121
00:04:54.680 --> 00:04:56.370
A lot of room, in this case, around the size.

122
00:04:56.370 --> 00:04:57.970
You're gonna adjust this to whatever you want.

123
00:04:57.970 --> 00:05:00.810
The default is 0.1 but you can give it a bit more room

124
00:05:00.810 --> 00:05:02.500
if you, for example, wanted to print this out,

125
00:05:02.500 --> 00:05:03.820
or you wanted to write some notes near it

126
00:05:03.820 --> 00:05:06.100
or something like that, you can do it.

127
00:05:06.100 --> 00:05:07.528
And so, that's something you can use

128
00:05:07.528 --> 00:05:10.370
if bbox_inches is tight.

129
00:05:10.370 --> 00:05:11.520
If it's not tight,

130
00:05:11.520 --> 00:05:13.820
then you're using the bounding box of the figure,

131
00:05:13.820 --> 00:05:15.590
and this will not take effect.

132
00:05:15.590 --> 00:05:17.700
So, that's something to take into account.

133
00:05:17.700 --> 00:05:18.830
I'm going to remove it for now

134
00:05:18.830 --> 00:05:20.590
because we don't really need it here.

135
00:05:20.590 --> 00:05:23.210
And I'm going to set the support adjustment to 0.35,

136
00:05:23.210 --> 00:05:24.830
because I think that's sort of something

137
00:05:24.830 --> 00:05:28.010
that we need if we ever want to show the figure ourselves.

138
00:05:28.010 --> 00:05:29.050
And, but let's take a look

139
00:05:29.050 --> 00:05:31.550
at a couple other configuration options that we can use

140
00:05:31.550 --> 00:05:32.720
when saving a figure,

141
00:05:32.720 --> 00:05:34.735
although we can use them also in other places as well.

142
00:05:34.735 --> 00:05:38.420
And that is the face colour and the edge colour.

143
00:05:38.420 --> 00:05:41.270
You can set the face colour to any colour you want.

144
00:05:41.270 --> 00:05:44.100
For example, face colour equal,

145
00:05:44.100 --> 00:05:46.910
let's say 5c44fd, for example.

146
00:05:46.910 --> 00:05:48.160
And now when we run this,

147
00:05:49.470 --> 00:05:51.752
we get a beautiful blue in the background there,

148
00:05:51.752 --> 00:05:54.880
that totally makes the text super readable.

149
00:05:54.880 --> 00:05:56.350
And yeah, you can use that

150
00:05:56.350 --> 00:05:58.950
if you want to set a background colour for your graph.

151
00:05:58.950 --> 00:06:01.400
Notice that the axes have their own face colour,

152
00:06:01.400 --> 00:06:03.650
but you can set yourself separately.

153
00:06:03.650 --> 00:06:07.340
This is the face colour of the background image.

154
00:06:07.340 --> 00:06:08.539
So, that sets that.

155
00:06:08.539 --> 00:06:11.320
You can also use edge colour

156
00:06:11.320 --> 00:06:13.530
if you want to set the colour of the border,

157
00:06:13.530 --> 00:06:15.520
I'm gonna run this and open graph.png,

158
00:06:15.520 --> 00:06:18.050
and you'll see that, you can't see that blue anywhere.

159
00:06:18.050 --> 00:06:22.380
And that's because by default, figures don't have an edge.

160
00:06:22.380 --> 00:06:24.910
So, here we're setting the edge colour, but there is no edge.

161
00:06:24.910 --> 00:06:26.470
So, there's nothing to be seen.

162
00:06:26.470 --> 00:06:29.410
We have to manually change that when we create the figure.

163
00:06:29.410 --> 00:06:32.740
We can see line width, equal five, for example.

164
00:06:32.740 --> 00:06:35.010
And now when we run this and recreate our image,

165
00:06:35.010 --> 00:06:37.423
we can open it and you can see the border there.

166
00:06:38.500 --> 00:06:40.090
Alright, so I just wanted to teach you a little bit

167
00:06:40.090 --> 00:06:41.600
about these export options.

168
00:06:41.600 --> 00:06:45.030
I think that bbox_inches equal tight is really cool.

169
00:06:45.030 --> 00:06:46.750
It means that you don't really have to worry

170
00:06:46.750 --> 00:06:49.050
about the subplot adjustment whether your labels

171
00:06:49.050 --> 00:06:50.610
are exactly gonna fit, you know,

172
00:06:50.610 --> 00:06:53.180
whether your poll title is going to be super long or not.

173
00:06:53.180 --> 00:06:55.500
After all, when you're dealing with dynamic data,

174
00:06:55.500 --> 00:06:57.160
you may not necessarily know exactly

175
00:06:57.160 --> 00:06:59.870
how much space you need in the subplots,

176
00:06:59.870 --> 00:07:03.650
This is particularly useful to give defaults to the user

177
00:07:03.650 --> 00:07:05.410
when you're doing plt dot show.

178
00:07:05.410 --> 00:07:08.200
Because they can then change that if they want.

179
00:07:08.200 --> 00:07:09.841
So, just as an example there,

180
00:07:09.841 --> 00:07:13.730
I'll bring back plt dot show, and run this.

181
00:07:13.730 --> 00:07:16.060
And remember that here we've got point three five

182
00:07:16.060 --> 00:07:18.280
in the adjustment, but you can change that

183
00:07:18.280 --> 00:07:20.220
if it turns out it's too much or too little,

184
00:07:20.220 --> 00:07:23.160
you can just go ahead and add more room or remove room

185
00:07:23.160 --> 00:07:24.960
if you don't need it as the user.

186
00:07:24.960 --> 00:07:26.200
But when you're creating an image,

187
00:07:26.200 --> 00:07:27.840
you of course don't have that capability,

188
00:07:27.840 --> 00:07:29.748
you only have the image that is produced.

189
00:07:29.748 --> 00:07:31.670
That's why I like this.

190
00:07:31.670 --> 00:07:33.850
It allows you to give the user everything

191
00:07:33.850 --> 00:07:35.750
rather than just relying on them

192
00:07:35.750 --> 00:07:37.700
to change the settings as they need.

193
00:07:37.700 --> 00:07:39.290
So, thank you guys for joining me in this video.

194
00:07:39.290 --> 00:07:42.140
Thank you for watching, and I'll see you in the next one.

