2016-08-08 56 views
1

Ich mache Illustrationen für mein Papier in Python mit matplotlib Bibliothek. In dieser Illustration habe ich viele Linien, Polygone, Kreise etc. Aber dann möchte ich auch ein .png Bild von außen einfügen.Einfügen/Anpassen von Png in Plot [Matplotlib]

Hier ist, was ich versuche, so weit zu tun:

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

fig, ax = plt.subplots() 

plt.tick_params(axis='x', which='both', bottom='off', top='off', labelbottom='off') 
ax.axis('off') 

# drawing circle 
ax.add_patch(
       plt.Circle((0, 0), 0.5, color = 'black') 
      ) 
# drawing polygon 
ax.add_patch(
     Polygon(
      [[0,0], [20, 15], [20, 40]], 
      closed=True, fill=False, lw=1) 
     ) 
# importing image 
im = plt.imread("frame.png") 
# defining image position/size 
rect = 0.5, 0.4, 0.4, 0.4 # What should these values be? 
newax = fig.add_axes(rect, anchor='NE', zorder=1) 
newax.imshow(im) 
newax.axis('off') 

ax.set_aspect(1) 
ax.set_xlim(0, 60) 
ax.set_ylim(0, 40) 
plt.show() 

Die Frage ist also, wie kann ich feststellen, die Werte für rect = 0.5, 0.4, 0.4, 0.4? Zum Beispiel möchte ich, dass die untere linke Ecke meines .png an dem Punkt [20, 15] liegt und ich möchte, dass seine Höhe 25 ist.

Dies ist das resultierende Bild:

nonadjusted image

Aber ich will diese Dummy-Rahmen meiner Polygonpunkte eingestellt werden, wie dies (dieses in Photoshop angepasst ist):

adjusted in photoshop

PS Hier ist die link mit der frame.png zu experimentieren.

Antwort

1

Können Sie Ihre Linien und das Bild auf der gleichen Achse zeichnen? dafür, verwenden Sie die extent Schlüssel in plt.imshow()

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


im='d:/frame.png' 
img=plt.imread(im) 
fig, ax = plt.subplots() 

frame_height=25 
x_start=20 
y_start=15 
ax.imshow(img,extent=[x_start,x_start+frame_height,y_start,y_start+frame_height]) 

ax.add_patch(
     Polygon(
      [[0,0], [20, 15], [20, 40]], 
      closed=True, fill=False, lw=1) 
     ) 
ax.set_xlim(0, 60) 
ax.set_ylim(0, 40) 
plt.show() 
+0

Danke, das für mich gearbeitet. Hätte nicht geglaubt, dass die Antwort so einfach wäre. Entschuldigung, wenn die Frage zu dumm war. –