简体   繁体   English

Matplotlib savefig to eps忽略visibility = False

[英]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. 我正在尝试将流线图保存为EPS,然后使用epstopdf将其转换为PDF,因为这样可以提供更小的文件大小。

I use multiple subplots that share their x and y axis. 我使用多个共享x和y轴的子图。 I add one overarching subplot, so I can easily add a xlabel and ylabel. 我添加了一个总体子图,所以我可以轻松添加一个xlabel和ylabel。 I set frameon=False, so it doesn't appear. 我设置frameon = False,所以它不会出现。 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. 保存到EPS然后转换为PDF会使得标签出现,并干扰我的文本。 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. 奇怪的是,保存为pdf没有这个问题,但文件大小是11倍。

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

You can try some other backends. 您可以尝试其他一些后端。 For examle pgf backend (available in matplotlib 1.3+) is also capable of producing pdf: 对于例如pgf后端(在matplotlib 1.3+中可用)也能够生成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: 你可以检查枯萎的后端是否支持eps或pdf:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM