3

I'm trying to save a streamline plot as an EPS, and then convert it to PDF using epstopdf, since this gives a much smaller filesize.

I use multiple subplots that share their x and y axis. I add one overarching subplot, so I can easily add a xlabel and ylabel. I set frameon=False, so it doesn't appear. Afterward, I set the spines and ticks of this axis off. When the figure is displayed, I do not see anything from the big axis. So far, so good.

The problem appears when I save the figure. Saving to EPS and then converting to PDF makes the ticklabels appear, and interfere with my text. Removing the ticklabels outright is also no good, since the spacing then places the labels among the ticklabels of the plots that I do want to see. Curiously, saving to pdf does not have this problem, but the file size is 11 times greater.

Does anyone know what I'm doing wrong, or what is going on?

Working example:

import matplotlib.pyplot as plt
import numpy as np
import subprocess

fig, ax = plt.subplots(2, 2, sharex=True, sharey=True)
ax = ax.flatten()
ax = np.append(ax, fig.add_subplot(1, 1, 1, frameon=False))
ax[-1].spines['top'].set_color('none')
ax[-1].spines['bottom'].set_color('none')
ax[-1].spines['left'].set_color('none')
ax[-1].spines['right'].set_color('none')
ax[-1].tick_params(
    labelcolor='none', top='off', bottom='off', left='off', right='off')
ax[-1].set_xlabel('$u$', fontsize=14)
ax[-1].set_ylabel('$v$', fontsize=14)
plt.setp(ax[-1].get_xticklabels(), visible=False)

fig.savefig('TestPdf.pdf')
fig.savefig('TestEps.eps')
subprocess.check_call(['epstopdf', 'TestEps.eps'])
plt.show()
Wouter
  • 31
  • 2
  • http://stackoverflow.com/questions/19761335/phantom-axis-in-eps-images-despite-having-set-invisible-axis-in-matplotlib Strongly suspect that this the same problem – tacaswell Apr 27 '14 at 17:45
  • I hadn't found that question, but it indeed appears to be related. – Wouter Apr 27 '14 at 18:37

1 Answers1

1

You can try some other backends. For examle pgf backend (available in matplotlib 1.3+) is also capable of producing pdf:

import matplotlib
matplotlib.use("pgf")

You can get a list of backends available with:

matplotlib.rcsetup.all_backends

And You can check wither backend supports eps or pdf with:

import matplotlib
matplotlib.use("BACKEND")
import matplotlib.pyplot as plt
fig = plt.figure()
print fig.canvas.get_supported_filetypes()
Adobe
  • 12,967
  • 10
  • 85
  • 126
  • I tried all backends available, all presented the same problem: EPS just doesn't display the axes correctly. – Wouter Apr 27 '14 at 18:36
  • But if Your final goal is pdf -- why would You need eps at all? If pdf produced directly is larger than pdf produced with eps -- it only means You should compress the pdf produced directly, e.g. with gs: `gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="Compressed.pdf" "Original.pdf"`. – Adobe Apr 27 '14 at 19:18