将颜色条添加到带有未填充圆圈的散点图

时间:2018-02-05 10:24:41

标签: python matplotlib colorbar

我无法让pyplot颜色条在散点图中配合,我专门设置了facecolors = None,只想显示散乱圆圈的边缘。有人可以指点我正确的方向吗?感谢。

这很好用:

    # 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

1 个答案:

答案 0 :(得分:0)

我认为你可以通过创建&#34;普通&#34;来欺骗系统。使用c=参数绘制颜色条的散点图。然后,在第二步中,删除facecolor以仅留下边缘。

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)

enter image description here