Reputation: 977
I am creating a subplot using matplotlib, where the top x-axis is different from the bottom x-axis. I added the xticks and xlabels to the top myself.
I would like there to be xtick marks corresponding to the bottom x-axis at the bottom of the middle and top subplots, where the xticks point outwards (or down) - like at the very bottom. Is there a way to do this?
This is the code I am using so far to customize the ticks:
f, (ax1, ax2, ax3) = plt.subplots(3, sharex=False, sharey=False)
f.subplots_adjust(hspace=0)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
ax3.get_xaxis().set_tick_params(direction='out', top='off', which='both')
ax2.get_xaxis().set_tick_params(direction='out', bottom='on', top='off', which='both')
ax1.minorticks_off()
ax1.get_xaxis().tick_top()
ax1.set_xticks([np.divide(1.0,100.0), np.divide(1.0,50.0), np.divide(1.0,35.0),
np.divide(1.0,20.0), np.divide(1.0,10.0), np.divide(1.0,5.0),
np.divide(1.0,3.0), np.divide(1.0,2.0), np.divide(1.0,1.0)])
ax1.set_xticklabels([100, 50, 35, 20, 10, 5, 3, 2, 1])
I am finding that since I made customized xticks for the top plot, I can't do this, and specifying the direction to be 'out' in the bottom subplot ruins the tick marks in the middle. Since there is no space between the subplots, the top of the bottom plot shares its x-axis with the bottom of the middle subplot, etc...
Is there a workaround for this?
Upvotes: 1
Views: 1980
Reputation: 2690
You can draw the middle axes above the bottom axes by setting the respective z orders. The top ticks can be done w/a call to axvline.
import matplotlib.pyplot as plt
import numpy as np
f, (ax1, ax2, ax3) = plt.subplots(3, sharex=False, sharey=False)
f.subplots_adjust(hspace=0)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
ax3.get_xaxis().set_tick_params(direction='out', top='off', which='both')
ax2.get_xaxis().set_tick_params(direction='out', bottom='on', top='off', which='both')
ax1.minorticks_off()
ax1.get_xaxis().tick_top()
ax1.set_xticks([np.divide(1.0,100.0), np.divide(1.0,50.0), np.divide(1.0,35.0),
np.divide(1.0,20.0), np.divide(1.0,10.0), np.divide(1.0,5.0),
np.divide(1.0,3.0), np.divide(1.0,2.0), np.divide(1.0,1.0)])
ax1.set_xticklabels([100, 50, 35, 20, 10, 5, 3, 2, 1])
for i, ax in enumerate((ax3, ax2, ax1)):
ax.set_zorder(i)
for tick in ax2.xaxis.get_ticklocs():
ax2.axvline(tick, ymin=0.9)
plt.show()
Upvotes: 1