Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib - Border around scatter plot points

I am following this tutorial.

I would like to use Matplotlib to create a scatter plot with points that are colored inside, but have a black border, such as this plot:

photo

However, when I copy the code exactly, I get this plot instead.

enter image description here

Here is the code:

colors = ['black', 'blue', 'purple', 'yellow', 'white', 'red', 'lime', 'cyan', 'orange', 'gray']
for i in range(len(colors)):
    x = reduced_data_rpca[:, 0][digits.target == i]
    y = reduced_data_rpca[:, 1][digits.target == i]
    plt.scatter(x, y, c=colors[i])
plt.legend(digits.target_names, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.xlabel('First Principal Component')
plt.ylabel('Second Principal Component')
plt.title("PCA Scatter Plot")
plt.show()

I tried adjusting the style, but that didn't help.

like image 339
max Avatar asked Jun 05 '18 18:06

max


People also ask

What is CMAP in scatter plot?

cmap: A map of colors to use in the plot.

What is Alpha in scatter plot?

Matplotlib allows you to regulate the transparency of a graph plot using the alpha attribute. By default, alpha=1. If you would like to form the graph plot more transparent, then you'll make alpha but 1, such as 0.5 or 0.25.

How do you change the shape of a scatterplot in Matplotlib?

Just use the marker argument of the plot() function to custom the shape of the data points.

What is marker size in scatter?

Per the documentation, scatter marker size property is marker proportional to area while the line marker property is proportional to diameter. The default values are 36 and 6 points, respectively, so they are the same visually by default.


1 Answers

When you use scatter plot, you set a color for both face and edge. In the official documentation you can find an additional parameter, edgecolors, which allows setting the edge color.

edgecolors : color or sequence of color, optional, default: None

If None, defaults to ‘face’

If ‘face’, the edge color will always be the same as the face color.

If it is ‘none’, the patch boundary will not be drawn.

For non-filled markers, the edgecolors kwarg is ignored and forced to ‘face’ internally.

So, after all, you need only plt.scatter(x, y, c=colors[i],edgecolors='black')

like image 146
rth Avatar answered Oct 18 '22 17:10

rth