-: Hi guys and welcome back.
In this video, we're going to learn
how to create a custom legend for our graphs.
We've learned two ways of creating legends.
The first one is to pass in a label
to each plot and then calling .legend on the axes
and that basically generates a legend
for everything that has a label.
The second approach was to assign the result
of calling the plotting functions
to variables and then passing them alongside with labels
to the legends call.
However, what we've got here is a third scenario
where neither of those approaches really work.
So let me explain.
What we've got here is a figure with a single subplot
and then a bar chart.
The bar chart has six columns,
just some random numbers in here
but essentially, the labels are either tech companies
or clothing companies.
So we've got Apple, Google and Microsoft
as tech companies,
Burberry, Zara and Superdry as clothing companies.
Something that we can pass to the bar call
that we haven't looked at yet
is that we can pass a colour list.
Each colour will map to one bar.
So Apple, Google and Microsoft
are gonna be this colour, 5c44fd,
Burberry, Zara and Superdry
are gonna be this colour.
Then we are rotating the xticks
and we're showing this
so let me run it and show you what it looks like.
This is what it looks like.
We've got the blue companies being the tech companies,
red companies being clothing companies.
So we want a legend now
that says blue is tech, red is clothing
but how can we do that
because we only have one plot here?
So if we set a label here,
that's gonna be well, just for the entire plot,
not for the different elements within the plot.
Similarly, if we assign this to a variable
and then we pass it to the legend call,
we again are only gonna be able to set one legend item
for this plot, so we need a custom legend.
Each legend element is composed of two things,
a patch of colour and a label.
So we're going to first of all, create the patch.
So we'll say from matplotlib.patches import Patch.
This allows us to create just a patch of colour somewhere
to be drawn and we're going to add the handles
for the legends down here.
So we'll create two patches.
The first patch has a facecolor of 5c44fd.
And a label of Tech.
And the second patch has a facecolor of ff5566
and a label of Clothing.
So these here are our two handles for the legend.
Handles is the same as a legend item.
So now that we've got this,
all we have to do is say plt.legend handles equal
and we can pass in our variable there.
Now, if we run this,
you can see that we get Tech and Clothing there
with their own little patch of colour and their label.
And this is one of these things
that I mentioned earlier on in the course.
You can see that matplotlib has moved the legend over
to the left because it realised that it would overlap
with this bar here
but it doesn't overlap with anything here.
So that's it, creating a custom legend
can be pretty helpful,
so I hope you've enjoyed this video and learned something.
Thank you for joining me
and I'll see you in the next one.