简体   繁体   English

使用imshow时,Matplotlib PDF输出不尊重zorder

[英]Matplotlib PDF output does not respect zorder when using imshow

I am plotting multiple images in matplotlib using imshow as well as some vector data between them using the zorder keyword. 我正在使用imshow在matplotlib中绘制多个图像,以及使用zorder关键字在它们之间绘制一些矢量数据。

Minimal example: 最小的例子:

import numpy as np
import matplotlib.pyplot as plt

img = np.arange(100).reshape((10,10))
plt.imshow(img, extent = [0.25, 0.75, 0.25, 0.75], zorder = 10)
plt.imshow(img, extent = [0.1, 0.9, 0.1, 0.9], zorder = 1)
plt.plot([0, 1], [0, 1], color = 'black', zorder = 5)
plt.axis([0, 1, 0, 1])

plt.savefig('img.png')

When exporting to PNG, the output is as expected. 导出到PNG时,输出符合预期。 However, when saving to PDF (or EPS, SVG, ...), the zorder is not respected (the line is plotted over both images). 但是,当保存为PDF(或EPS,SVG,...)时,不遵守zorder(在两个图像上绘制线条)。 It seems the two images are combined into a single one when they are exported. 这两个图像在导出时似乎合并为一个。 Saving the images as vectors instead of rasters by using pcolormesh instead of imshow works, but the resulting PDFs are huge when plotting large images. 使用pcolormesh而不是imshow将图像保存为矢量而不是栅格,但是在绘制大图像时生成的PDF很大。 Is there a way to make this work with imshow ? 有没有办法让这个工作与imshow

This sounds like you should raise a bug report here - I did find a similar issue here but your problem looks different enough to merit a separate ticket. 这听起来像你应该在这里提出一个错误报告 - 我确实在这里找到了类似的问题但你的问题看起来不同,值得一个单独的票。

What you could do is split your diagonal line manually into two sections rather than relying on the z order to hide it for you. 你可以做的是手动将对角线分成两部分,而不是依靠z顺序为你隐藏它。 Something like: 就像是:

plt.plot([0, 0.25], [0, 0.25], color = 'black', zorder = 5)
plt.plot([0.75, 1], [0.75, 1], color = 'black', zorder = 5)

Replacing your: 替换你的:

plt.plot([0, 1], [0, 1], color = 'black', zorder = 5)

Update: 更新:

Given that your real world plot is probably a lot more complex than a straight line you could possibly take your top layer, your plot and find a mask/boundary where the two intercept the remove, (automatically), the portions of your lines that need to be hidden. 鉴于你的真实世界情节可能比直线复杂得多,你可以采取你的顶层,你的情节并找到一个掩模/边界,其中两个拦截移除,(自动),你需要的线条部分被隐藏

The other option would be to produce all of your layers as separate images and then layer them up using PIL but since the PNG output works OK you would probably be better off outputting as PNG and then converting to PDF or embedding as PDF as a work around. 另一种选择是将所有图层生成为单独的图像,然后使用PIL对它们进行分层,但由于PNG输出正常工作,因此最好输出为PNG,然后转换为PDF或嵌入PDF作为解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 3D Matplotlib 中的参数曲线不遵守 zorder。 解决方法? - 3D parametric curve in Matplotlib does not respect zorder. Workaround? 使用`savefig'时,缺少`matplotlib.imshow`在PDF中插入的图像 - Missing images inserted by `matplotlib.imshow` in PDF when using `savefig` matplotlib 中的“alpha”如何与 zorder 相关,如何对称地混合 colors? - How does 'alpha' in matplotlib work with respect to zorder, and how can one blend colors symmetrically? 使用带有 imshow matplotlib 的网格时删除刻度 - Removing ticks when using grid with imshow matplotlib 使用插值和 alpha 将 imshow 保存为 pdf 时的 Matplotlib 问题 - Matplotlib issue when saving imshow as pdf with interpolation and alpha 设置 output 范围 matplotlib - imshow - Set output range of matplotlib - imshow AttributeError: 'numpy.int32' object 在 matplotlib 中使用 FuncAnimation 时没有属性 'get_zorder' - AttributeError: 'numpy.int32' object has no attribute 'get_zorder' when using FuncAnimation in matplotlib 如何在matplotlib中使用imshow绘制netcdf时增加对比度? - How to increase contrast when plotting a netcdf using imshow in matplotlib? 使用matplotlib.pyplot.imshow()时如何确定颜色? - How to determine the colours when using matplotlib.pyplot.imshow()? 使用 matplotlib plt.imshow() 时从像素更改为微米 - Change from pixel to micron when using matplotlib plt.imshow()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM