The xlabels and xtick labels overlap between subplots in matplotlib. How to solve it?

85
February 10, 2021, at 9:30 PM

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')  
READ ALSO
ANDROID REPLAY TOUCH EVENTS [closed]

ANDROID REPLAY TOUCH EVENTS [closed]

Want to improve this question? Update the question so it's on-topic for Stack Overflow

98
GitBash installed but not able to find any node module

GitBash installed but not able to find any node module

I have installed git-bash in windows 10After that, I installed http-server and lerna globally using -g

90
How to implement HTML/JS with Github API to save changes in file to Repository

How to implement HTML/JS with Github API to save changes in file to Repository

In numerous software/web programs, including TiddlyWiki5, I have seen advanced "Save to Github" featuresThese features generally need use of username, repository, and password/PAT, and save changes to the actual file to the Github Repository

54
How can I properly using Comparables in this situation?

How can I properly using Comparables in this situation?

I'm doing an exercise from Sedgewick's Algorithms textbook, the exercise is to find the number of compares for quicksortBelow is my code:

74