旋转子图中的刻度标签(Pyplot,Matplotlib,gridspec) [英] Rotate tick labels in subplot (Pyplot, Matplotlib, gridspec)
问题描述
我正在尝试将子图(使用GridSpec创建)的x标签旋转45度.我尝试使用 axa.set_xticks()
和 axa.set_xticklabels
,但是它似乎不起作用.谷歌也没有帮助,因为大多数关于标签的问题都是关于正常图,而不是子图.
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.
请参见下面的代码:
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()
任何帮助将不胜感激!
推荐答案
您可以在此行中设置刻度标签的旋转属性:
You can set the rotation property of the tick labels with this line:
plt.setp(axa.xaxis.get_majorticklabels(), rotation=45)
setp
是一个实用函数,用于设置多个艺术家的属性(在本例中为所有刻度标签).
setp
is a utility function to set a property of multiple artists (all ticklabels in this case).
顺便说一句:'正常' 和 matplotlib 中的子图没有区别.两者都只是 Axes 对象.唯一的区别是在同一图中,它们的大小和位置以及数量.
BTW: There is no difference between a 'normal' and a subplot in matplotlib. Both are just Axes objects. The only difference is the size and position and the number of them in the same figure.
这篇关于旋转子图中的刻度标签(Pyplot,Matplotlib,gridspec)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!