Matplotlib EPS导出忽略插值='最近'

时间:2012-04-17 02:58:41

标签: python matplotlib

我想将没有插值的热图导出为EPS文件。将imshow()interpolation='nearest'一起使用,如果我导出为PDF(或PNG或SVG),则图像看起来正确,没有插值。但如果我导出为EPS,它似乎忽略了interpolation='nearest'

有没有办法在没有插值的情况下导出为EPS?

以下示例代码演示了导出文件类型的差异:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.rand(4,4)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(data,interpolation='nearest')

fig.savefig('test.eps')
fig.savefig('test.pdf')
fig.savefig('test.png')

1 个答案:

答案 0 :(得分:2)

较新版本的matplotlib接受参数interpolation='none',这可能会产生预期的效果。对于您的代码,这将是

import numpy as np
import matplotlib.pyplot as plt

data = np.random.rand(4,4)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(data,interpolation='none')

fig.savefig('test.eps')
fig.savefig('test.pdf')
fig.savefig('test.png')