在Python中,使用Matplotlib,如何绘制带有空圆圈的散点图?目标是在已经由scatter()绘制的一些彩色圆盘周围绘制空圆圈,以便突出显示它们,理想情况下无需重新绘制彩色圆圈。
我尝试了facecolors=None,但没有用。
在Python中,使用Matplotlib,如何绘制带有空圆圈的散点图?目标是在已经由scatter()绘制的一些彩色圆盘周围绘制空圆圈,以便突出显示它们,理想情况下无需重新绘制彩色圆圈。
我尝试了facecolors=None,但没有用。
当前回答
来自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
试试下面的方法:
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()
注意:对于其他类型的图,请参阅这篇关于使用markeredgecolor和markerfacecolor的文章。
其他回答
这些有用吗?
plt.scatter(np.random.randn(100), np.random.randn(100), facecolors='none')
或者使用plot()
plt.plot(np.random.randn(100), np.random.randn(100), 'o', mfc='none')
来自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
试试下面的方法:
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()
注意:对于其他类型的图,请参阅这篇关于使用markeredgecolor和markerfacecolor的文章。
我猜你想强调一些符合特定标准的点。您可以使用Prelude的命令对带有空圆圈的突出点进行第二个散点图,并使用第一个调用绘制所有点。确保s参数足够小,以使较大的空圆包围较小的填充圆。
另一种选择是不使用分散,而是使用圆形/椭圆命令单独绘制补丁。这些在matplotlib中。补丁,这里有一些关于如何绘制圆形、矩形等的示例代码。
这里有另一种方法:这将添加一个圆到当前轴,图或图像或其他东西:
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 )
(由于imshow aspect="auto",图片中的圆圈被压扁为椭圆)。
基于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()