Euler_Salter
Euler_Salter

Reputation: 3561

Matplotlib: adding legend for each column of numpy array

Hi I have a numpy array

a = np.random.uniform(0,1, size = (10,3))

I want to plot each of the columns with its own label

plt.plot(a, label = ['label1', 'label2', 'label3'])
plt.legend()

How can I do that? The above is my tentative, but didn't work.

Upvotes: 2

Views: 3105

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339102

A slightly shorter approach (because the handles are already present in the legend):

import numpy as np
import matplotlib.pyplot as plt

a = np.random.uniform(0,1, size = (10,3))

plt.plot(a)
plt.legend(['label1', 'label2', 'label3'])

plt.show()

Upvotes: 3

Euler_Salter
Euler_Salter

Reputation: 3561

I think I found it here

l1,l2, l3 = plt.plot(a)
plt.legend((l1,l2, l3), ('label1', 'label2', 'label3'))

Upvotes: 0

Related Questions