我正在尝试制作包含多个子图的情节,我希望他们分享他们的轴。我一直在尝试使用matplotlib.ticker.MaxNLocator
修剪刻度线上的标签,因此该图形具有可读轴。但是,无论我使用prune='upper'
,'lower'
还是'both'
,我最终都会看到彼此重叠的标签,如下图所示:
我正在使用的代码版本略微简化(虽然仍然相当长,很抱歉)如下:
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')
在使用MaxNLocator
函数时,我误解了什么?我正在使用Matplotlib 2.0.0。
(同样很明显,任何有关如何改善此图以提高可读性并减少硬编码量的评论都非常感谢!)