将matplotlib填充轮廓图保存为.pdf或.eps时的别名 [英] Aliasing when saving matplotlib filled contour plot to .pdf or .eps

查看:251
本文介绍了将matplotlib填充轮廓图保存为.pdf或.eps时的别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib.pyplot.contourf()函数生成一个归档的轮廓图.该函数调用中的参数为:

I'm generating a filed contour plot with the matplotlib.pyplot.contourf() function. The arguments in the call to the function are:

contourf(xvec,xvec,w,levels,cmap=matplotlib.cm.jet)

其中

xvec = numpy.linspace(-3.,3.,50)
levels = numpy.linspace(-0.01,0.25,100)

w是我的数据.

生成的图在屏幕上看起来还不错,但是当我使用对matplotlib.pyplot.savefig()的调用保存为pdf时,生成的pdf有很多别名(我想就是这样).对savefig的调用只是savefig('filename.pdf').我尝试使用dpi参数,但是没有运气.调用matplotlib.get_backend()会弹出"TkAgg".

The resulting plot looks pretty good on screen, but when I save to pdf using a call to matplotlib.pyplot.savefig(), the resulting pdf has a lot of aliasing (I think that is what it is) going on. The call to savefig is simply savefig('filename.pdf'). I have tried using the dpi argument, but without luck. A call to matplotlib.get_backend() spits out 'TkAgg'.

我将附加一个另存为pdf的图形,与一个另存为png的图形(类似于在屏幕上显示的图形)进行比较,以演示该问题:

I will attach a figure saved as pdf, compared to a figure saved as png (similar to what it looks like on screen) to demonstrate the problem:

png不带别名: https://dl.dropbox.com/u/6042643/wigner_g0.17.png

pdf: https://dl.dropbox.com/u/6042643/wigner_g0.17.pdf

请告知我是否还有其他细节可以帮助您给出答案.我应该提一下,另存为.eps会产生与另存为pdf相似的不良结果.但是pdf文件更清楚地显示了问题.我的目标是最终获得生产质量的.eps文件,我可以将其附加到乳胶文档上,以科学论文的形式发表.如果可以找到可以令人满意的结果的方式,我会对围绕一种格式保存的工作感到满意,然后将其转换.

Please let me know if there are any other details I could give to help you give an answer. I should mention that saving as .eps gives similar bad results as saving to pdf. But the pdf shows the problem even clearer. My goal is to end up with a production quality .eps that I can attach to a latex document to be published as a scientific paper. I would be happy with some kind of work around where I save in one format, then convert it, if I can find a way that gives satisfying results.

最好

Arne

推荐答案

使用@pelson的有用答案一段时间后,我终于找到了解决这个长期存在的问题的适当方法(当前在Matplotlib 2.0中),不需要多次调用轮廓或栅格化图形.

After using the useful answer by @pelson for a while, I finally found a proper solution to this long-standing problem (currently in Matplotlib 2.0), which does not require multiple calls to contour or rasterizing the figure.

我会在此处引用我的原始答案,以获取更广泛的解释和示例.

I refer to my original answer here for a more extensive explanation and examples.

总而言之,该解决方案由以下几行组成:

In summary, the solution consists of the following lines:

cnt = plt.contourf(x, y, z)

for c in cnt.collections:
    c.set_edgecolor("face")

plt.savefig('test.pdf')

这篇关于将matplotlib填充轮廓图保存为.pdf或.eps时的别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆