WEBVTT

1
00:00:00.350 --> 00:00:01.780
<v ->Hi guys and welcome back.</v>

2
00:00:01.780 --> 00:00:02.910
In this video, we're going to learn

3
00:00:02.910 --> 00:00:06.450
how to create a custom legend for our graphs.

4
00:00:06.450 --> 00:00:08.700
We've learned two ways of creating legends.

5
00:00:08.700 --> 00:00:10.550
The first one is to pass in a label

6
00:00:10.550 --> 00:00:14.170
to each plot and then calling .legend on the axes

7
00:00:14.170 --> 00:00:15.950
and that basically generates a legend

8
00:00:15.950 --> 00:00:17.860
for everything that has a label.

9
00:00:17.860 --> 00:00:20.320
The second approach was to assign the result

10
00:00:20.320 --> 00:00:22.160
of calling the plotting functions

11
00:00:22.160 --> 00:00:25.930
to variables and then passing them alongside with labels

12
00:00:25.930 --> 00:00:27.850
to the legends call.

13
00:00:27.850 --> 00:00:31.200
However, what we've got here is a third scenario

14
00:00:31.200 --> 00:00:33.600
where neither of those approaches really work.

15
00:00:33.600 --> 00:00:34.513
So let me explain.

16
00:00:35.350 --> 00:00:38.720
What we've got here is a figure with a single subplot

17
00:00:38.720 --> 00:00:40.060
and then a bar chart.

18
00:00:40.060 --> 00:00:42.180
The bar chart has six columns,

19
00:00:42.180 --> 00:00:43.930
just some random numbers in here

20
00:00:43.930 --> 00:00:47.100
but essentially, the labels are either tech companies

21
00:00:47.100 --> 00:00:48.200
or clothing companies.

22
00:00:48.200 --> 00:00:49.730
So we've got Apple, Google and Microsoft

23
00:00:49.730 --> 00:00:50.660
as tech companies,

24
00:00:50.660 --> 00:00:53.840
Burberry, Zara and Superdry as clothing companies.

25
00:00:53.840 --> 00:00:56.390
Something that we can pass to the bar call

26
00:00:56.390 --> 00:00:57.480
that we haven't looked at yet

27
00:00:57.480 --> 00:00:59.650
is that we can pass a colour list.

28
00:00:59.650 --> 00:01:02.200
Each colour will map to one bar.

29
00:01:02.200 --> 00:01:04.560
So Apple, Google and Microsoft

30
00:01:04.560 --> 00:01:07.390
are gonna be this colour, 5c44fd,

31
00:01:07.390 --> 00:01:08.830
Burberry, Zara and Superdry

32
00:01:08.830 --> 00:01:10.180
are gonna be this colour.

33
00:01:10.180 --> 00:01:11.830
Then we are rotating the xticks

34
00:01:11.830 --> 00:01:12.840
and we're showing this

35
00:01:12.840 --> 00:01:14.750
so let me run it and show you what it looks like.

36
00:01:14.750 --> 00:01:16.000
This is what it looks like.

37
00:01:16.000 --> 00:01:19.030
We've got the blue companies being the tech companies,

38
00:01:19.030 --> 00:01:21.230
red companies being clothing companies.

39
00:01:21.230 --> 00:01:22.890
So we want a legend now

40
00:01:22.890 --> 00:01:26.600
that says blue is tech, red is clothing

41
00:01:26.600 --> 00:01:27.890
but how can we do that

42
00:01:27.890 --> 00:01:29.860
because we only have one plot here?

43
00:01:29.860 --> 00:01:31.200
So if we set a label here,

44
00:01:31.200 --> 00:01:34.030
that's gonna be well, just for the entire plot,

45
00:01:34.030 --> 00:01:36.450
not for the different elements within the plot.

46
00:01:36.450 --> 00:01:38.530
Similarly, if we assign this to a variable

47
00:01:38.530 --> 00:01:40.690
and then we pass it to the legend call,

48
00:01:40.690 --> 00:01:44.280
we again are only gonna be able to set one legend item

49
00:01:44.280 --> 00:01:47.590
for this plot, so we need a custom legend.

50
00:01:47.590 --> 00:01:49.970
Each legend element is composed of two things,

51
00:01:49.970 --> 00:01:52.540
a patch of colour and a label.

52
00:01:52.540 --> 00:01:55.150
So we're going to first of all, create the patch.

53
00:01:55.150 --> 00:01:59.150
So we'll say from matplotlib.patches import Patch.

54
00:01:59.150 --> 00:02:01.670
This allows us to create just a patch of colour somewhere

55
00:02:01.670 --> 00:02:04.900
to be drawn and we're going to add the handles

56
00:02:04.900 --> 00:02:06.640
for the legends down here.

57
00:02:06.640 --> 00:02:08.190
So we'll create two patches.

58
00:02:08.190 --> 00:02:13.190
The first patch has a facecolor of 5c44fd.

59
00:02:13.250 --> 00:02:15.650
And a label of Tech.

60
00:02:15.650 --> 00:02:20.310
And the second patch has a facecolor of ff5566

61
00:02:20.310 --> 00:02:22.270
and a label of Clothing.

62
00:02:22.270 --> 00:02:26.730
So these here are our two handles for the legend.

63
00:02:26.730 --> 00:02:29.500
Handles is the same as a legend item.

64
00:02:29.500 --> 00:02:30.710
So now that we've got this,

65
00:02:30.710 --> 00:02:34.760
all we have to do is say plt.legend handles equal

66
00:02:34.760 --> 00:02:36.950
and we can pass in our variable there.

67
00:02:36.950 --> 00:02:38.280
Now, if we run this,

68
00:02:38.280 --> 00:02:40.240
you can see that we get Tech and Clothing there

69
00:02:40.240 --> 00:02:43.340
with their own little patch of colour and their label.

70
00:02:43.340 --> 00:02:44.350
And this is one of these things

71
00:02:44.350 --> 00:02:45.950
that I mentioned earlier on in the course.

72
00:02:45.950 --> 00:02:48.600
You can see that matplotlib has moved the legend over

73
00:02:48.600 --> 00:02:51.150
to the left because it realised that it would overlap

74
00:02:51.150 --> 00:02:52.670
with this bar here

75
00:02:52.670 --> 00:02:54.790
but it doesn't overlap with anything here.

76
00:02:54.790 --> 00:02:56.450
So that's it, creating a custom legend

77
00:02:56.450 --> 00:02:57.520
can be pretty helpful,

78
00:02:57.520 --> 00:02:59.560
so I hope you've enjoyed this video and learned something.

79
00:02:59.560 --> 00:03:00.570
Thank you for joining me

80
00:03:00.570 --> 00:03:02.220
and I'll see you in the next one.

