small scatter plot markers in matplotlib are always black

后端 未结 4 461
后悔当初
后悔当初 2021-02-02 05:47

I\'m trying to use matplotlib to make a scatter plot with very small gray points. Because of the point density, the points need to be small. The problem is that the scatter()

相关标签:
4条回答
  • 2021-02-02 06:14

    The absolute simplest answer to your question is: use the color parameter instead of the c parameter to set the color of the whole marker.

    It's easy to see the difference when you compare the results:

    from matplotlib import pyplot as plt
    
    plt.scatter([1,2,3], [3,1,2], c='0.8')  # marker not all gray
    
    plt.scatter([1,2,3], [3,1,2], color='0.8')  # marker all gray
    

    Details: For your simple use case where you just want to make your whole marker be the same shade of gray color, you really shouldn't have to worry about things like face color vs edge color, and whether your marker is defined as all edges or some edges and some fill. Instead, just use the color parameter and know that your whole marker will be set to the single color that you specify!

    0 讨论(0)
  • 2021-02-02 06:15

    If the marker has no face (cannot be filled, e.g. '+','x'), then the edgecolor has to be set instead of c, and lw should not be 0:

    scatter([1,2,3], [2,4,5], marker='+', edgecolor='r')
    

    The following will no work

    scatter([1,2,3], [2,4,5], s=1,  marker='+', facecolor='0.5', lw = 0)
    

    because the edge/line will not be displayed, so nothing will be displayed.

    0 讨论(0)
  • 2021-02-02 06:21
    scatter([1,2,3], [2,4,5], s=1, facecolor='0.5', lw = 0)
    

    This sets the markersize to 1 (s=1), the facecolor to gray (facecolor='0.5'), and the linewidth to 0 (lw=0).

    0 讨论(0)
  • 2021-02-02 06:24

    In response to zwol's question in comment - my reputation is not high enough to leave comments, so this will have to do: In the event that your colors come from a colormap (i.e., are from a "sequence of values to be mapped") you can use color = as demonstrated in the following:

    from matplotlib import pyplot
    
    x = [1,5,8,9,5]
    y = [4,2,4,7,9]
    numSides = [2,3,1,1,5]
    
    cmap = pyplot.cm.get_cmap("copper_r")
    
    min, max = min(numSides), max(numSides)
    for i in range(len(x)):
        if numSides[i] >= 2:
            cax = pyplot.scatter(x[i], y[i], marker = '+', s = 100, c = numSides[i], cmap = cmap)
            cax.set_clim(min, max)
        elif numSides[i] == 1:
            pyplot.scatter(x[i], y[i], marker = '.', s = 40, color = cmap(numSides[i]))
    
    fig = pyplot.gcf()
    fig.set_size_inches(8.4, 6)
    fig.savefig('figure_test.png', dpi = 200)
    pyplot.show()
    
    0 讨论(0)
提交回复
热议问题