matplotlib散射边而不指定edgecolor

问题描述 投票:4回答:1

现在看来,默认的散点图标记是没有边的实心圆。我想要一个带边缘的标记,并且facecolor =“none”。但是如果facecolor =“none”但未指定edgecolor,则绘图为空。我希望标记有多种不同的颜色,但不关心哪一种颜色。

我怎样才能“打开”边缘?

python matplotlib scatter
1个回答
6
投票

有两种方法可以生成空的或空心的散射标记:

Setting facecolor to "none"

您可以“关闭”面部,而不是“只是打开”边缘。因此,为了使散射标记的面部颜色透明,您可以将生成的PolyCollecton的面颜色设置为"none"

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

这与sc = ax.scatter(x,y, c=x, facecolor="none")不同,因为c论证覆盖了facecolor

完整的例子:

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

另一种选择是使用非填充标记。这将仅在边缘处具有其面部颜色。一个例子可能是来自marker="$\u25EF$"STIX font(另见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

注意:在python 2中,您需要使用marker=ur"$\u25EF$"

© www.soinside.com 2019 - 2024. All rights reserved.