1

I am plotting 2D numpy arrays using

import numpy as np
import matplotlib.pyplot as plt

x = np.array([1,2,3])
y = np.array([[2,2.2,3],[1,5,1]])
plt.plot(x,y.T[:,:])
plt.legend()
plt.show()

I want a legend that tells which line belongs to which row. Of course, I realize I can't give it meaningful names, but I need some sort of unique label for the line without running through loop.

1 Answer 1

2
import numpy as np
import matplotlib.pyplot as plt
import uuid

x = np.array([1,2,3])
y = np.array([[2,2.2,3],[1,5,1]])

fig, ax = plt.subplots()
lines = ax.plot(x,y.T[:,:])
ax.legend(lines, [str(uuid.uuid4())[:6] for j in range(len(lines))])


plt.show()

example output

(This is off of the current mpl master branch with a preview of the 2.0 default styles)

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

2 Comments

The OP asked for unique labels, so those labels are guaranteed to be unique ;)
ued [ j+1 for j in range(len(lines))] which is unique enough. Thank you

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.