【问题标题】:Seaborn regplot with colorbar?Seaborn regplot 与颜色条?
【发布时间】:2015-08-01 22:02:39
【问题描述】:

我正在用 seaborn 的regplot 策划一些事情。据我了解,它在幕后使用pyplot.scatter。所以我假设如果我将散点图的颜色指定为一个序列,那么我就可以调用plt.colorbar,但它似乎不起作用:

sns.regplot('mapped both', 'unique; repeated at least once', wt, ci=95, logx=True, truncate=True, line_kws={"linewidth": 1, "color": "seagreen"}, scatter_kws={'c':wt['Cis/Trans'], 'cmap':'summer', 's':75})
plt.colorbar()

Traceback (most recent call last):

  File "<ipython-input-174-f2d61aff7c73>", line 2, in <module>
    plt.colorbar()

  File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 2152, in colorbar
    raise RuntimeError('No mappable was found to use for colorbar '

RuntimeError: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf).

为什么它不起作用,有没有办法解决它?


如果有一种简单的方法可以为尺寸生成图例,我会很好地使用点的大小而不是颜色

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    另一种方法是

    import seaborn as sns
    import matplotlib.pyplot as plt
    
    tips = sns.load_dataset("tips")
    
    points = plt.scatter(tips["total_bill"], tips["tip"],
                         c=tips["size"], s=75, cmap="BuGn")
    plt.colorbar(points)
    
    sns.regplot("total_bill", "tip", data=tips, scatter=False, color=".1")
    

    【讨论】:

    • 谢谢你,@mwaskom!为我没有弄清楚这一点而感到羞耻!我想我过多地假设了 seaborn 的全能,因此不要考虑将它与“原始”matplotlib 拼接在一起:)
    • 您可以在“纯”seaborn 中执行此操作。无需混合 plt scatter 然后 regplot。在我的回答中,您可以一起使用 seaborn scaptterplot 和 regplot,并将颜色条直接添加到 regplot。
    【解决方案2】:

    regplot 的 color 参数将单一颜色应用于 regplot 元素(这在 seaborn 文档中)。要控制散点图,需要通过 kwargs 传递:

    import pandas as pd
    import seaborn as sns
    import numpy.random as nr
    import matplotlib.pyplot as plt
    
    fig, ax = plt.subplots()
    
    data = nr.random((9,3))
    df = pd.DataFrame(data, columns=list('abc'))
    out = sns.regplot('a','b',df, scatter=True,
                      ax=ax,
                      scatter_kws={'c':df['c'], 'cmap':'jet'})
    

    然后您从 AxesSubplot seaborn 返回中获取可映射的事物(由 scatter 生成的集合),并指定您想要可映射的颜色条。请注意我的 TODO 评论,如果您打算对情节进行其他更改来运行它。

    outpathc = out.get_children()[3] 
    #TODO -- don't assume PathCollection is 4th; at least check type
    
    plt.colorbar(mappable=outpathc)
    
    plt.show()
    

    【讨论】:

    • 很好的答案,但在 seaborn 情节中使用喷气式颜色图让你感到羞耻 :)
    • 教学法!很明显,这是一个颜色图,不是 seaborn 做的。 ...虽然丑陋,但对色盲没有帮助。
    • 谢谢!这是一个不错的选择,但我会接受另一个,因为它不对任何事情做任何假设,并且应该适用于任何数据。
    • 这很好,但我猜颜色方法不会将标称值转换为离散颜色。所以我必须这样做:color_map = {' 36 months': 0, ' 60 months': 1} plot_df['term_color'] = plot_df.term.map(color_map)
    【解决方案3】:

    事实上,您可以简单地访问 seaborn 绘图的图形对象并使用其colorbar() 函数手动添加颜色条。我发现这种方法要好得多。

    此代码生成附加图:

    import seaborn as sns
    import matplotlib.pyplot as plt
    tips = sns.load_dataset("tips")
    sns.scatterplot(tips["total_bill"], tips["tip"],
                         hue=tips["size"], s=75, palette="BuGn", legend=False)
    reg_plot=sns.regplot("total_bill", "tip", data=tips, scatter=False, color=".1")
    reg_plot.figure.colorbar(mpl.cm.ScalarMappable([![enter image description here][1]][1]norm=mpl.colors.Normalize(vmin=0, vmax=tips["size"].max(), clip=False), cmap='BuGn'),label='tip size')
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-17
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 2016-01-05
    相关资源
    最近更新 更多