small scatter plot markers in matplotlib are always black

Matplotlib

Matplotlib Problem Overview


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() function's markers seem to have both a line and a fill. When the markers are small, only the line is visible, not the fill, and the line isn't the right colour (it's always black).

I can get exactly what I want using gnuplot: plot 'nodes' with points pt 0 lc rgb 'gray'

How can I make very small gray points using matplotlib scatterplot()?

Matplotlib Solutions


Solution 1 - Matplotlib

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).

Solution 2 - Matplotlib

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.

Solution 3 - Matplotlib

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!

Solution 4 - Matplotlib

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()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzooView Question on Stackoverflow
Solution 1 - MatplotlibDaanView Answer on Stackoverflow
Solution 2 - MatplotlibcyborgView Answer on Stackoverflow
Solution 3 - Matplotlibqg_jinnView Answer on Stackoverflow
Solution 4 - MatplotlibmarisanoView Answer on Stackoverflow