1

I want to plot a 2D NumPy array using line plots for each of the columns:

import numpy as np
import matplotlib.pyplot as plt

arr = np.random.random((10, 5))
ax.plot(arr)

However, I am not sure how to assign label names for each of the five columns.

Assume that the column names are : a, b, c, d and e.

1 Answer 1

5

As far as I'm aware, there's no built-in way to pass in multiple different labels for each line in a single call to plot. You could loop over columns in your array and plot each one separately:

labels = ['a', 'b', 'c', 'd', 'e']

for column, label in zip(arr.T, labels):
    ax.plot(column, label=label)

Or you could construct your legend by passing the line objects and their corresponding labels explicitly:

lines = ax.plot(arr)
ax.legend(lines, labels)
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.