关于matplotlib:如何在Python中用空圆做散点图?

How to do a scatter plot with empty circles in Python?

在Python中,使用Matplotlib,如何绘制带有空圆的散点图? 目标是在已经由scatter()绘制的某些彩色磁盘周围绘制空圆,以便突出显示它们,理想情况下不必重绘彩色圆。

我尝试facecolors=None,但无济于事。


从分散的文档中:

1
2
3
4
5
6
Optional kwargs control the Collection properties; in particular:

    edgecolors:
        The string ‘none’ to plot faces with no outlines
    facecolors:
        The string ‘none’ to plot unfilled outlines

请尝试以下操作:

1
2
3
4
5
6
7
8
import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(60)
y = np.random.randn(60)

plt.scatter(x, y, s=80, facecolors='none', edgecolors='r')
plt.show()

example image

注意:对于其他类型的绘图,请参见markeredgecolormarkerfacecolor的用法。


这些行得通吗?

1
plt.scatter(np.random.randn(100), np.random.randn(100), facecolors='none')

example image

或使用plot()

1
plt.plot(np.random.randn(100), np.random.randn(100), 'o', mfc='none')

example image


这是另一种方式:这会在当前轴,图或图像等上添加一个圆:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from matplotlib.patches import Circle  # $matplotlib/patches.py

def circle( xy, radius, color="lightsteelblue", facecolor="none", alpha=1, ax=None ):
   """ add a circle to ax= or current axes
   """

        # from .../pylab_examples/ellipse_demo.py
    e = Circle( xy=xy, radius=radius )
    if ax is None:
        ax = pl.gca()  # ax = subplot( 1,1,1 )
    ax.add_artist(e)
    e.set_clip_box(ax.bbox)
    e.set_edgecolor( color )
    e.set_facecolor( facecolor )  #"none" not None
    e.set_alpha( alpha )

alt text

(由于imshow aspect="auto",图片中的圆圈被挤压成椭圆形)。


在matplotlib 2.0中,有一个名为fillstyle的参数
这样可以更好地控制标记的填充方式。
就我而言,我已将其与错误栏一起使用,但通常可用于标记
http://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.errorbar.html

fillstyle接受以下值:['full'|"左" | ‘正确’|"底部" |"顶部" | '没有']

使用fillstyle时要牢记两个重要事项:

1)如果将mfc设置为任何类型的值,它将具有优先权,因此,如果您将fillstyle设置为" none",则它不会生效。
因此,请避免同时使用mfc和fillstyle

2)您可能想控制标记的边缘宽度(使用markeredgewidthmew),因为如果标记相对较小且边缘宽度较厚,即使标记没有填充,标记也会看起来像填充的。

以下是使用错误栏的示例:

1
myplot.errorbar(x=myXval, y=myYval, yerr=myYerrVal, fmt='o', fillstyle='none', ecolor='blue',  mec='blue')

因此,我假设您想突出显示符合特定条件的一些要点。您可以使用Prelude的命令来绘制高光点的第二个散点图(带有一个空圆),并使用第一个调用来绘制所有点。确保s参数足够小,以使较大的空圆圈包围较小的填充圆。

另一个选择是不使用散点图,而使用circle / ellipse命令分别绘制补丁。这些位于matplotlib.patches中,这是一些有关如何绘制圆形矩形等的示例代码。


基于Gary Kerr的示例,如此处所建议,可以使用以下代码创建与指定值相关的空圆:

1
2
3
4
5
6
7
8
9
10
11
12
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.markers import MarkerStyle

x = np.random.randn(60)
y = np.random.randn(60)
z = np.random.randn(60)

g=plt.scatter(x, y, s=80, c=z)
g.set_facecolor('none')
plt.colorbar()
plt.show()