0

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.

enter image description here

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')  

user3737702
  • 591
  • 1
  • 10
  • 17
  • Hello, i used, but did not work – user3737702 Feb 10 '21 at 15:21
  • 1
    Not reproducible in [matplotlib 3.3.3.](https://imgur.com/a/8a6bbp3). Besides - don't do `from pylab import *` because [don't use pylab](https://matplotlib.org/3.3.1/api/index.html?highlight=pylab#module-pylab) and don't [`import *`](https://stackoverflow.com/questions/2386714/why-is-import-bad) – Mr. T Feb 10 '21 at 15:29
  • ok. I tried this, but still did not work – user3737702 Feb 10 '21 at 15:44
  • 1
    No, that advice was unrelated. I don't know why your output differs. – Mr. T Feb 10 '21 at 15:49
  • I tested your code with matplotlib 3.3.2 and it worked fine with just `fig.tight_layout()`. To get a similar result with `plt.subplots_adjust()` use `hspace=0.3` or more (that is for the oddly large figure height of 18 inches). I suggest using `subplots_adjust` instead of `tight_layout` as it preserves the figure size of the output image. – Patrick FitzGerald Feb 10 '21 at 17:09
  • Thx,I changed to 3x2 grid and my computer, now everything works fine. Strange that computer can have a impact on the result... – user3737702 Feb 11 '21 at 04:50

0 Answers0