Python Matplotlib图像上的透明填充
我正在尝试使用matplotlib在画布上的图像上绘制透明多边形: canvas类中的代码:Python Matplotlib图像上的透明填充,python,image,matplotlib,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
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()图片上方放置半透明多边形的内容
有什么建议吗
谢谢
tylerthemiler我不完全理解您的代码(请提供一个完整的工作示例),但以下代码将多边形放置在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到3,因此这就是多边形的值范围。在这个特定问题中,zip
将用于从pointListX
和pointListY
数组中提取多边形所需的Nx2数组。zip
在python 3中不起作用。可能是因为它返回一个迭代器,而不是在python 2中输出的列表。