2013-11-04 40 views
2

已经设置为不可见轴,我设法把这些线在我的matplotlib代码幻象轴尽管matplotlib

ax.spines['top'].set_visible(False) 
ax.spines['right'].set_visible(False) 
ax.spines['left'].set_visible(False) 

在保存的图像隐藏顶部,右侧的希望,左轴。他们在PNG图像上工作得很好,但在保存的eps文件中,左侧和顶部(右侧轴确实消失)仍然存在边界(不确定它们是否是轴)。

有关如何在保存为EPS图像时如何隐藏轴/边框边界的任何想法?

BTW:我不想

ax.axis('off') 

,因为我确实需要中轴下方工作。

EDIT

我只是做了多次试验用下面的最小工作示例中,原来的轴将是不可见的,即使在EPS输出,如果我任 1)关闭光栅化在EPS; 或 2)关闭xticks和xticklabels上的手动设置

但是,上述两个功能都是我绝对需要保留在eps输出中的,所以,任何解决方案?

import matplotlib.pyplot as plt 
import numpy as np 
# setting up fig and ax 
fig = plt.figure(figsize=(12,6)) 
ax = fig.add_axes([0.00,0.10,0.90,0.90]) 
# translucent vertical band as the only subject in the figure 
# note the zorder argument used here 
ax.axvspan(2014.8, 2017.8, color="DarkGoldenRod", alpha=0.3, zorder=-1) 
# setting up axes 
ax.set_xlim(2008, 2030) 
ax.set_ylim(-2, 2) 
# if you toggle this to False, the axes are hidden 
if True : 
    # manually setting ticks 
    ticks = np.arange(2008, 2030, 2) 
    ax.set_xticks(ticks) 
    ax.set_xticklabels([r"$\mathrm{" + str(r) + r"}$" for r in ticks], fontsize=26, rotation=30) 
    ax.get_xaxis().tick_bottom() 
    ax.set_yticks([]) 
# hide all except for the bottom axes 
ax.spines['top'].set_visible(False) 
ax.spines['right'].set_visible(False) 
ax.spines['left'].set_visible(False) 
# if you toggle this to False, the axes are hidden 
if True : 
    # this is to make sure the rasterization works. 
    ax.set_rasterization_zorder(0) 
# save into eps and png separately 
fig.savefig("test.eps", papertype="a4", format="eps", bbox_inches='tight', pad_inches=0.1, dpi=None) 
fig.savefig("test.png", papertype="a4", format="png", bbox_inches='tight', pad_inches=0.1, dpi=None) 

和屏幕截图EPS

eps

和对PNG

png

+0

你能发表该eps文件的截图吗? – Raiyan

+0

@Raiyan完成,示例代码,新发现,以及显示的数字。 – nye17

+0

嘿,我只是在我的电脑上运行你的代码;该eps顶部没有黑线。 – Raiyan

回答

0

作为工作的时候,我会建议增加这行代码:

ax.spines['top'].set_color((0,0,0,0)) 

我基本上把顶轴设置为透明。

+0

很好的调整。我没有测试,但它应该工作。我会再等几天,看看是否有更多的破坏而不是伪装。 – nye17

1

这是由于已修复mpl(https://github.com/matplotlib/matplotlib/issues/2473)中的一个错误(https://github.com/matplotlib/matplotlib/pull/2479)。

问题是AGG中空画布的默认颜色是(0,0,0,0)(完全透明的黑色),但是eps不处理alpha(它只是被丢弃)所以(0,0, 0,0) - >(0,0,0)当推入一个eps文件。如果关闭轴,那么画布的该区域永远不会被绘制,因此它保持默认颜色。接受的答案迫使这些像素在光栅化过程中得到渲染(并合成到白色背景上),所以您在eps文件中看不到黑线。