1

Say I have the following:

fig  = figure()
ax   = f.add_subplot(111)

# my_values holds a 2D numpy array
lines = ax.plot(my_values)

Say that for each column of my_values I have a string holding the legend that I want associated with the corresponding line.

e.g.

my_legends = ['foo ' + str(x) for x in xrange(my_values.shape[1])]

I have the handles for the figure (fig), axes (ax) and the lines (lines), how can I add these legends to the plot?

1 Answer 1

2

You could call ax.legend:

ax.legend(lines, my_legends)

import matplotlib.pyplot as plt
import numpy as np

fig  = plt.figure()
ax   = fig.add_subplot(111)
my_values = np.cumsum(np.random.random(100)-0.5).reshape(-1, 2)
lines = ax.plot(my_values)
ax.legend(lines, ['eenie', 'meenie'], loc='best')
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.