2

我设法将这些行放在我的 matplotlib 代码中

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

希望在保存的图像中隐藏顶部、右侧和左侧轴。它们在 png 图像中运行良好,但在保存的 eps 文件中,左侧和顶部仍然存在边界(不确定它们是否是轴)(右轴确实消失了)。

保存为 eps 图像时如何隐藏轴/帧边界的任何想法?

顺便说一句:我不想要

ax.axis('off')

因为我确实需要底轴才能工作。

编辑

我刚刚使用以下最小工作示例进行了几次测试,结果证明,即使在 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 的屏幕截图

每股收益

和PNG

PNG

4

2 回答 2

2

这是由于 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 文件中的黑线。

于 2014-11-26T18:38:36.213 回答
0

作为一种解决方法,我建议添加这行代码:

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

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

于 2013-11-06T06:48:26.780 回答