Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 views
in Technique[技术] by (71.8m points)

python - matplotlib scatter edge without specifying edgecolor

It seems that now the default scatter plot marker is a filled circle without an edge. I want a marker with an edge and with facecolor="none". But if facecolor="none" but edgecolor is not specified, then the plot is empty. I want markers be in multiple distinct colors, but don't care which one has which color.

How can I just "turn on" the edges?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two ways to produce empty or hollow scatter markers:

Setting facecolor to "none"

Instead of "just turning on" the edges, you may "turn off" the faces. So in order to make the facecolors of scatter markers transparent you may set the facecolor of the resulting PolyCollecton to "none".

sc = ax.scatter(...)
sc.set_facecolor("none")

This is different from sc = ax.scatter(x,y, c=x, facecolor="none"), because the c argument overwrites the facecolor.

Complete example:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,2*np.pi,20)
y = np.sin(x)

fig, ax=plt.subplots()
sc = ax.scatter(x,y, c=x, cmap="nipy_spectral")
sc.set_facecolor("none")

plt.show()

enter image description here

Using non-filled marker

A different option is to use a non-filled marker. This would have its facecolor only at the edge. An example may be marker="$u25EF$" from the STIX font (also see this question)

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,2*np.pi,20)
y = np.sin(x)

fig, ax=plt.subplots()

sc = ax.scatter(x,y, c=x, marker="$u25EF$", cmap="nipy_spectral")

plt.show()

enter image description here

Note: In python 2, you would need to use marker=ur"$u25EF$" instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...