Matplotlib透明填充图像

tyl*_*ler 2 python image 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()
Run Code Online (Sandbox Code Playgroud)

代码我遇到问题:

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()
Run Code Online (Sandbox Code Playgroud)

所以,我想用self.ui.canvas2.axes中的imshow()'ed图片放置一个半透明的多边形来替换注释.

有什么建议??

谢谢,

tylerthemiler

Dav*_*ker 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()
Run Code Online (Sandbox Code Playgroud)

如果您在堆叠对象时遇到问题,还可以显式设置zorder参数.