Matplotlib透明填充图像

时间:2011-12-29 03:42:58

标签: python image matplotlib

我正在尝试使用matplotlib在画布上的图像上绘制透明多边形:

canvas类的代码:

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

1 个答案:

答案 0 :(得分: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参数。