Skip to content
Advertisement

How to create multiple legends?

JavaScript

line plot

I’m trying to add a legend containing the black line. However the black line is a separate lineplot. How Do I include the black line into the existing legend or a separate legend?

Advertisement

Answer

You can to add a label to the line, via sns.lineplot(..., label=...).

Note that when using bbox_to_anchor for the legend, you also need to set loc=.... By default, loc='best', which change the anchor point depending on small changes in the plot or its parameters. plt.tight_layout() fits the legend and the labels nicely into the plot figure.

Here is some example code using Seaborn’s flights dataset.

JavaScript

two linesplots with legend

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement