我正在使用imshow
生成叠加图,并使用matplotlib.backends.backend_pdf.PdfPages
将它们打印为多页pdf。我使用以下方法将叠加层中背景的 alpha 设置为 0:
edge = np.ma.masked_where(edge == 0, edge)
cmap = plt.cm.winter
cmap.set_bad('g', 0)
然后使用以下两个imshow命令将其覆盖到背景图像上:
plt.imshow(back[:, :, mid], cmap=plt.cm.gray, interpolation='nearest')
plt.imshow(edge[:, :, mid], cmap=cmap, interpolation='nearest', alpha=0.5)
如果我使用 matplotlib.pyplot.show()
,则会产生一个正确渲染的图像,但不是 matplotlib.backends.backend_pdf.PdfPages.savefig()
.具体来说,alpha 信息在 imshow
调用中被忽略,但在正确呈现的 cmap.set_bad
调用中不会被忽略。
谢谢,约瑟夫
对于任何遇到此问题的人...
解决方案是将 Matplotlib 更新到最新版本。我在 Ubuntu 12.04 存储库中使用了最新版本的 Matplotlib,但我落后了多个版本。该错误现已解决。谢谢!