Question

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)

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

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

BTW: I don't want

ax.axis('off')

as I do need the bottom axis to work.

EDIT

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

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)

and screenshot for eps

eps

and for png

png

Was it helpful?

Solution 2

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.

OTHER TIPS

This was due to a bug in mpl (https://github.com/matplotlib/matplotlib/issues/2473) which has been fixed (https://github.com/matplotlib/matplotlib/pull/2479).

The problem is that the default color for the empty canvas in AGG was (0, 0, 0, 0) (fully transparent black) but eps does not handle alpha (it just gets dropped) so (0,0,0,0) -> (0,0,0) when shoved into an eps file. If you turn the axes off, that region of the canvas is never drawn too, hence it stays the default color. The accepted answer forces those pixels to get rendered (and composited onto a white background) during the rasterization so you don't see the black line in the eps file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top