478

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(total_lengths, sort_times_bubble, 'b-',
                total_lengths, sort_times_ins, 'r-',
                total_lengths, sort_times_merge_r, 'g+',
                total_lengths, sort_times_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 ideally generates a graph like the one below:

Graph

Nothing special, I know. However, it is unclear what data is being plotted where (I'm trying to plot the data of some sorting algorithms, length against time taken, and I'd like to make sure people know 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?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
  • I am confused by your concern of creating an extra variable. You have to make those objects behind the scenes anyway. – tacaswell Oct 01 '13 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. – Games Brainiac Oct 01 '13 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 Oct 01 '13 at 21:55

6 Answers6

794

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

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • 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 Jul 05 '17 at 03:42
  • 15
    `plt.legend(loc='upper left')` also works, where `plt` is from `import matplotlib.pyplot as plt`. – Matt Kleinsmith Jan 20 '18 at 08:10
  • 4
    Note for others: the `plt.legend()` call **needs** to be **after** `plt.plot(label="lab1")` – mauriii Nov 25 '19 at 20:18
  • 7
    @davidA Yes, you can simply pass a list of strings into [`plt.legend`](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.legend.html): `plt.legend(['First Label', 'Second Label'])` – Apollys supports Monica Dec 12 '19 at 23:41
  • 3
    I am sure actual documentation at: https://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 – user1953366 Apr 25 '20 at 04:18
62

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().

cameronroytaylor
  • 1,578
  • 1
  • 17
  • 18
  • copy-paste answer that doesn't require any reading, and with a picture! this answer deserves more credit – Gulzar Jun 10 '19 at 13:59
  • Hi Cameron, I tried to reach you and this is the best option I found... You are the author of a very helpful tool I used a lot: https://web.media.mit.edu/~crtaylor/calculator.html Unfortunately it does not work for some time now. Is it me or is the tool, that is broken. If so, can you fix it? – Thomas Wilde Dec 17 '20 at 21:20
  • 1
    Whoops! Fixed. Thanks for the message! Hope you learned something useful about Axes instances :) – cameronroytaylor Dec 19 '20 at 04:23
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

Akash Kandpal
  • 3,126
  • 28
  • 25
25

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

Boris Yakubchik
  • 3,861
  • 3
  • 34
  • 41
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)

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
sajalagrawal
  • 151
  • 1
  • 6
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.

blaklaybul
  • 810
  • 2
  • 7
  • 11