2016-01-20 16 views
7

私はを保持するfloat 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

matplotlib.cbookから)プロットはpyplot有する円】画像の上に円を配置する例を示します/ 9215658/plot-a-circle-with-pyplot) – kazemakase

+0

あなたは本当ですか?.. – orkan

+0

まあまあです。その質問への答えは、まさにあなたが求めたものである円を描く方法を示しています:) – kazemakase

答えて

10

これはmatplotlib.patches.Circleパッチで実行できます。

例では、XとYの配列をループし、座標ごとに円パッチを作成する必要があります。ここ

は可能重複http://stackoverflow.com/questions(

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