[英]Assign label names for line plot from NumPy 2D array
I want to plot a 2D
NumPy
array using line
plots for each of the columns: 我想为每个列使用line
图绘制2D
NumPy
数组:
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. 但是,我不确定如何为这五列分配label
名称。
Assume that the column names are : a
, b
, c
, d
and e
. 假定列名称为: a
, b
, c
, d
和e
。
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
. 据我所知,没有内置方法可以在一次调用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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.