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

发布于 2024-10-01 08:49:21 字数 183 浏览 2 评论 0原文

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

我尝试了facecolors=None,但没有成功。

In Python, with Matplotlib, how can a scatter plot with empty circles be plotted? 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.

I tried facecolors=None, to no avail.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

暗地喜欢 2024-10-08 08:49:21

分散文档

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

注意:有关其他类型的绘图,请参阅这篇文章介绍了 markeredgecolormarkerfacecolor< /代码>。

From the documentation for 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()

example image

Note: For other types of plots see this post on the use of markeredgecolor and markerfacecolor.

彩扇题诗 2024-10-08 08:49:21

这些有用吗?

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

示例图像

Would these work?

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

example image

or using plot()

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

example image

莳間冲淡了誓言ζ 2024-10-08 08:49:21

这是另一种方法:这会向当前轴、绘图或图像或其他任何内容添加一个圆圈:

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

(图片被压缩为椭圆,因为 imshowspect="auto" )。

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 )

alt text

(The circles in the picture get squashed to ellipses because imshow aspect="auto" ).

○愚か者の日 2024-10-08 08:49:21

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

Basend on the example of Gary Kerr and as proposed here one may create empty circles related to specified values with following code:

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()
羁客 2024-10-08 08:49:21

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

fillstyle 接受以下值: '左' | '对' | '底部' | '顶部' | 'none']

使用 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')

In matplotlib 2.0 there is a parameter called fillstyle
which 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

fillstyle accepts the following values: [‘full’ | ‘left’ | ‘right’ | ‘bottom’ | ‘top’ | ‘none’]

There are two important things to keep in mind when using 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

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.

Following is an example using errorbars:

myplot.errorbar(x=myXval, y=myYval, yerr=myYerrVal, fmt='o', fillstyle='none', ecolor='blue',  mec='blue')
赠我空喜 2024-10-08 08:49:21

所以我假设您想强调一些符合特定标准的要点。您可以使用 Prelude 的命令使用空圆圈对突出显示的点进行第二次散点图,并第一次调用以绘制所有点。确保 s 参数足够小,以便较大的空心圆能够包围较小的实心圆。

另一种选择是不使用散点图并使用圆/椭圆命令单独绘制面片。这些位于 matplotlib.patches 中,这里是一些有关如何绘制圆圈的示例代码矩形等

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文