2016-01-20 13 views
7

ich habe float x/y-arrays, die kreis zentrieren.zeichnungskreise auf bild mit matplotlib und 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() 

Ich möchte Kreise auf meinem Bild anzeigen, indem Sie diese Zentren verwenden. Wie kann ich das erreichen?

+2

Mögliche Duplikat [zeichnen einen Kreis mit pyplot] (http://stackoverflow.com/questions/9215658/plot-a-circle-with-pyplot) – kazemakase

+0

bist du sicher? .. – orkan

+0

Ganz so. Antworten auf diese Frage zeigen, wie man Kreise zeichnet, was genau das ist, wonach du gefragt hast :) – kazemakase

Antwort

10

Sie können dies mit dem Patch matplotlib.patches.Circle tun.

Für Ihr Beispiel müssen wir die X- und Y-Arrays durchlaufen und dann für jede Koordinate ein Kreispatch erstellen.

Hier ist ein Beispiel Kreise oben auf ein Bild platzieren (vom 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

danke großes Beispiel – orkan

+0

es Kreise in der Handlung ist, nicht auf dem Bild. img würde nicht geändert werden –

+0

@andrewmatuk: was meinst du? Wenn Sie das Bild mit 'savefig' speichern, werden die Kreise ebenfalls gespeichert – tom

Verwandte Themen