如何用Python中的空心圆做散点图?

Eri*_*got 143 python geometry scatter matplotlib scatter-plot

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

我试过facecolors=None,但没有用.

Gar*_*err 209

从散文文档:

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
Run Code Online (Sandbox Code Playgroud)

请尝试以下方法:

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()
Run Code Online (Sandbox Code Playgroud)

示例图片

注意:对于其他类型的图表,请参阅有关使用和的这篇文章.markeredgecolormarkerfacecolor

  • 很有帮助.它是`markerfacecolor ='none',这就是它现在的样子. (21认同)
  • 谢谢!问题是,更常见的(在Python中)`facecolors = None`不起作用,这使我绊倒了. (4认同)
  • 我的matplotlib风格设置为将边缘宽度设置为零.如果你没有看到任何带有`markerfacecolor ='none'的标记,请尝试添加`markeredgewidth = 1.0` (2认同)
  • 边缘宽度当前(v3.4.2)由参数“linewidths”而不是“markeredgewidth”指定。[文档](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.scatter.html#matplotlib.pyplot.scatter) (2认同)

Gök*_*ver 60

这些会有用吗?

plt.scatter(np.random.randn(100), np.random.randn(100), facecolors='none')
Run Code Online (Sandbox Code Playgroud)

示例图片

或使用plot()

plt.plot(np.random.randn(100), np.random.randn(100), 'o', mfc='none')
Run Code Online (Sandbox Code Playgroud)

示例图片

  • 对于`scatter`,您需要指定`edgecolors`,如`edgecolors='r'`。否则,圆圈消失。[见这里](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.scatter.html) (3认同)
  • @T_T 如果我指定 `edgecolors='r'` 我必须手动指定颜色。有没有办法让空圆圈不消失,但让 PyPlot 自动选择每个系列的颜色? (3认同)
  • 当您使用`plot`而不是`scatter`时,这很有用 (2认同)

den*_*nis 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 )
Run Code Online (Sandbox Code Playgroud)

替代文字

(图片中的圆圈被压扁为椭圆形,因为imshow aspect="auto").


Aro*_*roc 7

根据 Gary Kerr 的示例,按照此处的建议,可以使用以下代码创建与指定值相关的空圆圈:

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()
Run Code Online (Sandbox Code Playgroud)


Sal*_*ino 6

在 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),因为如果标记相对较小且边缘宽度较厚,即使标记未填充,标记也会看起来像填充。

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

myplot.errorbar(x=myXval, y=myYval, yerr=myYerrVal, fmt='o', fillstyle='none', ecolor='blue',  mec='blue')
Run Code Online (Sandbox Code Playgroud)

  • 重新控制线宽的好技巧!在 **matplotlib 3.3.1** 中,“markeredgewidth”参数称为“linewidths”。请参阅[文档](https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter)中的最新信息 (2认同)