开发者

small scatter plot markers in matplotlib are always black

I'm trying to use matplotlib to make a scatter plot with very small gray points. Because o开发者_JAVA百科f 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()?


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


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.


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!


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜