508

How can one create a legend for a line graph in Matplotlib's PyPlot without creating any extra variables?

Please consider the graphing script below:

if __name__ == '__main__':
    PyPlot.plot(length, bubble, 'b-',
                length, ins, 'r-',
                length, merge_r, 'g+',
                length, merge_i, 'p-', )
    PyPlot.title("Combined Statistics")
    PyPlot.xlabel("Length of list (number)")
    PyPlot.ylabel("Time taken (seconds)")
    PyPlot.show()

As you can see, this is a very basic use of matplotlib's PyPlot. This generates the following graph:

Graph

However, it is unclear which line is which. Thus, I need a legend; however, taking a look at the following example below (from the official site):

ax = subplot(1,1,1)
p1, = ax.plot([1,2,3], label="line 1")
p2, = ax.plot([3,2,1], label="line 2")
p3, = ax.plot([2,3,1], label="line 3")

handles, labels = ax.get_legend_handles_labels()

# reverse the order
ax.legend(handles[::-1], labels[::-1])

# or sort them by labels
import operator
hl = sorted(zip(handles, labels), key=operator.itemgetter(1))
handles2, labels2 = zip(*hl)

ax.legend(handles2, labels2)

You will see that I need to create an extra variable ax. How can I add a legend to my graph without having to create this extra variable and retaining the simplicity of my current script?

3
  • I am confused by your concern of creating an extra variable. You have to make those objects behind the scenes anyway.
    – tacaswell
    Commented Oct 1, 2013 at 21:01
  • 3
    @tcaswell Well let me try to assuage them. I do not want to create extra variables, because it adds complexity to the whole script. I'm trying to teach this to a bunch of students, and since they have't used matplotlib before, I wanted to keep things as simple as possible. Also, if you take a look at Rob's answer, its far simpler than the example shown on the website. I hope that helps. Commented Oct 1, 2013 at 21:08
  • 3
    I would argue that using the state machine interface makes it harder to understand in the long run because so much of it is being done 'by magic'. Also, the convention is to use import matplotlib.pyplot as plt instead of PyPlot
    – tacaswell
    Commented Oct 1, 2013 at 21:55

7 Answers 7

843

Add a label= to each of your plot() calls, and then call legend(loc='upper left').

Consider this sample (tested with Python 3.8.0):

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 20, 1000)
y1 = np.sin(x)
y2 = np.cos(x)

plt.plot(x, y1, "-b", label="sine")
plt.plot(x, y2, "-r", label="cosine")
plt.legend(loc="upper left")
plt.ylim(-1.5, 2.0)
plt.show()

enter image description here Slightly modified from this tutorial: http://jakevdp.github.io/mpl_tutorial/tutorial_pages/tut1.html

5
  • 5
    Is there a way to do this if you don't know the labels at the time the series is plotted? I.e. a way to add labels to a series after it has already been plotted? Or maybe a way to modify placeholder labels before showing the legend?
    – davidA
    Commented Jul 5, 2017 at 3:42
  • 15
    plt.legend(loc='upper left') also works, where plt is from import matplotlib.pyplot as plt. Commented Jan 20, 2018 at 8:10
  • 6
    Note for others: the plt.legend() call needs to be after plt.plot(label="lab1")
    – mauriii
    Commented Nov 25, 2019 at 20:18
  • 7
    @davidA Yes, you can simply pass a list of strings into plt.legend: plt.legend(['First Label', 'Second Label']) Commented Dec 12, 2019 at 23:41
  • 3
    I am sure actual documentation at: matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.legend.html also answers this, but it is such a pain to get that info from there as compared to here. We need to re-imagine documentation. lolz Commented Apr 25, 2020 at 4:18
63

You can access the Axes instance (ax) with plt.gca(). In this case, you can use

plt.gca().legend()

You can do this either by using the label= keyword in each of your plt.plot() calls or by assigning your labels as a tuple or list within legend, as in this working example:

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-0.75,1,100)
y0 = np.exp(2 + 3*x - 7*x**3)
y1 = 7-4*np.sin(4*x)
plt.plot(x,y0,x,y1)
plt.gca().legend(('y0','y1'))
plt.show()

pltGcaLegend

However, if you need to access the Axes instance more that once, I do recommend saving it to the variable ax with

ax = plt.gca()

and then calling ax instead of plt.gca().

1
  • 1
    copy-paste answer that doesn't require any reading, and with a picture! this answer deserves more credit
    – Gulzar
    Commented Jun 10, 2019 at 13:59
31

You can add a custom legend documentation

first = [1, 2, 4, 5, 4]
second = [3, 4, 2, 2, 3]
plt.plot(first, 'g--', second, 'r--')
plt.legend(['First List', 'Second List'], loc='upper left')
plt.show()

enter image description here

28

Here's an example to help you out ...

fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(111)
ax.set_title('ADR vs Rating (CS:GO)')
ax.scatter(x=data[:,0],y=data[:,1],label='Data')
plt.plot(data[:,0], m*data[:,0] + b,color='red',label='Our Fitting 
Line')
ax.set_xlabel('ADR')
ax.set_ylabel('Rating')
ax.legend(loc='best')
plt.show()

enter image description here

1
  • 23
    I'm just curious, why is your fitting line so far off the data? Commented Dec 12, 2019 at 23:28
15

A simple plot for sine and cosine curves with a legend.

Used matplotlib.pyplot

import math
import matplotlib.pyplot as plt
x=[]
for i in range(-314,314):
    x.append(i/100)
ysin=[math.sin(i) for i in x]
ycos=[math.cos(i) for i in x]
plt.plot(x,ysin,label='sin(x)')  #specify label for the corresponding curve
plt.plot(x,ycos,label='cos(x)')
plt.xticks([-3.14,-1.57,0,1.57,3.14],['-$\pi$','-$\pi$/2',0,'$\pi$/2','$\pi$'])
plt.legend()
plt.show()

Sin and Cosine plots (click to view image)

10

Add labels to each argument in your plot call corresponding to the series it is graphing, i.e. label = "series 1"

Then simply add Pyplot.legend() to the bottom of your script and the legend will display these labels.

1
  • 1
    This is the right idea, but you never add the labels so the legend will be empty
    – tacaswell
    Commented Oct 1, 2013 at 21:02
3

Explicitly passing handles and labels

Going off of OP's code where a bunch of lines were drawn with a single plot() call, we can still add a legend afterwards. With pyplot.plot, all lines are drawn on a single Axes object which can be accessed via plt.gca().lines. In the example code below, print(plt.gca().lines) outputs <Axes.ArtistList of 4 lines>. This list contains all the lines drawn on the Axes in the order they were drawn. Also, since this is a list, it can be unpacked into separate variables. So you can reorder them however you want and pass them as the first argument along with the corresponding labels list as the second argument to plt.legend().

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-0.7, 1, 100)
y0 = 13 - np.exp(1-2*x)
y1 = 7 - 4*np.sin(4*x)
y2 = np.random.default_rng(0).normal(8, 3, 100)

plt.plot(
    x, y0, 'b--', 
    x, y1, 'r-',
    x, x+14, 'k',
    x, y2, 'g+'
)

exp, sine, line, normal = plt.gca().lines   # unpack the list
plt.legend([line, normal, exp, sine], ['line', 'normal', 'exp', 'sine']);

img1

Labeling existing plots

You can also pass a list of labels to the legend() call to label existing plots (without the handles). The labels in the list must be ordered in the order the corresponding plot is plotted. This method is especially useful if you use a third-party library that uses matplotlib in the backend but doesn't have a label= parameter in the plotting function call. In other words, you can label plots after it is plotted. However, make sure to get the order right.

plt.plot(x, y0, 'b--', x, y1, 'r-')
plt.plot(x, x+14, c='k')
plt.scatter(x, y2, c='g', marker='+', linewidths=1)
plt.legend(['exp', 'sine', 'line', 'normal']);

Not the answer you're looking for? Browse other questions tagged or ask your own question.