【问题标题】:Removing frame while keeping axes in pyplot subplots在 pyplot 子图中保留轴的同时删除框架
【发布时间】:2014-03-27 20:08:49
【问题描述】:

我正在创建一个包含 3 个子图的图形,并且想知道是否有任何方法可以移除它们周围的框架,同时保持轴就位?

【问题讨论】:

    标签: matplotlib frame axes subplot


    【解决方案1】:

    试试plt.box(on=None)只删除了情节周围的边界框(框架),这是我想要做的。

    plt.axis('off') 删除了刻度标签和边界框,这不是我想要完成的。

    【讨论】:

      【解决方案2】:

      如果您想删除轴脊,而不是其他信息(刻度、标签等),您可以这样做:

      fig, ax = plt.subplots(7,1, sharex=True)
      
      t = np.arange(0, 1, 0.01)
      
      for i, a in enumerate(ax):
          a.plot(t, np.sin((i + 1) * 2 * np.pi * t))
          a.spines["top"].set_visible(False)
          a.spines["right"].set_visible(False)
          a.spines["bottom"].set_visible(False)
      

      或者,更简单的是,使用seaborn

      fig, ax = plt.subplots(7,1, sharex=True)
      
      t = np.arange(0, 1, 0.01)
      
      for i, a in enumerate(ax):
          a.plot(t, np.sin((i + 1) * 2 * np.pi * t))
      
      seaborn.despine(left=True, bottom=True, right=True)
      

      这两种方法都会给你:

      【讨论】:

      • 如果你碰巧有极坐标图,刺的定义不同,所以根据stackoverflow.com/a/22848030/1034716,你需要这样做:a.spines['polar'].set_visible(False)
      • 在 Python 3 中,for 循环更加简单:for a in ax:
      • @mwaskom 如何将 x 轴保持在上述代码的最后一个子图中?
      【解决方案3】:

      您可以使用轴句柄的axis('off') 方法实现类似的效果。这是你所追求的吗? (下图示例代码)。

      fig, ax = plt.subplots(7,1)
      
      t = np.arange(0, 1, 0.01)
      
      for i, a in enumerate(ax):
          a.plot(t, np.sin((i+1)*2*np.pi*t))
          a.axis('off')
      
      plt.show()
      

      【讨论】:

        猜你喜欢
        • 2021-03-13
        • 2018-08-15
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        • 2013-09-22
        • 2013-12-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多