seaborn/matplotlib中的散点图,其中点大小和颜色由连续数据帧列给出

jll*_*jll 6 python matplotlib pandas

我想在seaborn/matplotlib中制作一个散点图,其中点的大小由数据帧中的(连续)值确定,点的颜色也由数据帧中另一列的连续值确定.在ggplot中,这样做的方法是:

ggplot(iris) + geom_point(aes(x=Sepal.Width, y=Sepal.Length, size=Petal.Width, color=Petal.Length))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 (这里的颜色/尺寸是连续的非分类值)

seaborn/matplotlib中的语法是什么?

Imp*_*est 7

以下是从问题中重现代码图.选择一个图例有点麻烦,因为我们必须手动定义一些代理艺术家来放置图例并删除通过seaborn样式生成的第一个自动图例条目.

在此输入图像描述

import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset("iris")

plt.scatter(iris.sepal_width, iris.sepal_length, 
            c = iris.petal_length, s=(iris.petal_width**2)*60, cmap="viridis")
ax = plt.gca()

plt.colorbar(label="petal_length")
plt.xlabel("sepal_width")
plt.ylabel("sepal_length")

#make a legend:
pws = [0.5, 1, 1.5, 2., 2.5]
for pw in pws:
    plt.scatter([], [], s=(pw**2)*60, c="k",label=str(pw))

h, l = plt.gca().get_legend_handles_labels()
plt.legend(h[1:], l[1:], labelspacing=1.2, title="petal_width", borderpad=1, 
            frameon=True, framealpha=0.6, edgecolor="k", facecolor="w")

plt.show()
Run Code Online (Sandbox Code Playgroud)

请注意,size参数s表示点的面积.因此,为了使直径与要显示的数量成比例,必须将其平方.