43

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.

2
  • 3
    If I were to take a guess; marker='o' but you haven't given a runnable example. Commented Jun 5, 2018 at 18:43
  • 8
    You are looking for edgecolors= "black" Commented Jun 5, 2018 at 18:46

2 Answers 2

71

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

Sign up to request clarification or add additional context in comments.

1 Comment

For people like me that needed transparent points with a non-transparent border. Combined edgecolor='b' with color='none'
3

you can add edgecolors parameter to your plt.scatter() method.

plt.scatter(x, y, c=colors[i], edgecolors='black')

Scatter plot of dataframe with edgecolor is enabled.

Not: At least it works for me. I am following this course.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.