首页 > 解决方案 > 根据一列中的数据在 matplotlib 图表中标记观察结果

问题描述

我用 matplotlib 创建了图:

在此处输入图像描述 我想添加每个点标记,但是应该从列中检索此数据,因此每个点都根据数据框中的“名称”列获取标记。

在此处输入图像描述

有没有办法根据列添加数据?

我试图用这个添加它:

plt.text(1, 1, df['name'], fontsize=9)

但它没有用,我收到了这个错误:

ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

这就是我构建整个图表的方式:

df_PCA=pca_func(StandardNormalVariate(df_time.iloc[:,15:]))

#PLOT 
fig = plt.figure(figsize = (12,8))
ax = fig.add_subplot(1,1,1) 
ax.set_xlabel('PCA1', fontsize = 15)
ax.set_ylabel('PCA2', fontsize = 15)
ax.set_title('PCA - 6 Treatments- 21/06/2019 14:00', fontsize = 20)
#plt.text(1, 1, df_time['name'], fontsize=9)

targets = df_time.code.unique()
colors = ['tab:red','tab:green','tab:blue','tab:orange','tab:purple','tab:cyan']
for target, color in zip(targets,colors):
    #goes to line. and check of 1,2,3,4,5,6 are true or false, 6 times total, and if true gives the color.
    indicesToKeep = df['code'] == target
    ax.scatter(df_PCA.loc[indicesToKeep, 'principal component 1']
               , df_PCA.loc[indicesToKeep, 'principal component 2']
               , c = color
               , s = 100)
ax.legend(targets)
ax.grid()

我的最终目标:在每个点旁边添加观察名称,以识别右上角并将其从我的数据集中删除

标签: pythonmatplotlibplot

解决方案


推荐阅读