Matplotlib:x轴标题间距

时间:2017-03-10 15:35:02

标签: python matplotlib

当我在matplotlib中增加字体大小时,x轴标题的部分被截断。有没有办法保持情节/数字不变,只是告诉matplotlib增加输出页面大小而不是切断?

我的代码是:

import matplotlib.pyplot as plt

plt.style.use('paper')


fig = plt.figure()
ax1 = plt.subplot2grid((3,1),(0,0),rowspan=2)
ax2 = plt.subplot2grid((3,1),(2,0),sharex=ax1)


# first subplot
plt.setp(ax1.get_xticklabels(),visible=False)
ax1.set_ylabel("$\mathrm{Arbitrary\; Units}$", size=20)
ax1.yaxis.get_major_formatter().set_powerlimits((0,1))
ax1.get_yaxis().set_label_coords(-0.1,0.5)

x1,x2 = [],[]
for i in xrange(1000000):
    r = rnd.random()
    x1.append(r**(1/2))
    x2.append(r**(1/3))

xmin = 0
xmax = 1
nbins = 50

h1,_,_ = ax1.hist(x1,bins=nbins,range=(xmin,xmax),normed=1,color="#FF0000",histtype='step')
h2,_,_ = ax1.hist(x2,bins=nbins,range=(xmin,xmax),normed=1,color="#00FF00",histtype='step')

# legend
old, = ax1.plot([0,0],color="#FF0000",label="2")
new, = ax1.plot([0,0],color="#00FF00",label="3")
ax1.legend(loc=2, ncol=1, borderaxespad=0.)
old.set_visible(False)
new.set_visible(False)

# second subplot
ax2.plot(np.linspace(xmin,xmax,nbins),h2/h1)
ax2.plot([xmin,xmax],[1,1],color="black")
ax2.set_ylim(ymin=0,ymax=1.99)
ax2.set_xlabel(r'$\phi_\mu$', size=20, weight="light")
ax2.set_ylabel("$\mathrm{Ratio}$", size=20)
ax2.get_yaxis().set_label_coords(-0.1,0.5)

fig.subplots_adjust(hspace=0.)
ax2.set_yticks(ax2.get_yticks()[1:-1])

我使用的样式表('paper')是:

axes.axisbelow: True
axes.grid: False
axes.labelcolor: 333333
axes.linewidth: 0.6
backend: GTK
font.family: serif
font.size: 20
grid.alpha: 0.1
grid.color: black
grid.linestyle: -
grid.linewidth: 0.8
legend.fontsize: 20
legend.framealpha: 0
legend.numpoints: 1
lines.linestyle: -
lines.linewidth: 2
lines.markeredgewidth: 0
patch.linewidth: 2
text.color: 555555
text.usetex: True
xtick.color: 333333
ytick.color: 333333

这就是它的样子:

https://preview.ibb.co/miJg6F/foo.png

由于我希望子图共享x轴,我不希望它们之间有任何空间。

有人可以帮我吗?

干杯。

0 个答案:

没有答案