1

When I have connected plots, I like to set different xticks on each plot so that there isn't an overlap between the last tick on the first plot and the first tick on the last plot. Here's what I mean:

import numpy as np
from matplotlib import pyplot as plt

x_bounds = [0, 4]
x1_ticks = np.arange(4)
x2_ticks = np.arange(5)

y_bounds = [0, 8]
y1_ticks = np.arange(9)
y2_ticks = np.arange(8)

fig, ((ax11, ax12), (ax21, ax22)) = plt.subplots(2, 2, figsize = [8, 8], sharex = True, sharey = True)
fig.subplots_adjust(wspace=0)
fig.subplots_adjust(hspace=0)

ax11.plot(x, x)
ax12.plot(x, np.sqrt(x))
ax21.plot(x, x**2)
ax22.plot(x, np.exp(x))

ax21.set_xticks(x1_ticks)
ax22.set_xticks(x2_ticks)
ax11.set_yticks(y1_ticks)
ax21.set_yticks(y2_ticks)

ax21.set_xlim(x_bounds)
ax22.set_xlim(x_bounds)
ax11.set_ylim(y_bounds)
ax21.set_ylim(y_bounds)

enter image description here

However, clearly this didn't work. How can I fix this?

Xanshiz
  • 95
  • 1
  • 6
  • 1
    Set the ticks first, then set the limits. If you set the ticks, Matplotlib resets the limits to include all the ticks (not my favourite behaviour, but thats what someone decided at some point). – Jody Klymak Jan 17 '23 at 18:19
  • Thanks for the suggestion. I reordered (edited the original post for the updated code) and the results are still the same. – Xanshiz Jan 17 '23 at 18:23
  • Apologies, should have read your code more closely. What do you want to happen at x=0 and x=4? You've jammed the axes together, and forced the ticks to be at the edges, so of course the ticklables are going to overlap. If you want to turn the first tick off then `ax.set_xticks(x1_ticks, labels=[' ' if x ==4 else f'{x}' for x in x1_ticks])` should make the last tick not be labeled. – Jody Klymak Jan 17 '23 at 20:51
  • Yes, I would like to remove the "4" at the overlap. ``plt.setp(ax21.get_xticklabels()[-1], visible=False)`` ended up working – Xanshiz Jan 18 '23 at 19:33

0 Answers0