代码之家  ›  专栏  ›  技术社区  ›  MB-F

Matplotlib:共享轴上的不同记号标签

  •  3
  • MB-F  · 技术社区  · 9 年前

    我使用 AxesGrid 这导致轴相交处的刻度线标签重叠(图A)。为了避免这种重叠,我想删除右下轴的第一个记号。但是,轴是共享的,因此其他轴上的第一个刻度标签也被删除(图B)。

    有没有办法在共享轴上显示不同的刻度线标签?

    import matplotlib.pyplot as plt
    from mpl_toolkits.axes_grid1 import AxesGrid
    
    fig = plt.figure()
    grid = AxesGrid(fig, 111, nrows_ncols=(2, 2), share_all=True)
    #grid[-1].set_xticks([0.2, 0.4, 0.6, 0.8, 1.0])  # This applies to *all* axes
    plt.show()
    

    enter image description here

    1 回复  |  直到 9 年前
        1
  •  1
  •   Ed Smith    9 年前

    您可以从网格中获取轴句柄,它只是一个列表 ax=grid[3] 然后使用 xticks = ax.xaxis.get_major_ticks() xticks[1].label1.set_visible(False) 作为最小示例,

    import matplotlib.pyplot as plt
    from mpl_toolkits.axes_grid1 import AxesGrid
    import numpy as np
    from matplotlib.cbook import get_sample_data
    
    #Setup figure/grid
    fig = plt.figure()
    grid = AxesGrid(fig, 111, nrows_ncols = (2, 2), share_all=True)
    
    #Plot some data
    f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
    Z = np.load(f)
    for i in range(4):
        im = grid[i].imshow(Z)
    
    #Set tick one of axis 3 in grid to off
    ax = grid[3]
    xticks = ax.xaxis.get_major_ticks()
    xticks[1].label1.set_visible(False)
    plt.draw()
    plt.show()