微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 for 循环压缩一系列 seaborn 散点图?

如何解决如何使用 for 循环压缩一系列 seaborn 散点图?

有谁知道我可以如何使用 for 循环来遍历它?

sns.FacetGrid(iris,hue = "Species").map(sns.scatterplot,"Sepal Length","Sepal Width").add_legend()
sns.FacetGrid(iris,"Petal Length","Petal Width").add_legend()
sns.FacetGrid(iris,"Petal Length").add_legend()
sns.FacetGrid(iris,"Petal Width","Sepal Width").add_legend()

对于直方图,这很容易,但我试图将这段代码浓缩为散点图,但没有成功。我想我每次都需要遍历一对独特的列,但我不知道该怎么做。或者即使这是正确的做法。数据来自 Iris 数据集:http://archive.ics.uci.edu/ml/datasets/Iris

*我知道这是非常徒劳的,我只是想避免代码中的重复...

谢谢,

Caio

解决方法

使用您需要的值一次循环遍历两个列表可能会起作用:

first_names = ["Sepal Length","Petal Length","Sepal Length","Petal Width"]
second_names = ["Sepal Width","Petal Width","Sepal Width","Sepal Width"]

for first_name,second_name in zip(first_names,second_names):
    sns.FacetGrid(iris,hue = "Species").map(sns.scatterplot,first_name,second_name).add_legend()

如果您想获得萼片长度/宽度和花瓣长度/宽度的所有可能组合(虽然在您当前的代码中看起来不像),您可以使用:

names = ["Sepal Length","Petal Width"]

for first_name in names:
    for second_name in names:
        sns.FacetGrid(iris,second_name).add_legend()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。