Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib savefig to eps ignores visibility=False

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()
like image 305
Wouter Avatar asked Aug 15 '26 10:08

Wouter


1 Answers

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()
like image 68
Adobe Avatar answered Aug 16 '26 23:08

Adobe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!