尽管已在matplotlib中设置了不可见轴,但eps图像中的幻像轴 [英] phantom axis in eps images despite having set invisible axis in matplotlib

查看:95
本文介绍了尽管已在matplotlib中设置了不可见轴,但eps图像中的幻像轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法将这些行添加到我的matplotlib代码中

I managed to put those line in my matplotlib code

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

希望隐藏已保存图像中的上,右和左轴.它们在png图像中效果很好,但是在保存的eps文件中,左侧和顶部(右侧的轴确实消失了)仍然存在边界(不确定它们是否是轴).

in hopes of hiding the top, right, and left axes in the saved images. They worked fine in png images, but in the saved eps files, there are still boundaries (not sure whether they are axes) on the left and the top (the right axis indeed disappeared).

关于另存为eps图像时如何隐藏轴/框架边界的任何想法吗?

any ideas on how to hide the axis/frame boundaries when saving as eps images?

顺便说一句:我不要

ax.axis('off')

因为我确实需要下轴才能工作.

as I do need the bottom axis to work.

编辑

我只是用以下最小的工作示例进行了几次测试,结果发现即使在eps输出中,轴也将是不可见的 1)在eps中关闭光栅化; 或者 2)关闭xticks和xticklabels上的手动设置

I just did several tests with the following minimal working example, it turns out the axes will be invisible even in eps outputs if I either 1) turn off rasterization in eps; or 2) turn off manual settings on xticks and xticklabels

但是,上述两项功能都是我绝对需要保留在eps输出中的,因此,有什么解决方案吗?

However, both above features are what I absolutely need to keep in the eps output, so, any solutions?

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

推荐答案

作为解决方法,我建议添加以下代码行:

As a work around, I would suggest adding this line of code:

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

我基本上是将顶部轴设置为透明.

I'm basically setting the top axis to transparent.

这篇关于尽管已在matplotlib中设置了不可见轴,但eps图像中的幻像轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆