Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

small scatter plot markers in matplotlib are always black

Tags:

matplotlib

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

like image 824
zoo Avatar asked Jun 28 '11 07:06

zoo


People also ask

How do I change the marker color in matplotlib?

All of the line properties can be controlled by keyword arguments. For example, you can set the color, marker, linestyle, and markercolor with: plot(x, y, color='green', linestyle='dashed', marker='o', markerfacecolor='blue', markersize=12).

How do you change a marker color in a scatter plot in Python?

You can specify the marker size with the parameter s and the marker color with c in the plt. scatter() function.

How do I make the dots transparent in matplotlib?

Adjust the Transparency of Scatter PointsUtilize the alpha argument in our scatter method and pass in a numeric value between 0 and 1. A value of 0 will make the plots fully transparent and unable to view on a white background. A value of 1 is the default with non-transparent points.


3 Answers

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

like image 58
Daan Avatar answered Oct 03 '22 16:10

Daan


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.

like image 45
cyborg Avatar answered Oct 03 '22 16:10

cyborg


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!

like image 4
qg_jinn Avatar answered Oct 03 '22 16:10

qg_jinn