[英]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.