将色条添加到散点图并使用非实心圆 [英] Add colorbar to scatterplot with non-filled circles

查看:66
本文介绍了将色条添加到散点图并使用非实心圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让pyplot彩色条配合使用,在我专门设置facecolors = None且仅想显示分散圆的边缘的散点图中.有人能指出我正确的方向吗?谢谢.

I cannot get the pyplot colorbar to cooperate in a scatter where I specifically set facecolors=None and only want to display the edge of the scattered circles. Could someone please point me in the right direction? Thanks.

这很好:

    # Solid circles - colorbar shows
    plt.scatter(x, y, c=z, s=15, lw=0.3, cmap='viridis')
    plt.colorbar()
    plt.show()

...但这不是:

    c = plt.cm.viridis(z)
    plt.scatter(x, y, facecolors = 'none', edgecolors = c, s = 15, lw = 0.3) 
    plt.colorbar()
    plt.show()


 Traceback (most recent call last): File "/API/ML_GenerateFeatures.py", line 221, in 
<module> va.scatter_color(df.OIR, df.fwd_delta_mid, df.spread)
File "/API/ML_GenerateFeatures.py", line 184, in scatter_color plt.colorbar() 
File "C:\Python27\Lib\site-packages\matplotlib\pyplot.py", line 2180, in colorbar ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
File "C:\Python27\Lib\site-packages\matplotlib\figure.py", line 1844, in colorbar cb = cbar.colorbar_factory(cax, mappable, **kw)
File "C:\Python27\Lib\site-packages\matplotlib\colorbar.py", line 1365, in colorbar_factory cb = Colorbar(cax, mappable, **kwargs)
File "C:\Python27\Lib\site-packages\matplotlib\colorbar.py", line 918, in __init__ mappable.autoscale_None()
File "C:\Python27\Lib\site-packages\matplotlib\cm.py", line 348, in autoscale_None 
raise TypeError('You must first set_array for mappable')
TypeError: You must first set_array for mappable

推荐答案

我认为您可以通过使用 c = 参数创建颜色栏的正常"散点图来欺骗系统.然后,在第二步中,除去面部颜色,只保留边缘.

I think you can trick the system by creating a "normal" scatterplot using the c= argument to seed the colorbar. Then, in a second step, remove the facecolor to leave only the edges.

x = np.random.random(size=(100,))
y = np.random.random(size=(100,))
c = np.random.random(size=(100,))

fig, ax = plt.subplots()
g = ax.scatter(x, y, marker='o', c=c)
g.set_facecolor('none')
fig.colorbar(g)

这篇关于将色条添加到散点图并使用非实心圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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