1

I have an odd number of subplots like so:

import matplotlib.pyplot as plt 

fig, axes = plt.subplots(2, 2, sharex=True)
for i, ax in enumerate(axes.flat):
    ax.plot(range(10))
fig.delaxes(axes.flat[-1])

I want them all to have the same x-axis, but easily add the x-ticks back to the plot on the right, since there is no longer a 4th plot.

It seems like there should be an easier/cleaner solution than adding each subplot manually (similar to this answer), but I can't seem to find anything. Thanks.

1 Answer 1

2

you can use setp to make the xtick labels visible for ax[0][1] like this

import matplotlib.pyplot as plt 

fig, axes = plt.subplots(2, 2, sharex=True)
for i, ax in enumerate(axes.flat):
    ax.plot(range(10))
# for matploltib version 2.0.1
plt.setp(axes[0][1].get_xticklabels(), visible=True)
# for matplotlib version 2.1.1
axes[0][1].xaxis.set_tick_params(which='both', labelbottom=True, labeltop=False)
fig.delaxes(axes.flat[-1])
plt.show()

which will result in

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

what version of matplotlib are you using? I tried this with 2.1.0 and 2.1.1 (from conda-forge) and couldn't get this result (axes[0][1] still has no tick labels).
see my latest edit. If the answer helped, please upvote/accept the answer. thanks!
That worked, thanks! I upvoted the answer, but don't have enough reputation for it to show up :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.