带颜色条的 Seaborn regplot? [英] Seaborn regplot with colorbar?

查看:40
本文介绍了带颜色条的 Seaborn regplot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 seaborn 的 regplot 绘制一些东西.据我了解,它在后台使用了 pyplot.scatter .因此,我假设如果将散点图的颜色指定为序列,则可以调用 plt.colorbar ,但它似乎不起作用:

I'm plotting something with seaborn's regplot. As far as I understand, it uses pyplot.scatter behind the scenes. So I assumed that if I specify colour for scatterplot as a sequence, I would then be able to just call plt.colorbar, but it doesn't seem to work:

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

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

Why doesn't it work, and is there a way around it?

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

I would be fine with using size of the dots instead of colour if there was an easy way to generate a legend for the sizes

推荐答案

另一种方法是

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

这篇关于带颜色条的 Seaborn regplot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆