2014-12-05 103 views
2

我想繪製散點圖上隨機生成的一些磁盤的位置,並查看磁盤是否相互「連接」。爲此,我需要設置固定/鏈接到軸刻度的每個磁盤的半徑。
plt.scatter函數中的's'參數使用了點,所以尺寸相對於軸不固定。如果我動態地放大繪圖,則散點圖標記大小在繪圖中保持不變,並且不隨軸放大。
如何設置半徑使其具有確定的值(相對於軸)?如何在散點圖上設置圓形標記的固定/靜態大小?

回答

3

而不是使用plt.scatter,我建議使用patches.Circle繪製圖(類似於this answer)。這些補丁保持固定的大小,這樣就可以動態地放大檢查「關係」:

import matplotlib.pyplot as plt 
from matplotlib.patches import Circle # for simplified usage, import this patch 

# set up some x,y coordinates and radii 
x = [1.0, 2.0, 4.0] 
y = [1.0, 2.0, 2.0] 
r = [1/(2.0**0.5), 1/(2.0**0.5), 0.25] 

fig = plt.figure() 

# initialize axis, important: set the aspect ratio to equal 
ax = fig.add_subplot(111, aspect='equal') 

# define axis limits for all patches to show 
ax.axis([min(x)-1., max(x)+1., min(y)-1., max(y)+1.]) 

# loop through all triplets of x-,y-coordinates and radius and 
# plot a circle for each: 
for x, y, r in zip(x, y, r): 
    ax.add_artist(Circle(xy=(x, y), 
        radius=r)) 

plt.show() 

情節此生成這個樣子的:

initial plot

使用從縮放選項圖表窗口,可以得到這樣的情節:

zoom

這版本放大一直保持噸他原來的圈大小,所以'連接'可以看出。


如果你想改變的圓圈是透明的,patches.Circle需要一個alpha作爲參數。只要確保你與呼叫插入到Circle沒有add_artist

ax.add_artist(Circle(xy=(x, y), 
       radius=r, 
       alpha=0.5)) 
+0

是可以設置的阿爾法值的圓,所以這是一個有點透明的,而當它們重疊,這部分有一個變暗的顏色? – Physicist 2014-12-12 16:06:23

+0

@Physicist查看編輯。調用時可以設置圓的alpha值。 – Schorsch 2014-12-12 16:19:23