2011-12-29 156 views
2

我試圖使用matplotlib在畫布上的圖像繪製透明多邊形:我有麻煩Matplotlib在圖像透明填充

def update_figure(self, dataOverride = None): 
    if self.data is not None or dataOverride is not none: 
     FigureCanvas.updateGeometry(self) 
     self.axes.clear() 
     if dataOverride is not None: 
      self.axes.imshow(dataOverride, cmap = self.getColorMap()) 
     else: 
      self.axes.imshow(self.data, cmap = self.getColorMap()) 
    self.draw() 

代碼:

從帆布類中的代碼

def renderPoly(self, pointListX, pointListY): 

    #Adds in picture to self.ui.canvas2.axes 
    self.ui.canvas2.update_figure() 

    #Code that draws polygon with len(pointListX) points 
    #with the points at pointListX and pointListY over the 
    #current image in self.ui.canvas2.update_figure() 

所以,我想用一些東西來替換註釋,這些註釋會在self.ui.canvas2.axes中將imshow()的ed圖片放在半透明多邊形上。

有什麼建議?

感謝,

tylerthemiler

+0

在這裏他們的問題通常不會寫'Thanks,Tylerthemiler',這與論壇有點不同。這個想法是,這些問題應該經得起時間的考驗,就像其他人一樣。 – Yann 2011-12-29 10:48:39

+1

你的問題很好地佈置,給你一些想法,以及你有什麼問題。但是到目前爲止,你還沒有解釋你所嘗試過的。你會得到什麼樣的錯誤?你嘗試過什麼樣的多邊形例子?如果你還沒有嘗試過一個例子,並且找不到一個簡單的(谷歌)搜索,那麼告訴我們:我找不到任何向軸添加多邊形的好例子。這將有助於答案人意識到你甚至沒有一個合適的例子。 – Yann 2011-12-29 10:53:55

回答

7

我不完全理解你的代碼(請提供了一個有效的例子),但下面的代碼把一個多邊形通過一個imshow圖像:

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

data = np.random.rand(4, 4) 
plt.imshow(data) 

pointListX = (0, 2, 1) 
pointListY = (0, 1, 3) 
xyList = list(zip(pointListX, pointListY)) # `list` not necessary for python2 
p = Polygon(xyList, alpha=0.2) 
plt.gca().add_artist(p) 

plt.show() 

如果您在堆疊對象時遇到問題,您還可以明確設置參數zorder

+0

我認爲這將是關鍵補充說,「多邊形」單位必須以數據單位,即您的隨機數據從每個軸上的0到3,以便這個多邊形的值的範圍。 – Yann 2011-12-29 11:12:24

+0

同樣在這個特定的問題中,將使用'zip'來將多邊形所需的Nx2數組從「pointListX」和「pointListY'數組中取出。 – Yann 2011-12-29 11:13:48

+0

我在解決方案中添加了'zip'命令。謝謝@Yann! – 2011-12-29 12:10:04