50

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!

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
andreas-p
  • 663
  • 1
  • 5
  • 8

3 Answers3

111

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)
tommy.carstensen
  • 8,962
  • 15
  • 65
  • 108
  • 54
    By default `tick_params` applies the rotation to both axis of the plot, to rotate only the x-axis: `ax.tick_params('x', labelrotation=45) ` – Sarah N Jan 13 '20 at 13:56
  • 9
    Why are so many ways to do this, and why do some of them work sometimes and some of them never work? This is why matplotlib frustrates me. The lack of consistency across use cases makes it a library I want to walk away from, if there was a better option. – rocksNwaves May 05 '20 at 21:14
  • @rocksNwaves In Python itself there are also multiple ways of achieving the same thing. It took me a few years to get used to matplotlib. I'm still learning. Previously I was using gnuplot, but matplotlib is much more powerful; especially in a Python context. I still like the simplicity of gnuplot a lot; and occasionally I use it for doing plots from the command line. – tommy.carstensen May 06 '20 at 17:32
  • 4
    By default, this rotates the text around the center point, which is not that useful when you have long labels, so I like to use `ax.set_xticklabels(labels, rotation=45, ha='right')` to force right-aligned text – Michael Clerx Dec 03 '21 at 12:48
23

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

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.

hitzg
  • 12,133
  • 52
  • 54
19

Just wanted to add another solution that I found on the matplotlib git discussion page:

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

PeptideWitch
  • 2,239
  • 14
  • 30