python - 如何将标签添加到pairplot?
问题描述
我有一个具有以下形式的数据框:
0 1 2 3 ... 9 10 11 output
0 2.451775 1.9565675 0.843128 -0.007820 ... 0.74554812 0.090777 -1.90625 a
1 -0.855458 -0.8444604 -0.619685 0.2273399 ... 1.3771857 4.089319 -0.16289 a
2 1.554580 1.9164567 0.643128 -0.0077550 ... 0.7771542 5.090777 -1.90625 b
这是标准化 numpy 数组并添加最后一列名为 output 的结果。我添加了最后一列,以便能够在带有 hue=output 的 sns.pairplot 中使用它。
所以我想要的是显示而不是数字 0,1,2,.., 一组标签,因此我有一个标签列表,例如:
labels=["label 1","label 2",...,"label n" ]
我想添加到我的pairplot中以使用标签名称而不是索引号0、1、...n-1。我做了以下事情:
sns.pairplot(df,vars=labels,hue="output")
但我收到以下错误:
KeyError: 'label 1'
我尝试了`reset_index(drop = True)的选项,但根本没有结果。我怎样才能解决这个问题?
谢谢
解决方案
我认为您可以通过从“matplotlib”轴获取信息并设置它来做到这一点。
labels=["label 1","label 2",...,"label n" ]
g = sns.pairplot(df,vars=labels,hue="output")
for ax, lbl in zip(g.axes.flatten(), labels):
# print(ax, lbl)
ax.set_ylabel(f'{lbl}')
推荐阅读
- mysql - MySQL如何获取分钟级数据,数据频率为15秒
- php - 如何计算日历列上有多少事件?
- python - 如何比较 python 中的一些列表并将它们添加到数据框中?
- .net-core - 在应用程序之间切换时,即使访问令牌/ID 令牌有效,IdentityServer 4 也会导航到登录屏幕。有什么见解吗?
- c# - 将值从 datagridview 转换为文本框
- ruby-on-rails - 在 rails admin 中过滤虚拟字段
- java - 推特上传按钮的网址
- java - Spring Cloud Data Flow - 在 CommandLineRunner 之前处理异常
- python - XGBoost超参数ValueError的优化:标签必须由形式为0、1、2、...、[num_class - 1]的整数标签组成
- powershell - 将 $MyInvocation.UnBoundArguments 传递给 Powershell 中的嵌套函数