2016-01-20 104 views
7

我有浮動 x/y陣列舉行中心。圖像與matplotlib和numpy的圖圈

import matplotlib.pylab as plt 
import numpy as np 
npX = np.asarray(X) 
npY = np.asarray(Y) 
plt.imshow(img) 
// TO-DO 
plt.show() 

我想通過使用此中心在我的圖像上顯示圈子。我怎樣才能做到這一點?

+2

的可能的複製[繪製與pyplot圓(http://stackoverflow.com/questions/9215658/plot-a-circle-with-pyplot) – kazemakase

+0

你確定嗎?.. – orkan

+0

確實如此。該問題的答案顯示瞭如何繪製圓,這正是你所要求的:) – kazemakase

回答

10

你可以用matplotlib.patches.Circle補丁來做到這一點。

對於您的示例,我們需要遍歷X和Y數組,然後爲每個座標創建一個圓形補丁。

這裏的放置圓圈上的圖像的頂部(從matplotlib.cbook)的示例

import matplotlib.pyplot as plt 
import numpy as np 
from matplotlib.patches import Circle 

# Get an example image 
import matplotlib.cbook as cbook 
image_file = cbook.get_sample_data('grace_hopper.png') 
img = plt.imread(image_file) 

# Make some example data 
x = np.random.rand(5)*img.shape[1] 
y = np.random.rand(5)*img.shape[0] 

# Create a figure. Equal aspect so circles look circular 
fig,ax = plt.subplots(1) 
ax.set_aspect('equal') 

# Show the image 
ax.imshow(img) 

# Now, loop through coord arrays, and create a circle at each x,y pair 
for xx,yy in zip(x,y): 
    circ = Circle((xx,yy),50) 
    ax.add_patch(circ) 

# Show the image 
plt.show() 

enter image description here

+0

謝謝你很好的例子 – orkan

+0

它是圓形的情節,而不是圖像。 img不會被改變 –

+0

@andrewmatuk:你是什麼意思?如果使用'savefig'保存圖像,圓圈也會被保存 – tom