3

I want to export a heat map without interpolation as an EPS file. Using imshow() with interpolation='nearest', if I export as PDF (or PNG or SVG) the image looks right, with no interpolation. But if I export as EPS, it seems to ignore interpolation='nearest'.

Is there any way to export as EPS without interpolation?

Here is example code demonstrating differences in the export filetype:

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')
johndmurray
  • 33
  • 1
  • 3
  • I am unable to reproduce any error. When I copy and paste the code and then examine the saved figures, they all look identical. I am using matplotlib version 0.99.3 on Ubuntu 11.04 with Python 2.7.1. – ely Apr 17 '12 at 03:04
  • I'm using matplotlib 1.0.1 on Mac OSX with Python 2.7.2. Could this be due to OS-specific differences in the EPS backend? – johndmurray Apr 17 '12 at 03:10
  • It's certainly possible. One thing to try is just using a command line utility like imagemagick to convert the PDF to EPS after saving. It's a pain, but may work. If it works, you can even use the `os` module and issue the command from your Python script each time. – ely Apr 17 '12 at 03:31
  • Did you try using `interpolation='none'`, which should also give you smaller file size. There are some issues with backends (Agg vs Cairo) though, see http://stackoverflow.com/questions/7346254/matplotlib-backend-differences-between-agg-and-cairo – David Zwicker Apr 17 '12 at 08:15
  • @EMS: When I tried using ImageMagick's `convert` to go convert from PDF to EPS, I lost resolution (e.g. my axis labels are now blurry.) It looks like the process didn't preserve the vector elements but rasterized as an intermediate step. I'm not experienced with ImageMagick though. – johndmurray Apr 17 '12 at 12:04
  • @DavidZwicker: Yes, using `'interpolation=none'` fixes it -- now EPS output from `imshow()` is not interpolated. Thanks! I just had to update image.py to accept `'none'` as an option. (For where to update image.py, I used [https://github.com/matplotlib/matplotlib/commit/79ca159f75375b55ac5d11617bb49fc0f9ed3402](https://github.com/matplotlib/matplotlib/commit/79ca159f75375b55ac5d11617bb49fc0f9ed3402)) – johndmurray Apr 17 '12 at 12:09
  • I'm glad that it worked for you. I posted the hint as an answer to your question, so other people can appreciate it easier. – David Zwicker Apr 17 '12 at 12:14

1 Answers1

3

Newer version of matplotlib accept the argument interpolation='none', which might produced the desired effect. For your code, this would read

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')
David Zwicker
  • 23,581
  • 6
  • 62
  • 77