MB-F
MB-F

Reputation: 23637

Matplotlib: Different ticklabels on shared axes

I create tightly spaced subplots with shared axes using AxesGrid. This leads to overlapping tick labels where the axes meet (Figure, A). To avoid this overlap I want to remove the first tick of the lower right axes. However, the axes are shared, so the first tick label is removed on the other axes too (Figure, B).

Is there a way to show different tick labels on shared axes?

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

Upvotes: 3

Views: 379

Answers (1)

Ed Smith
Ed Smith

Reputation: 13206

You can get the axis handle from grid which is just a list with ax=grid[3] and then use xticks = ax.xaxis.get_major_ticks() and xticks[1].label1.set_visible(False). As a minimal example,

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

Upvotes: 1

Related Questions