Matplotlib: scatter plot with colormaps for edgecolor but no facecolor

前端 未结 2 995
暖寄归人
暖寄归人 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条回答
  •  [愿得一人]
    2020-12-19 05:42

    The c argument will affect facecolor and edgecolor simultaneouly, the arguments facecolor and edgecolor are hence ignored.

    A solution would be not to use the c argument together with a colormap, but instead use facecolors and edgecolors alone. In this case facecolors can be set to "None" and edgecolors can be given a list of colors to use.

    To create this list, the same colormap can be applied.

    c = plt.cm.gist_rainbow(colors)
    plt.scatter(x, y, s=area,facecolors="None", edgecolors=c, lw=1,alpha=0.5)
    

    A complete example:

    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
    
    c = plt.cm.gist_rainbow(colors)
    plt.scatter(x, y, s=area,facecolors="None", edgecolors=c, lw=2,alpha=0.5)
    plt.show()
    

提交回复
热议问题