简体   繁体   English

如何在 Python 中绘制带有空圆圈的散点图?

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

In Python, with Matplotlib, how can a scatter plot with empty circles be plotted?在 Python 中,使用 Matplotlib,如何绘制带有圆圈的散点图? The goal is to draw empty circles around some of the colored disks already plotted by scatter() , so as to highlight them, ideally without having to redraw the colored circles.目标是在scatter()已经绘制的一些彩色圆盘周围绘制空圆圈,以便突出显示它们,理想情况下不必重新绘制彩色圆圈。

I tried facecolors=None , to no avail.我试过facecolors=None ,但无济于事。

From the documentation for scatter:来自 scatter 的文档

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

Try the following:请尝试以下操作:

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

示例图像

Note: For other types of plots see this post on the use of markeredgecolor and markerfacecolor .注意:对于其他类型的地块看到这个帖子就用markeredgecolormarkerfacecolor

Would these work?这些有用吗?

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

示例图像

or using plot()或使用 plot()

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

示例图像

Here's another way: this adds a circle to the current axes, plot or image or whatever :这是另一种方式:这会向当前轴、绘图或图像或其他任何内容添加一个圆圈:

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 )

替代文字

(The circles in the picture get squashed to ellipses because imshow aspect="auto" ). (由于imshow aspect="auto" ,图片中的圆圈被压缩成椭圆形)。

In matplotlib 2.0 there is a parameter called fillstyle which allows better control on the way markers are filled.在 matplotlib 2.0 中有一个名为fillstyle的参数,它可以更好地控制标记的填充方式。 In my case I have used it with errorbars but it works for markers in general http://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.errorbar.html在我的情况下,我将它与错误栏一起使用,但它通常适用于标记http://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.errorbar.html

fillstyle accepts the following values: ['full' | fillstyle接受以下值: ['full' | 'left' | '左' | 'right' | '正确' | 'bottom' | '底部' | 'top' | '顶' | 'none'] '没有任何']

There are two important things to keep in mind when using fillstyle ,使用fillstyle时要记住两件重要的事情,

1) If mfc is set to any kind of value it will take priority, hence, if you did set fillstyle to 'none' it would not take effect. 1) 如果 mfc 设置为任何类型的值,它将优先,因此,如果您确实将 fillstyle 设置为 'none',它将不会生效。 So avoid using mfc in conjuntion with fillstyle所以避免将 mfc 与 fillstyle 结合使用

2) You might want to control the marker edge width (using markeredgewidth or mew ) because if the marker is relatively small and the edge width is thick, the markers will look like filled even though they are not. 2) 您可能想要控制标记边缘宽度(使用markeredgewidthmew ),因为如果标记相对较小且边缘宽度较厚,即使标记没有填充,它们也会看起来像填充。

Following is an example using errorbars:以下是使用错误栏的示例:

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

Basend on the example of Gary Kerr and as proposed here one may create empty circles related to specified values with following code:基于 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()

So I assume you want to highlight some points that fit a certain criteria.所以我假设你想突出一些符合特定标准的要点。 You can use Prelude's command to do a second scatter plot of the hightlighted points with an empty circle and a first call to plot all the points.您可以使用 Prelude 的命令对突出显示的点绘制第二个散点图,其中包含一个空圆圈和第一次调用以绘制所有点。 Make sure the s paramter is sufficiently small for the larger empty circles to enclose the smaller filled ones.确保 s 参数足够小,以便较大的空圆圈包围较小的填充圆圈。

The other option is to not use scatter and draw the patches individually using the circle/ellipse command.另一个选项是不使用散点图,而是使用圆/椭圆命令单独绘制补丁。 These are in matplotlib.patches, here is some sample code on how to draw circles rectangles etc.这些在 matplotlib.patches 中, 这里是一些关于如何绘制圆形矩形等的示例代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM