WEBVTT

1
00:00:00.000 --> 00:00:01.270
<v ->Hi Guys, and welcome back.</v>

2
00:00:01.270 --> 00:00:02.580
In this video, we're going to learn

3
00:00:02.580 --> 00:00:05.040
how to add a legend to our charts,

4
00:00:05.040 --> 00:00:06.790
so that it's a bit easier to see

5
00:00:06.790 --> 00:00:09.760
what colours mean what in the plot.

6
00:00:09.760 --> 00:00:10.893
So, let's get started.

7
00:00:11.780 --> 00:00:14.410
Here we've got our chart from the last section,

8
00:00:14.410 --> 00:00:16.550
and we're going to press Play in here,

9
00:00:16.550 --> 00:00:18.760
and you can see that we get our chart.

10
00:00:18.760 --> 00:00:22.330
And we've got the blue and the orange colours,

11
00:00:22.330 --> 00:00:24.180
but it's not entirely clear what they mean.

12
00:00:24.180 --> 00:00:26.720
So, that's where the legend is gonna come into play.

13
00:00:26.720 --> 00:00:31.330
The first thing to do is to go ahead into our bar call here,

14
00:00:31.330 --> 00:00:33.060
and we're going to add a label.

15
00:00:33.060 --> 00:00:36.000
The label can be added to basically any plot.

16
00:00:36.000 --> 00:00:39.520
And it is used if you want to, when creating a legend.

17
00:00:39.520 --> 00:00:41.670
So, we're going to do men,

18
00:00:41.670 --> 00:00:45.610
and then in here, we're going to do label equal women.

19
00:00:45.610 --> 00:00:49.850
Now, just doing this won't do everything that we want.

20
00:00:49.850 --> 00:00:51.770
We've now given the plots a label,

21
00:00:51.770 --> 00:00:54.750
but we've not asked pyplot to create a legend.

22
00:00:54.750 --> 00:00:56.270
So, that's the next bit.

23
00:00:56.270 --> 00:00:58.460
We have to go ahead and down here,

24
00:00:58.460 --> 00:01:00.780
say axes dot legend,

25
00:01:00.780 --> 00:01:02.620
and that is going to generate a legend

26
00:01:02.620 --> 00:01:05.660
for the axes then have these plots.

27
00:01:05.660 --> 00:01:07.080
So, we can run it again.

28
00:01:07.080 --> 00:01:10.170
And now you can see we get men and women there.

29
00:01:10.170 --> 00:01:12.160
Pyplot is actually pretty smart with a legend.

30
00:01:12.160 --> 00:01:15.520
It's not always appearing in the top right hand corner.

31
00:01:15.520 --> 00:01:19.070
If you've got some short bars here and some long bars here,

32
00:01:19.070 --> 00:01:22.360
pyplot is often smart enough to move this over to the left.

33
00:01:22.360 --> 00:01:23.900
And, but it's not always perfect,

34
00:01:23.900 --> 00:01:25.773
so just bear that in mind as well.

35
00:01:26.720 --> 00:01:28.570
So, this was the first and simplest way

36
00:01:28.570 --> 00:01:30.330
of defining a legend.

37
00:01:30.330 --> 00:01:32.260
However, sometimes it can be useful

38
00:01:32.260 --> 00:01:36.420
to manually assign a legend item to individual plots.

39
00:01:36.420 --> 00:01:37.520
So, let's do that.

40
00:01:37.520 --> 00:01:40.020
We're gonna get rid of these labels,

41
00:01:40.020 --> 00:01:42.930
and instead, we're going to assign these plots

42
00:01:42.930 --> 00:01:44.100
to specific variables.

43
00:01:44.100 --> 00:01:47.250
So, we'll say men plot equal that,

44
00:01:47.250 --> 00:01:50.760
and women plot equal that.

45
00:01:50.760 --> 00:01:52.410
Now, when we've got this,

46
00:01:52.410 --> 00:01:56.190
we can then use them when we're creating our legends.

47
00:01:56.190 --> 00:01:58.100
So, what we do is in here,

48
00:01:58.100 --> 00:02:00.990
in the legend call, we pass in a tuple of plots.

49
00:02:00.990 --> 00:02:03.120
So, that's men plot and women plot.

50
00:02:03.120 --> 00:02:05.790
And then another tuple of labels.

51
00:02:05.790 --> 00:02:08.053
So, that's men and women.

52
00:02:09.290 --> 00:02:11.760
As you can see, that produces exactly the same output.

53
00:02:11.760 --> 00:02:14.570
I'm not entirely sure which one I prefer just yet.

54
00:02:14.570 --> 00:02:17.220
In this one, the legend is defined in one place,

55
00:02:17.220 --> 00:02:19.490
which is nice in terms of readability.

56
00:02:19.490 --> 00:02:22.100
But when you assign a label to each plot,

57
00:02:22.100 --> 00:02:26.170
then you allow pyplot to generate the legend for you

58
00:02:26.170 --> 00:02:28.690
from all the plots in the axes.

59
00:02:28.690 --> 00:02:30.970
So, it's up to you whether you want to create a legend

60
00:02:30.970 --> 00:02:32.830
for everything that has a label,

61
00:02:32.830 --> 00:02:34.770
or just for the things in here.

62
00:02:34.770 --> 00:02:36.720
And, but I wanted to show you both ways

63
00:02:36.720 --> 00:02:40.040
because they both can come in handy in different situations.

64
00:02:40.040 --> 00:02:41.860
Alright. Thank you guys for joining me in this video.

65
00:02:41.860 --> 00:02:44.560
Thanks for watching, and I'll see you in the next one.

