2010-11-10 159 views
88

在Python中,使用Matplotlib,如何繪製一個散點圖空的圈?我們的目標是繪製的空心圓,其中一些已經被scatter()繪製出來,以便突出顯示它們,理想情況下無需重畫彩色圓圈。如何在Python中使用空圈來繪製散點圖?

回答

134

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對使用​​markeredgecolormarkerfacecolor

+3

謝謝!問題是,更常見的(在Python中)'facecolors = None'不起作用,這使我絆倒了。 – EOL 2015-03-01 06:49:20

+8

非常有幫助。這是'markerfacecolor ='none'',這是現在的樣子。 – 2015-05-28 03:16:55

+0

我有我的matplotlib風格的方式,將邊緣寬度設置爲零。如果您沒有看到有'markerfacecolor ='none''的任何標記,請嘗試添加'markeredgewidth = 1.0' – oLas 2017-09-19 19:44:24

38

這些工作?

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

example image

,或者使用曲線圖()

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

example image

1

所以我假設你想突出一些符合某些標準的要點。您可以使用Prelude的命令以空圓圈和第一次調用繪製高亮點的第二個散點圖來繪製所有點。確保s參數對於較大的空圓來說足夠小,以包圍較小的實心圓。

另一種選擇是不使用scatter並使用circle/ellipse命令分別繪製補丁。這些都是matplotlib.patches,here是如何繪製圓矩形等

12

這裏是一些示例代碼的另一種方式:這增加了一圈當前座標,情節或圖像或其他:

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",圖片中的圓被壓扁以省略)。

+0

+1:有趣! – EOL 2010-11-23 16:27:37

2

在matplotlib 2.0中有一個叫做fillstyle 的參數,它允許更好地控制標記填充的方式。 在我來說,我已經與errorbars使用它,但它一般 http://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.errorbar.html

fillstyle工程標記接受以下值:「全」 | 'left'| 'right'| 'bottom'| 'top'| 「無」]

使用fillstyle時,有要記住兩個重要的事情,

1)如果MFC設置爲任何類型的值,它會採取優先,因此,如果您沒有設置填充樣式爲「沒有'它不會生效。 因此要避免使用MFC的連詞與填充樣式

2)您可能希望控制標記邊緣寬度(使用markeredgewidthmew),因爲如果標記比較小,邊寬很厚,標記看起來像填充即使它們不是。

下面是使用errorbars一個例子:

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