代码之家  ›  专栏  ›  技术社区  ›  norok2

如何在Matplotlib中从两个轴取消设置“sharex”或“sharey”

  •  2
  • norok2  · 技术社区  · 7 年前

    我有一系列的子图,我希望它们在所有子图中共享x轴和y轴,但不包括2个子图(以每行为基础)。

    我知道可以单独创建所有子地块,然后 add the sharex / sharey functionality afterward .

    然而,这是很多代码,因为我必须为大多数子地块这样做。

    一种更有效的方法是使用所需的 sharex / 沙里 属性,例如:

    import matplotlib.pyplot as plt
    
    fix, axs = plt.subplots(2, 10, sharex='row', sharey='row', squeeze=False)
    

    然后设定 解除 sharex / 沙里 功能,这可能 假设 工作方式如下:

    axs[0, 9].sharex = False
    axs[1, 9].sharey = False
    

    上述方法不起作用,但有没有办法做到这一点?

    0 回复  |  直到 7 年前
        1
  •  7
  •   ImportanceOfBeingErnest    7 年前

    正如@zan在文章中指出的 their answer ,你可以使用 ax.get_shared_x_axes() 获得 Grouper 包含所有链接轴的对象,然后 .remove 这石斑鱼的斧子。问题是(正如@WMiller所指出的),所有轴的代码都是一样的。

    所以我们需要

    1. 从石斑鱼上取下轴
    2. 使用相应的新定位器和格式化程序设置新的股票代码

    完整的例子

    import matplotlib
    import matplotlib.pyplot as plt
    import numpy as np
    
    fig, axes = plt.subplots(3, 4, sharex='row', sharey='row', squeeze=False)
    
    data = np.random.rand(20, 2, 10)
    
    for ax in axes.flatten()[:-1]:
        ax.plot(*np.random.randn(2,10), marker="o", ls="")
    
    
    
    # Now remove axes[1,5] from the grouper for xaxis
    axes[2,3].get_shared_x_axes().remove(axes[2,3])
    
    # Create and assign new ticker
    xticker = matplotlib.axis.Ticker()
    axes[2,3].xaxis.major = xticker
    
    # The new ticker needs new locator and formatters
    xloc = matplotlib.ticker.AutoLocator()
    xfmt = matplotlib.ticker.ScalarFormatter()
    
    axes[2,3].xaxis.set_major_locator(xloc)
    axes[2,3].xaxis.set_major_formatter(xfmt)
    
    # Now plot to the "ungrouped" axes
    axes[2,3].plot(np.random.randn(10)*100+100, np.linspace(-3,3,10), 
                    marker="o", ls="", color="red")
    
    plt.show()
    

    enter image description here

    注意,在上面我只改变了x轴的标记,也只改变了主刻度。你需要对y轴做同样的处理,如果需要的话,也需要对小刻度做同样的处理。

        2
  •  4
  •   ImportanceOfBeingErnest    7 年前

    你可以用 ax.get_shared_x_axes() 获取包含所有链接轴的Grouper对象。然后使用 group.remove(ax) 从该组中删除指定的轴。你也可以 group.join(ax1, ax2) 加入新的股份。

    import matplotlib.pyplot as plt
    import numpy as np
    
    fig, ax = plt.subplots(2, 10, sharex='row', sharey='row', squeeze=False)
    
    data = np.random.rand(20, 2, 10)
    for row in [0,1]:
        for col in range(10):
            n = col*(row+1)
            ax[row, col].plot(data[n,0], data[n,1], '.')
    
    a19 = ax[1,9]
    
    shax = a19.get_shared_x_axes()
    shay = a19.get_shared_y_axes()
    shax.remove(a19)
    shay.remove(a19)
    
    a19.clear()
    d19 = data[-1] * 5
    a19.plot(d19[0], d19[1], 'r.')
    
    plt.show()
    

    这仍然需要一些调整来设置刻度,但右下角的绘图现在有自己的限制。 unshared axes

        3
  •  2
  •   William Miller devops-admin    7 年前

    您可以使用以下任一方法访问共享轴组: ax.get_shared_x_axes() 还是按财产 ax._shared_y_axes 。然后可以使用重置标签的可见性 xaxis.set_tick_params(which='both', labelleft=True) 或者使用 setp(ax, get_xticklabels(), visible=True) 然而,这两种方法都有一个固有的问题:勾号格式化程序仍然在轴之间共享。据我所知,这是无法避免的。下面是一个例子来说明:

    import matplotlib.pyplot as plt
    import numpy as np
    
    np.random.seed(1)
    fig, axs = plt.subplots(2, 2, sharex='row', sharey='row', squeeze=False)
    axs[0][0]._shared_x_axes.remove(axs[0][0])
    axs[0][0]._shared_y_axes.remove(axs[0][0])
    
    for ii in range(2):
        for jj in range(2):
            axs[ii][jj].plot(np.random.randn(100), np.linspace(0,ii+jj+1, 100))
    
    axs[0][1].yaxis.set_tick_params(which='both', labelleft=True)
    axs[0][1].set_yticks(np.linspace(0,2,7))
    plt.show()
    

    Unsetting shared axis