简体   繁体   English

为NumPy 2D数组中的线图分配标签名称

[英]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 . 假定列名称为: abcde

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM