2013-10-03 160 views
5

我試圖在Matplotlib中繪製一個2D圖像(從PNG導入)並將其旋轉任意角度。我想創建一個簡單的動畫,顯示一個對象隨着時間的旋轉,但現在我只是想旋轉圖像。我試過幾個變化在下面的代碼,但沒有成功:在Matplotlib中旋轉圖像

import matplotlib.pyplot as plt 
import matplotlib.transforms as tr 
import matplotlib.cbook as cbook 

image_file = cbook.get_sample_data('ada.png') 
image = plt.imread(image_file) 

imAx = plt.imshow(image) 
rot = tr.Affine2D().rotate_deg(30) 
imAx.set_transform(imAx.get_transform()+rot) 

plt.axis('off') # clear x- and y-axes 
plt.show() 

我敢肯定,我失去了一些東西,但我一直無法從matplotlib文檔和示例看着辦吧。

謝謝!

回答

7

this代碼看看:

import scipy 
from scipy import ndimage 
import matplotlib.pyplot as plt 
import numpy as np 

lena = scipy.misc.lena() 
lx, ly = lena.shape 
# Copping 
crop_lena = lena[lx/4:-lx/4, ly/4:-ly/4] 
# up <-> down flip 
flip_ud_lena = np.flipud(lena) 
# rotation 
rotate_lena = ndimage.rotate(lena, 45) 
rotate_lena_noreshape = ndimage.rotate(lena, 45, reshape=False) 

plt.figure(figsize=(12.5, 2.5)) 


plt.subplot(151) 
plt.imshow(lena, cmap=plt.cm.gray) 
plt.axis('off') 
plt.subplot(152) 
plt.imshow(crop_lena, cmap=plt.cm.gray) 
plt.axis('off') 
plt.subplot(153) 
plt.imshow(flip_ud_lena, cmap=plt.cm.gray) 
plt.axis('off') 
plt.subplot(154) 
plt.imshow(rotate_lena, cmap=plt.cm.gray) 
plt.axis('off') 
plt.subplot(155) 
plt.imshow(rotate_lena_noreshape, cmap=plt.cm.gray) 
plt.axis('off') 

plt.subplots_adjust(wspace=0.02, hspace=0.3, top=1, bottom=0.1, left=0, 
        right=1) 

plt.show() 
+0

貌似ndimage.rotate()是我是後。謝謝! – user2844064