Individual alpha values in scatter plot

后端 未结 2 895
野的像风
野的像风 2020-12-02 17:12

I\'m wondering if it is possible to have individual alpha values for each point to be plotted using the scatter function of Matplotlib. I need to plot a set of

相关标签:
2条回答
  • 2020-12-02 17:26

    tcaswell's suggestion is correct, you can do it like this:

    import numpy as np
    import matplotlib.pylab as plt
    
    x = np.arange(10)
    y = np.arange(10)
    
    alphas = np.linspace(0.1, 1, 10)
    rgba_colors = np.zeros((10,4))
    # for red the first column needs to be one
    rgba_colors[:,0] = 1.0
    # the fourth column needs to be your alphas
    rgba_colors[:, 3] = alphas
    
    plt.scatter(x, y, color=rgba_colors)
    plt.show()
    

    0 讨论(0)
  • 2020-12-02 17:29

    You can use the color argument and a colormap with alpha. cmap linearly increases the alpha value from 0 to 1.

    import numpy as np
    import matplotlib.pylab as plt
    from matplotlib import colors
    
    c='C0'
    
    xs = np.arange(10)
    
    fig, ax = plt.subplots(1, 1)
    cmap = colors.LinearSegmentedColormap.from_list(
            'incr_alpha', [(0, (*colors.to_rgb(c),0)), (1, c)])
    ax.scatter(xs, xs, c=xs, cmap=cmap, ec=None, s=10**2)
    
    plt.show()
    
    0 讨论(0)
提交回复
热议问题