The xlabels and xtick labels overlap between subplots as shown in the figure. I am trying to use tight_layout() and plt.subplots_adjust(hspace=0.2) to solve it, but seems useless. Can anyone help me find out the worked solution? I also vary the figure size, and the hspace. But just does not work.
My code is as shown below.
# -*- coding: utf-8 -*-
from pylab import *
import re
matplotlib.rc('xtick', labelsize=12)
matplotlib.rc('ytick', labelsize=12)
plt.rcParams['font.size'] = 12
plt.rcParams['axes.labelsize'] = 12
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12
plt.rcParams['legend.fontsize'] = 12
rc('font',family='Arial')
fig = plt.figure(figsize=(3,18))
plt.subplots_adjust(hspace=0.2)
ax = fig.add_subplot(3,1,1)
test=[0,1,1,1,1,1,1,1,1]
ann=[1,1,1,1,1,1,1,1,1]
xgb=[0,0,0,1,1,1,1,1,1]
ytest=np.zeros(9)
yann=np.ones(9)
yxgb=np.ones(9)*2
x=arange(1,10)
xlabel("1")
k=0
for i in test:
if i==0:
plot(x[k],ytest[k],'ko', markersize=9)
else:
plot(x[k],ytest[k],'rx', markersize=9)
k=k+1
k=0
for i in ann:
if i==0:
plot(x[k],yann[k],'ko', markersize=9)
else:
plot(x[k],yann[k],'rx', markersize=9)
k=k+1
k=0
for i in xgb:
if i==0:
plot(x[k],yxgb[k],'ko', markersize=9)
else:
plot(x[k],yxgb[k],'rx', markersize=9)
k=k+1
ylim(-0.5,2.5)
labels = ['a', 'b', 'c']
y=[0,1,2]
xlabels = ['5-dry', '10-dry', '15-dry', '5-nat', '10-nat', '15-nat','5-wet', '10-wet', '15-wet']
x=[1,2,3,4,5,6,7,8,9]
plt.yticks(y, labels)
plt.xticks(x, xlabels, rotation=40,ha="right")
ax = fig.add_subplot(3,1,2)
test=[0,0,1,1,1,1,1,1,1]
ann=[1,1,1,1,1,1,1,1,1]
xgb=[0,0,0,1,1,1,1,1,1]
ytest=np.zeros(9)
yann=np.ones(9)
yxgb=np.ones(9)*2
x=arange(1,10)
xlabel("2")
k=0
for i in test:
if i==0:
plot(x[k],ytest[k],'ko', markersize=9)
else:
plot(x[k],ytest[k],'rx', markersize=9)
k=k+1
k=0
for i in ann:
if i==0:
plot(x[k],yann[k],'ko', markersize=9)
else:
plot(x[k],yann[k],'rx', markersize=9)
k=k+1
k=0
for i in xgb:
if i==0:
plot(x[k],yxgb[k],'ko', markersize=9)
else:
plot(x[k],yxgb[k],'rx', markersize=9)
k=k+1
ylim(-0.5,2.5)
labels = ['a', 'b', 'c']
y=[0,1,2]
xlabels = ['5-dry', '10-dry', '15-dry', '5-nat', '10-nat', '15-nat','5-wet', '10-wet', '15-wet']
x=[1,2,3,4,5,6,7,8,9]
plt.yticks(y, labels)
plt.xticks(x, xlabels, rotation=40,ha="right")
ax = fig.add_subplot(3,1,3)
test=[0,0,0,0,1,1,1,1,1]
ann=[1,1,1,1,1,1,1,1,1]
xgb=[0,0,0,1,1,1,1,1,1]
ytest=np.zeros(9)
yann=np.ones(9)
yxgb=np.ones(9)*2
x=arange(1,10)
xlabel("3")
k=0
for i in test:
if i==0:
plot(x[k],ytest[k],'ko', markersize=9)
else:
plot(x[k],ytest[k],'rx', markersize=9)
k=k+1
k=0
for i in ann:
if i==0:
plot(x[k],yann[k],'ko', markersize=9)
else:
plot(x[k],yann[k],'rx', markersize=9)
k=k+1
k=0
for i in xgb:
if i==0:
plot(x[k],yxgb[k],'ko', markersize=9)
else:
plot(x[k],yxgb[k],'rx', markersize=9)
k=k+1
ylim(-0.5,2.5)
labels = ['a', 'b', 'c']
y=[0,1,2]
xlabels = ['5-dry', '10-dry', '15-dry', '5-nat', '10-nat', '15-nat','5-wet', '10-wet', '15-wet']
x=[1,2,3,4,5,6,7,8,9]
plt.yticks(y, labels)
plt.xticks(x, xlabels, rotation=40,ha="right")
fig.tight_layout()
leg=legend( loc=7,numpoints=1)
leg.get_frame().set_alpha(0.0)
show()
fig.savefig('all.png', transparent=True,bbox_inches='tight')