简体   繁体   English

matplotlib:保存的 imshow pdf 看起来与绘图窗口不同

[英]matplotlib: saved imshow pdf looks different from the plot window

The following figure was plotted using imshow in matplotlib with option interpolation='none' :下图是在matplotlib中使用imshow绘制的,带有选项interpolation='none'在此处输入图像描述 However, after I saved it as a pdf file, the saved pdf file looks quite different:但是,在我将其保存为 pdf 文件后,保存的 pdf 文件看起来完全不同:在此处输入图像描述 The problem is: the blue patterns become very blurry.问题是:蓝色图案变得非常模糊。

My question is: How can I save a pdf figure that looks exactly like the plot window?我的问题是:如何保存看起来与绘图窗口完全相同的 pdf 图形?

I solved this problem by specifying the dpi in the savefig for filetype pdf.我通过在文件类型 pdf 的 savefig 中指定 dpi 解决了这个问题。 Even though i read online that dpi is not supposed to make a difference in the vector based pdf format in theory, it did solve the problem for me in practice.尽管我在网上读到 dpi 在理论上不应该对基于矢量的 pdf 格式产生影响,但它在实践中确实为我解决了这个问题。

plt.imshow(np.random.random((10,10)))
plt.savefig("test.pdf", dpi=300)

PDF format is a vector image format. PDF 格式是一种矢量图像格式。 This means it is upto the program you open it in to interpret how it should be drawn.这意味着它取决于您打开它的程序来解释它应该如何绘制。 This can have some benefits when you want to be able to arbitrarily zoom in and out of an image while keeping high quality.当您希望能够在保持高质量的同时任意放大和缩小图像时,这可能会带来一些好处。 However some programs can modify the image through anti-aliasing.但是有些程序可以通过抗锯齿来修改图像。

Your best bet for consistency is to use a pixel based image format.保持一致性的最佳选择是使用基于像素的图像格式。 I would suggest try saving it as a .png.我建议尝试将其保存为 .png。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM