【问题标题】:Scatterplot with point colors representing a continuous variable in seaborn FacetGrid带有点颜色的散点图表示 seaborn FacetGrid 中的连续变量
【发布时间】:2017-11-22 07:48:40
【问题描述】:

我正在尝试在 python 中使用 seaborn 生成多面板图形,我希望我的多面板图形中点的颜色由连续变量指定。这是我尝试使用“iris”数据集做的一个示例:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
iris = sns.load_dataset('iris')

g = sns.FacetGrid(iris, col = 'species', hue = 'petal_length', palette = 'seismic')
g = g.map(plt.scatter, 'sepal_length', 'sepal_width', s = 100, alpha = 0.5)
g.add_legend()

这使得下图:

这很好,但传说太长了。我想对这些值的 1/4 进行采样(理想情况下),或者禁止显示颜色条。 例如,这样的事情可能是可以接受的,但我仍然想把它分成三个物种。

plt.scatter(iris.sepal_length, iris.sepal_width, alpha = .8, c = iris.petal_length, cmap = 'seismic')
cbar = plt.colorbar()

知道如何充分利用这两个情节吗?

编辑: 这个话题似乎是一个好的开始。

https://github.com/mwaskom/seaborn/issues/582

不知何故,对于这个用户来说,在其他一切运行之后简单地附加 plt.colorbar 似乎以某种方式工作。不过在这种情况下似乎没有帮助。

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    FacetGrid hue 是分类的,不是连续的。在FacetGrid 中获得散点图的连续颜色图需要做一些工作(与链接的 Github 问题中的imshow 不同,matplotlib 不保留对“当前活动的散点图映射器”的引用,以便对plt.colorbar 的魔术调用不会获取应用于点颜色的映射)。

    g = sns.FacetGrid(iris, col='species', palette = 'seismic')
    
    def facet_scatter(x, y, c, **kwargs):
        """Draw scatterplot with point colors from a faceted DataFrame columns."""
        kwargs.pop("color")
        plt.scatter(x, y, c=c, **kwargs)
    
    vmin, vmax = 0, 7
    cmap = sns.diverging_palette(240, 10, l=65, center="dark", as_cmap=True)
    
    g = g.map(facet_scatter, 'sepal_length', 'sepal_width', "petal_length",
              s=100, alpha=0.5, vmin=vmin, vmax=vmax, cmap=cmap)
    
    # Make space for the colorbar
    g.fig.subplots_adjust(right=.92)
    
    # Define a new Axes where the colorbar will go
    cax = g.fig.add_axes([.94, .25, .02, .6])
    
    # Get a mappable object with the same colormap as the data
    points = plt.scatter([], [], c=[], vmin=vmin, vmax=vmax, cmap=cmap)
    
    # Draw the colorbar
    g.fig.colorbar(points, cax=cax)
    

    【讨论】:

    • 在 matplotlib 2.0.2 上使用此代码,在 python 2.7 上使用 seaborn 0.7.1 我得到this image,它以相同颜色显示所有散点。知道为什么会这样吗?
    【解决方案2】:

    由于您询问的是散点图例,因此可以调整@mwaskom 的解决方案以生成具有如下散点图的图例:

    import numpy as np
    import seaborn as sns
    import matplotlib.pyplot as plt
    iris = sns.load_dataset('iris')
    
    g = sns.FacetGrid(iris, col='species', palette = 'seismic')
    
    def facet_scatter(x, y, c, **kwargs):
        kwargs.pop("color")
        plt.scatter(x, y, c=c, **kwargs)
    
    vmin, vmax = 0, 7
    cmap = plt.cm.viridis
    
    norm=plt.Normalize(vmin=vmin, vmax=vmax)
    
    g = g.map(facet_scatter, 'sepal_length', 'sepal_width', "petal_length",
              s=100, alpha=0.5, norm=norm, cmap=cmap)
    
    # Make space for the colorbar
    g.fig.subplots_adjust(right=.9)
    
    lp = lambda i: plt.plot([], color=cmap(norm(i)), marker="o", ls="", ms=10, alpha=0.5)[0]
    labels = np.arange(0,7.5,0.5)
    h = [lp(i) for i in labels]
    g.fig.legend(handles=h, labels=labels, fontsize=9)
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      • 2017-08-02
      • 2015-05-28
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      相关资源
      最近更新 更多