Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

python - Rotate tick labels in subplot (Pyplot, Matplotlib, gridspec)

I am attempting to rotate the x labels of a subplot (created using GridSpec) by 45 degrees. I have tried using axa.set_xticks() and axa.set_xticklabels, but it does not seem to work. Google wasn't helping either, since most questions concerning labels are about normal plots, and not subplots.

See code below:

width = 20                                    # Width of the figure in centimeters
height = 15                                   # Height of the figure in centimeters
w = width * 0.393701                            # Conversion to inches
h = height * 0.393701                           # Conversion to inches

f1 = plt.figure(figsize=[w,h])
gs = gridspec.GridSpec(1, 7, width_ratios = [1.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])

axa = plt.subplot(gs[0])
axa.plot(dts, z,'k', alpha=0.75, lw=0.25)
#axa.set_title('...')
axa.set_ylabel('TVDSS ' + '$[m]$', fontsize = '10' )
axa.set_xlabel('slowness 'r'$[mu s/m]$', fontsize = '10')
axa.set_ylim(245, 260)
axa.set_xlim(650, 700)
axa.tick_params(labelsize=7)
axa.invert_yaxis()
axa.grid()

Any help will be greatly appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do it in multiple ways:

Here is one solution making use of tick_params:

ax.tick_params(labelrotation=45)

Here is another solution making use of set_xticklabels:

ax.set_xticklabels(labels, rotation=45)

Here is a third solution making use of set_rotation:

for tick in ax.get_xticklabels():
    tick.set_rotation(45)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...