使用 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,其中数据按行或列分组,数据点根据数据框中的列着色?

这就是你想要做的吗?

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

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')

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

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

生成此图:

潜在的问题是 FacetGrid 的 map 函数无意中管理了 color 的值,方法是将它们添加到绘图函数的 **kwargs.
如果你想为散点图指定你自己的ccolor,你可以在一个小包装器中从kwargs中删除颜色参数,然后指定你自己的没有任何错误:

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')