【问题标题】:How to remove frame from pyplot floating axis?如何从 pyplot 浮动轴中删除框架?
【发布时间】:2021-03-13 23:17:27
【问题描述】:

我想知道如何从floating_axes 中的matplotlib 中删除框架。 I am following the setup_axes1 function from the matplotlib gallery here to rotate a plot.

代码贴在下面。

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

我尝试了以下常用方法的变体来删除ax1aux_ax 上的框架,但它们都不起作用。

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

任何帮助或建议表示赞赏!

【问题讨论】:

    标签: python matplotlib subplot axes


    【解决方案1】:

    玩了一会儿,我发现坐标轴存储在ax1.axis 对象中。将set_visible(False) 应用于其每个元素会生成 matplotlib 文档中显示的图形,没有轴(也没有刻度)。

    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
    

    如果您想保留刻度,可以进一步使用存储在ax1.axis 中的对象。例如,以下替换仅删除了刺,但保留了刻度和刻度标签。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2019-01-10
      相关资源
      最近更新 更多