代码之家  ›  专栏  ›  技术社区  ›  Eric O. Lebigot

如何在Python中使用空圆绘制散点图?

  •  127
  • Eric O. Lebigot  · 技术社区  · 14 年前

    在Python中,使用Matplotlib,如何使用 画圆?目标是画一个空圆圈 一些 scatter() ,以便突出显示它们,理想情况下无需重新绘制彩色圆圈。

    facecolors=None ,但毫无用处。

    5 回复  |  直到 6 年前
        1
  •  215
  •   Community CDub    8 年前

    documentation 对于分散:

    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
    

    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

    注: 有关其他类型的绘图,请参见 this post markeredgecolor markerfacecolor .

        2
  •  61
  •   endolith    12 年前

    这些有用吗?

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

    example image

    或者使用plot()

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

    example image

        3
  •  14
  •   denis    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" ).

        4
  •  4
  •   Salvatore Cosentino    7 年前

    在matplotlib 2.0中有一个名为 fillstyle 这样可以更好地控制标记填充的方式。 http://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.errorbar.html

    填充样式

    使用时要记住两件重要的事情 填充样式

    1) 如果将mfc设置为任何类型的值,它都将具有优先权,因此,如果将fillstyle设置为“none”,它将不会生效。 所以避免在fillstyle中同时使用mfc

    2) 您可能需要控制标记边缘宽度(使用 markeredgewidth mew )因为如果标记相对较小且边缘宽度较厚,则标记看起来像填充的,即使它们不是填充的。

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

    myplot.errorbar(x=myXval, y=myYval, yerr=myYerrVal, fmt='o', fillstyle='none', ecolor='blue',  mec='blue')
    
        5
  •  1
  •   whatnick    14 年前

    另一个选项是不使用“散布”并使用“圆/椭圆”命令分别绘制面片。这些在matplotlib.patches中, here 是一些关于如何绘制圆、矩形等的示例代码。

        6
  •  -1
  •   Aroc    5 年前

    here 可以使用以下代码创建与指定值相关的空圆:

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