繁体   English   中英

使用 seaborn.facetgrid,如何指定映射散点图的颜色以反映数据框中列的值?

[英]With seaborn.facetgrid, how do I specify the color of a mapped scatter plot to reflect the values of a column in the data frame?

我想创建散点图的 FacetGrid,其中点的颜色由绘制的数据框中的列定义。 但是,当我映射它时,似乎我无法将列名传递给plt.scatterc=参数,因为它被解释为一串颜色而不是列名:

import seaborn as sns
import matplotlib.pyplot as plt

sns.set(style='white')

iris = sns.load_dataset('iris')
g = sns.FacetGrid(iris, row='species', size=4)
g.map(plt.scatter, 'sepal_width', 'sepal_length', c='petal_length')

出去:

/home/user/anaconda/lib/python2.7/site-packages/matplotlib/colors.pyc in to_rgba_array(self, c, alpha)
    420             result = np.zeros((nc, 4), dtype=np.float)
    421             for i, cc in enumerate(c):
--> 422                 result[i] = self.to_rgba(cc, alpha)
    423             return result
    424 

/home/user/anaconda/lib/python2.7/site-packages/matplotlib/colors.pyc in to_rgba(self, arg, alpha)
    374         except (TypeError, ValueError) as exc:
    375             raise ValueError(
--> 376                 'to_rgba: Invalid rgba arg "%s"\n%s' % (str(arg), exc))
    377 
    378     def to_rgba_array(self, c, alpha=None):

ValueError: to_rgba: Invalid rgba arg "p"
to_rgb: Invalid rgb arg "p"
could not convert string to float: p

我预期的结果与plt.scatter(iris.sepal_width, iris.sepal_length, c=iris.petal_length)

在此处输入图片说明

我简单地尝试了sns.regplot ,但它似乎遇到了同样的问题。 如果我不指定 FacetGrid 的row=col=参数,我可以输入c=iris.petal_length以获得预期结果。

有没有办法创建一个 FacetGrid,其中数据按行或列分组,数据点根据数据框中的列着色?

在 DataFrame 中标识为列的变量需要与绘图函数中的位置参数相对应。 最简单的方法是在plt.scatter周围编写一个小包装函数,使其签名scatter(x, y, c)而不是scatter(x, y, s, c)

import seaborn as sns
import matplotlib.pyplot as plt

sns.set(style='white')

iris = sns.load_dataset('iris')
g = sns.FacetGrid(iris, row='species', size=4)

def scatter(x, y, c, **kwargs):
    plt.scatter(x, y, c=c, **kwargs)

g.map(scatter, 'sepal_width', 'sepal_length', 'petal_length')

这是你想要做的吗?

g.map(plt.scatter, 'sepal_width', 'sepal_length', c=iris.petal_length)

您可以通过指定hue参数来实现此目的。

g = sns.FacetGrid(iris, col='species', hue='petal_length', size=4)
g.map(plt.scatter, 'sepal_width', 'sepal_length')

这产生了这个情节:这个情节 .

潜在的问题是 FacetGrid 的map函数通过将它们添加到绘图函数的**kwargs来无意中管理color值。
如果要为散点图指定自己的ccolor ,可以在一个小包装器中从 kwargs 中删除 color 参数,然后指定您自己的参数,而不会出现任何错误:

def custom_scatter(x, y, c, **kwargs):
  del kwargs["color"]
  plt.scatter(x, y, c = c, **kwargs)

现在您可以使用custom_scatter进行映射:

g.map(custom_scatter, 'sepal_width', 'sepal_length', 'petal_length')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM