python - 根据一列中的数据在 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()
我的最终目标:在每个点旁边添加观察名称,以识别右上角并将其从我的数据集中删除
解决方案
推荐阅读
- apache-spark - 如何在 7 天内刷新一次运行结构化流式 Spark 应用程序的 kerberos 票证?
- java - 解析函数无法解析字符串并在 Java 中抛出错误
- python - Flask Bcrypt 密码哈希不匹配,如何解决?
- discord.py - Discord.py 嵌入文本文件,获得更多结果
- node.js - Heroku 部署后 React 应用程序未加载
- react-native - 本机基础复选框显示连字符而不是刻度
- sql - SQL查询的奇怪条件
- flask - 获取请求结果时邮递员 500 内部服务器错误中的错误
- python - QStackedWidget 打开多个窗口
- apache-flink - flink应用中如何指定两个source,一个process operator,一个sink operator