为来自一列矩阵的每个线图创建图例条目

时间:2021-12-16 23:40:56

I plot my matrix nH.T which has 4 columns. So I want to make the legend, each column has the name from list latent_prot2.

我绘制了我的矩阵nH.T,它有4列。所以我想制作图例,每列的名称都来自列表latent_prot2。

latent_prot2 = np.array(["A","B","C","E"])

ax3 = plt.subplot2grid((3, 4), (2, 0), colspan=3, rowspan=1)
ax3.plot(nH.T, label=[n for i,n in enumerate(latent_prot2)])
plt.legend()

What I get is all the list printed out for each line.

我得到的是每行打印的所有列表。

为来自一列矩阵的每个线图创建图例条目

I tried ax3.plot(nH.T, label=[n[i] for i,n in enumerate(latent_prot2)]) or change the last line to plt.legend(label=latent_prot2) but they are not work. How to write the name for each column data from matrix in legend from list? And it would be good if I don't have to go via for loop but I don't know that is possible or not.

我尝试了ax3.plot(nH.T,label = [n [i] for i,n in enumerate(latent_prot2)])或将最后一行更改为plt.legend(label = latent_prot2)但它们不起作用。如何从列表中的图例中的矩阵中为每个列数据写入名称?如果我不必通过循环但是我不知道这是否可能会很好。

1 个解决方案

#1


1  

Using the label argument to plot you will get the same label for each plotted curve.

使用label参数绘制图形,您将获得每条绘制曲线的相同标签。

To manipulate the legend, you would pass the list of elements to the legend call

要操纵图例,您需要将元素列表传递给图例调用

ax.legend(labels=["A","B","C","E"])

Complete example:

完整的例子:

import matplotlib.pyplot as plt
import numpy as np

a = np.cumsum(np.cumsum(np.random.randn(15,4), axis=0), axis=1)

lab = np.array(["A","B","C","E"])

fig, ax = plt.subplots()
ax.plot(a)
ax.legend(labels=lab )

plt.show()

为来自一列矩阵的每个线图创建图例条目

#1


1  

Using the label argument to plot you will get the same label for each plotted curve.

使用label参数绘制图形,您将获得每条绘制曲线的相同标签。

To manipulate the legend, you would pass the list of elements to the legend call

要操纵图例,您需要将元素列表传递给图例调用

ax.legend(labels=["A","B","C","E"])

Complete example:

完整的例子:

import matplotlib.pyplot as plt
import numpy as np

a = np.cumsum(np.cumsum(np.random.randn(15,4), axis=0), axis=1)

lab = np.array(["A","B","C","E"])

fig, ax = plt.subplots()
ax.plot(a)
ax.legend(labels=lab )

plt.show()

为来自一列矩阵的每个线图创建图例条目