Python 更改matplotlib中按列值输入的图例

Python 更改matplotlib中按列值输入的图例,python,matplotlib,label,legend,Python,Matplotlib,Label,Legend,在matplotlib图表中,我一直在努力更改图例中的标签。 这是我的图表: 我想更改图例,以便标签将基于列名中名为“name”的列中的值 以下是我创建原始图形的方式: ax = plt.figure() df.iloc[3000:3005,:].loc[:,float_cols].T.plot(figsize=(10,6)) plt.title('title',size=(20)) plt.ylabel('Y', size=(14)) plt.xlabel('x', size=(14))

在matplotlib图表中,我一直在努力更改图例中的标签。 这是我的图表:

我想更改图例,以便标签将基于列名中名为“name”的列中的值

以下是我创建原始图形的方式:

ax = plt.figure()
df.iloc[3000:3005,:].loc[:,float_cols].T.plot(figsize=(10,6))
plt.title('title',size=(20))
plt.ylabel('Y', size=(14))
plt.xlabel('x', size=(14))
以下是我尝试将图例更改为列名的方式:

targets = df['name']

ax = plt.figure()
df.iloc[3000:3005,:].loc[:,float_cols].T.plot(figsize=(10,6).label=targets)
plt.title('title',size=(20))
plt.ylabel('Y', size=(14))
plt.xlabel('x', size=(14))
但它不起作用。我也尝试过其他方法,比如使用plt.legend,但没有成功

我的最终目标:将图例更改为具有基于这些观察结果名称的标签(从列名开始)

编辑:我试过:

plt.figure()
for i in range(df.shape[1]):
  plt.plot(df, df.iloc[3000:3005,:].loc[:,float_cols], label = df.columns['name'])

plt.legend()
plt.tight_layout()
plt.show()
但它不起作用,出现了以下错误:

索引器:仅整数、片(
)、省略号(
), numpy.newaxis(
None
)和整数或布尔数组是有效的索引

我也试过:

plt.figure()
for i in range(df.shape[1]):
  plt.plot(df, df.iloc[3000:3005,:].loc[:,float_cols], label = df.columns[i])

plt.legend()
plt.tight_layout()
plt.show()
targets = df['name']

plt.figure()
for i in range(df.shape[1]):
    plt.plot(df.iloc[3000:3005,:], label = targets[i])

plt.legend()
plt.tight_layout()
plt.show()
但也有错误:

ValueError:x和y必须具有相同的第一维度,但具有形状 (8606444)和(5438)

编辑2:尝试了以下操作:

plt.figure()
for i in range(df.shape[1]):
  plt.plot(df, df.iloc[3000:3005,:].loc[:,float_cols], label = df.columns[i])

plt.legend()
plt.tight_layout()
plt.show()
targets = df['name']

plt.figure()
for i in range(df.shape[1]):
    plt.plot(df.iloc[3000:3005,:], label = targets[i])

plt.legend()
plt.tight_layout()
plt.show()
获取错误信息:

在 3 plt.图() 4对于范围内的i(df形状): ---->5 plt.绘图(df.iloc[3000:3005,:],标签=目标[i]) 6. 7 plt.图例()

中的~.conda\envs\reut\lib\site packages\pandas\core\series.py getitem(self,key) 869 key=com.apply\u如果可调用(key,self) 870尝试: -->871结果=self.index.get_值(self,key) 872 873如果不是标量(结果):

中的~.conda\envs\reut\lib\site packages\pandas\core\index\base.py 获取_值(自身、系列、键)4403 k= self.\u convert\u scalar\u indexer(k,kind=“getitem”)4404尝试: ->4405返回self.\u engine.get\u值(s,k,tz=getattr(series.dtype,“tz”,None))4406除了keyrerror 如果len(self)>0且(self.holds_integer()为e1:4407 或self.is_boolean()):

pandas中的pandas\libs\index.pyx.\u libs.index.IndexEngine.get\u value()

pandas中的pandas\libs\index.pyx.\u libs.index.IndexEngine.get\u value()

pandas中的pandas\libs\index.pyx.\u libs.index.IndexEngine.get\u loc()

pandas\u libs\hashtable\u class\u helper.pxi位于 pandas._libs.hashtable.Int64HashTable.get_item()

pandas\u libs\hashtable\u class\u helper.pxi位于 pandas._libs.hashtable.Int64HashTable.get_item()

关键错误:0


使用matplotlib regular
plt.plot()

根据数据帧的组织,您可以将
目标更改为您想要的任何内容,包括
df['name']

最后,如果没有
x
向量,只需使用
plt.plot(df.iloc[:,i],label=targets[i])
即可,它将根据位置索引绘制数据,如问题中的示例所示

根据聊天室中的评论和讨论进行编辑:

对于您的情况,您可以尝试类似的方法:

indexes_wanted = [2000, 100, 3200]
targets = df['names']

plt.figure()
for i in indexes_wanted:
  plt.plot(x, df.iloc[i, :][float_cols], label = targets.iloc[i])

plt.legend()
plt.tight_layout()
plt.show()

您可以使用常规的matplolib
plt.plot()
,对这些列进行循环,修改这个示例:它不起作用,您可以在我的原始帖子中看到。我不知道为什么它不起作用,可能是因为它不是x-y,但它有更多的维度,所以标签不是列的名称,而是列中的值现在图例标签是索引,我想从列“name”中获取值,您可能想编辑您的初始帖子,如“from column name”这是误导性的。我现在已经编辑了,我希望它更清楚,如果你有任何想法,我想知道,请考虑接受答案,如果解决方案现在是令人满意的。