I have created a subplots grid without any spaces between the subplots, with shared x,y-axes. I only show the ticks and labels for the outer subplots. The problem is that the tick numbers overlap at the borders of the subplots. With MaxNLocator, I can remove the upper or lower ticks, but only for all plots at once.
Question: How can I keep the highest tick only for certain plots (in this case x=2.0 only in the bottom right subplot, and y=3 only in the top left subplot)? Why does my conditional setting of ticks for certain subplots fail?
Code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
numPlotsY = 3
numPlotsX = 3
f, ax_grid = plt.subplots(numPlotsY,numPlotsX,sharex=True,sharey=True)
A = np.arange(numPlotsY)+1.0
phi = np.arange(numPlotsX)
x = np.linspace(0,2.0,100)
fontsize = 18
for y_i in range(0,numPlotsY):
for x_i in range(0,numPlotsX):
y = A[y_i]*np.sin(x*np.pi + phi[x_i])
ax = ax_grid[y_i,x_i]
ax.plot(x,y,lw=2.0)
if x_i == 0:
ax.set_ylabel(r'$y$', fontsize=fontsize)
if y_i == numPlotsY-1:
###########################
# Why doesn't this work?! #
###########################
if x_i != numPlotsX-1:
nbins = len(ax.get_xticklabels())
ax.xaxis.set_major_locator(MaxNLocator(nbins=nbins,prune='upper'))
else:
nbins = len(ax.get_xticklabels())
ax.xaxis.set_major_locator(MaxNLocator(nbins=nbins,prune=None))
ax.set_xlabel(r'$x/pi$', fontsize=fontsize)
if y_i == 0:
ax.set_title(r'$phi=%s$' % phi[x_i], fontsize=fontsize)
if x_i == numPlotsX-1:
ax.annotate(r'$A=%d$' % A[x_i], xy=(1.1,0.5), rotation=90,
ha='center',va='center',xycoords='axes fraction', fontsize=fontsize)
f.subplots_adjust(wspace=0,hspace=0)
plt.suptitle(r'$Acdotsinleft(2pi x + phiright)$',fontsize=18)
plt.show()
Aucun commentaire:
Enregistrer un commentaire