简体   繁体   English

在子图中旋转刻度标签(Pyplot、Matplotlib、gridspec)

[英]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.我正在尝试将子图(使用 GridSpec 创建)的 x 标签旋转 45 度。 I have tried using axa.set_xticks() and axa.set_xticklabels , but it does not seem to work.我试过使用axa.set_xticks()axa.set_xticklabels ,但它似乎不起作用。 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!任何帮助将不胜感激!

You can do it in multiple ways:您可以通过多种方式做到这一点:

Here is one solution making use of tick_params :这是使用tick_params一种解决方案:

ax.tick_params(labelrotation=45)

Here is another solution making use of set_xticklabels :这是另一个使用set_xticklabels解决方案:

ax.set_xticklabels(labels, rotation=45)

Here is a third solution making use of set_rotation :是使用set_rotation的第三个解决方案:

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

You can set the rotation property of the tick labels with this line:您可以使用此行设置刻度标签的旋转属性:

plt.setp(axa.xaxis.get_majorticklabels(), rotation=45)

setp is a utility function to set a property of multiple artists (all ticklabels in this case). setp是一个实用函数,用于设置多个艺术家的属性(在本例中为所有刻度标签)。

BTW: There is no difference between a 'normal' and a subplot in matplotlib.顺便说一句:matplotlib 中的“正常”和子图之间没有区别。 Both are just Axes objects.两者都只是 Axes 对象。 The only difference is the size and position and the number of them in the same figure.唯一的区别是大小和位置以及它们在同一图中的数量。

Just wanted to add another solution that I found on the matplotlib git discussion page :只是想添加我在matplotlib git 讨论页面上找到的另一个解决方案:

ax[your_axis].tick_params(axis='x', rotation=90)

You can specify the axis you want by passing in the specific paramater.您可以通过传入特定参数来指定所需的轴。 The advantage of this method over the accepted answer is that you can control which axis this change is applied to.与公认答案相比,此方法的优势在于您可以控制此更改应用于哪个轴。 Other parameters can be found here 其他参数可以在这里找到

ax[your_axis].tick_params(axis='x', rotation=90) This is correct answer. ax[your_axis].tick_params(axis='x', rotation=90) 这是正确答案。 I tried it on my code.我在我的代码上试过了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM