我设法将这些行放入我的 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')
因为我确实需要底轴才能工作。
编辑
我刚刚用下面的最小工作示例做了几个测试,事实证明,如果我要么 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
请您参考如下方法:
这是由于 mpl ( https://github.com/matplotlib/matplotlib/issues/2473) 中的错误已修复 (https://github.com/matplotlib/matplotlib/pull/2479)。
问题是 AGG 中空白 Canvas 的默认颜色是 (0, 0, 0, 0)(完全透明的黑色)但 eps 不处理 alpha(它只是被丢弃)所以 (0,0,0 ,0) -> (0,0,0) 当被插入 eps 文件时。如果关闭轴,则 Canvas 的该区域也不会被绘制,因此它保持默认颜色。接受的答案强制这些像素在光栅化过程中被渲染(并合成到白色背景上),因此您看不到 eps 文件中的黑线。
