Matplotlib: scatter plot with colormaps for edgecolor but no facecolor

前端 未结 2 996
暖寄归人
暖寄归人 2020-12-19 04:51

I want to have a scatter plot with colormap for edgecolors but no facecolors. When I use facecolor=\'None\', it does not work.

import numpy as          


        
2条回答
  •  梦毁少年i
    2020-12-19 05:49

    The problem is that color= overrides the facecolors= argument.

    The solution I came up with is to get the PathCollection returned by pyplot.scatter() and then change the facecolor directly. Note that you probably need to increase the line width to see the edges better.

    import numpy as np
    import matplotlib.pyplot as plt
    
    
    N = 50
    x = np.random.rand(N)
    y = np.random.rand(N)
    colors = np.random.rand(N)
    area = np.pi * (15 * np.random.rand(N))**2  # 0 to 15 point radii
    
    a = plt.scatter(x, y, s=area,c=colors,facecolor='none',lw=2,cmap="gist_rainbow", alpha=0.5)
    a.set_facecolor('none')
    plt.show()
    

提交回复
热议问题