Search code examples
pythonmatplotlibsubplotaxes

How to remove frame from pyplot floating axis?


I would like to know how to remove the frame from floating_axes in matplotlib. I am following the setup_axes1 function from the matplotlib gallery here to rotate a plot.

Code posted below.

def setup_axes1(fig, rect):
    """
    A simple one.
    """
    tr = Affine2D().scale(2, 1).rotate_deg(30)
    grid_helper = floating_axes.GridHelperCurveLinear(
        tr, extremes=(-0.5, 3.5, 0, 4),
        grid_locator1=MaxNLocator(nbins=4),
        grid_locator2=MaxNLocator(nbins=4))
    ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
    fig.add_subplot(ax1)
    aux_ax = ax1.get_aux_axes(tr)
    return ax1, aux_ax

I have tried variations of the following common ways to remove the frame on either ax1 or aux_ax, but none of them work.

# before adding subplot
for a in ax1.spines:
    ax1.spines[a].set_visible(False)

# when adding subplot
fig.add_subplot(ax1, frameon=False)

# after adding subplot
plt.axis('off')
plt.tick_params(labelcolor='none', top=False, bottom=False, left=False, right=False)

Any help or suggestions appreciated!


Solution

  • After playing around a bit, I found that the axes are stored in the ax1.axis object. Applying set_visible(False) to each of its elements produced the figure shown in the matplotlib documentation without axes (no ticks either).

    def setup_axes1(fig, rect):
        """
        A simple one.
        """
        tr = Affine2D().scale(2, 1).rotate_deg(30)
    
        grid_helper = floating_axes.GridHelperCurveLinear(
            tr, extremes=(-0.5, 3.5, 0, 4),
            grid_locator1=MaxNLocator(nbins=4),
            grid_locator2=MaxNLocator(nbins=4))
    
        ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
    
        fig.add_subplot(ax1)
    
        aux_ax = ax1.get_aux_axes(tr)
    
        for key in ax1.axis:
            ax1.axis[key].set_visible(False)
            
        return ax1, aux_ax
    

    If you want to keep the ticks, you can further play around with the objects stored in ax1.axis. For example, the following replacement removes only the spines, but keeps the ticks and tick labels.

    ax1.axis[key].line.set_visible(False)