如何在 Python 中绘制带有空圆圈的散点图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4143502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do a scatter plot with empty circles in Python?
提问by Eric O Lebigot
In Python, with Matplotlib, how can a scatter plot with emptycircles be plotted? The goal is to draw empty circles around someof the colored disks already plotted by scatter(), so as to highlight them, ideally without having to redraw the colored circles.
在 Python 中,使用 Matplotlib,如何绘制带有空圆圈的散点图?目标是在一些已经由 绘制的彩色圆盘周围绘制空圆圈scatter(),以便突出显示它们,理想情况下无需重新绘制彩色圆圈。
I tried facecolors=None, to no avail.
我试过了facecolors=None,无济于事。
采纳答案by Gary Kerr
From the documentationfor 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 poston the use of markeredgecolorand markerfacecolor.
注意:对于其他类型的地块看到这个帖子的使用markeredgecolor和markerfacecolor。
回答by G?khan Sever
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')


回答by whatnick
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. Make sure the s paramter is sufficiently small for the larger empty circles to enclose the smaller filled ones.
所以我假设你想突出一些符合特定标准的要点。您可以使用 Prelude 的命令对突出显示的点绘制第二个散点图,其中包含一个空圆圈和第一次调用以绘制所有点。确保 s 参数足够小,以便较大的空圆圈包围较小的填充圆圈。
The other option is to not use scatter and draw the patches individually using the circle/ellipse command. These are in matplotlib.patches, hereis some sample code on how to draw circles rectangles etc.
另一个选项是不使用散点图,而是使用圆/椭圆命令单独绘制补丁。这些在 matplotlib.patches 中,这里是一些关于如何绘制圆形矩形等的示例代码。
回答by denis
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")。
回答by Salvatore Cosentino
In matplotlib 2.0 there is a parameter called fillstylewhich allows better control on the way markers are filled.
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
在 matplotlib 2.0 中有一个名为的参数fillstyle,它可以更好地控制标记的填充方式。在我的情况下,我将它与错误栏一起使用,但它通常适用于标记
http://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.errorbar.html
fillstyleaccepts the following values: [‘full' | ‘left' | ‘right' | ‘bottom' | ‘top' | ‘none']
fillstyle接受以下值: ['full' | '左' | '正确' | '底部' | '顶' | '没有任何']
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. So avoid using mfc in conjuntion with fillstyle
1) 如果 mfc 设置为任何类型的值,它将优先,因此,如果您确实将 fillstyle 设置为 'none',它将不会生效。所以避免将 mfc 与 fillstyle 结合使用
2) You might want to control the marker edge width (using markeredgewidthor 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) 您可能想要控制标记边缘宽度(使用markeredgewidth或mew),因为如果标记相对较小且边缘宽度较厚,则即使未填充,标记也会看起来像填充。
Following is an example using errorbars:
以下是使用错误栏的示例:
myplot.errorbar(x=myXval, y=myYval, yerr=myYerrVal, fmt='o', fillstyle='none', ecolor='blue', mec='blue')
回答by Aroc
Basend on the example of Gary Kerr and as proposed hereone 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()

