I am trying to make a plot including a number of subplots, and I want them to share their axes. I have been trying to use matplotlib.ticker.MaxNLocator
to do prune the labels on my ticks, so the figure has readable axes. However, regardless of whether I use prune='upper'
, 'lower'
, or 'both'
, I end up with labels which overlap each other, as shown in the image below:
An ever so slightly simplified version of the code I am using (although still fairly long, sorry) is below:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.ticker as tck
chains = np.array([[-.4, 4, 0, 0, 0], [6, 19, 2000, 2.1e16, 6.1e13]])
pars = np.array([r'$SF_\infty$', r'$\tau_D$', r'$\tau$', 'width', 'scale'])
nplots = len(chains[0,:]) - 1
# Fix up matplotlib to give plots like you like
mpl.rcParams.update({'font.size': 6, 'font.family': 'serif', 'mathtext.fontset': 'cm', 'mathtext.rm': 'serif',
'lines.linewidth': .7, 'xtick.top': True, 'ytick.right': True})
# Make a plot
fig = plt.figure()
for i in range(1,nplots+1):
for j in range(nplots):
if (j<i):
ax = plt.subplot(nplots, nplots, (i-1)*nplots+j+1)
plt.plot(chains[ :, j ], chains[ :, i ], '.-', markersize=0.3, alpha=0.5)
# Set aspect
xlim, ylim = ax.get_xlim(), ax.get_ylim()
ax.axis([xlim[0], xlim[1], ylim[0], ylim[1]])
ax.set_aspect( float(xlim[1]-xlim[0]) / (ylim[1]-ylim[0]) )
# Print things around the edges
if (j == 0): plt.ylabel(pars[i])
if (i == nplots): plt.xlabel(pars[j])
if (j != 0): ax.tick_params(labelleft=False)
if (i != nplots): ax.tick_params(labelbottom=False)
if (j != i-1):
ax.get_xaxis().get_offset_text().set_visible(False)
ax.get_yaxis().get_offset_text().set_visible(False)
# End if-statements
# Fix tickers
ax.minorticks_on()
ax.tick_params(which='both', direction='inout', width=0.5)
ax.xaxis.set_major_locator(tck.MaxNLocator(nbins=5, prune='both'))
ax.yaxis.set_major_locator(tck.MaxNLocator(nbins=5, prune='both'))
# Saving file
plt.subplots_adjust(hspace=0, wspace=-.58)
plt.savefig('testing.png', dpi=400, bbox_inches='tight')
plt.close('all')
What am I misunderstanding in my usage of the MaxNLocator
function? I am using Matplotlib 2.0.0.
(Also obviously, any comments on how to improve this plot for readability and decrease the amount of hard-coding is much appreciated!)