Matplotlib savefig to eps忽略visibility = False

时间:2014-04-27 14:37:09

标签: python matplotlib eps

我正在尝试将流线图保存为EPS,然后使用epstopdf将其转换为PDF,因为这样可以提供更小的文件大小。

我使用多个共享x和y轴的子图。我添加了一个总体子图,所以我可以轻松添加一个xlabel和ylabel。我设置frameon = False,所以它不会出现。之后,我设置了这个轴的刺和蜱。当显示图形时,我从大轴看不到任何东西。到目前为止,非常好。

保存图片时出现问题。保存到EPS然后转换为PDF会使得标签出现,并干扰我的文本。完全删除蜱标签也没有用,因为间距会将标签放在我想要看到的图的刻度标签之间。奇怪的是,保存为pdf没有这个问题,但文件大小是11倍。

有谁知道我做错了什么,或者发生了什么事?

工作示例:

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()

1 个答案:

答案 0 :(得分:1)

您可以尝试其他一些后端。对于例如pgf后端(在matplotlib 1.3+中可用)也能够生成pdf:

import matplotlib
matplotlib.use("pgf")

您可以通过以下方式获取可用的后端列表:

matplotlib.rcsetup.all_backends

你可以用以下方法检查后端是否支持eps或pdf:

import matplotlib
matplotlib.use("BACKEND")
import matplotlib.pyplot as plt
fig = plt.figure()
print fig.canvas.get_supported_filetypes()