0

I am trying to rotate the x axis labels for every subplot. Here is my code:

fig.set_figheight(10)
fig.set_figwidth(20)
ax.set_xticklabels(dr_2012['State/UT'], rotation = 90)

ax[0, 0].bar(dr_2012['State/UT'], dr_2012['Primary Total'])
ax[0, 0].set_title('Dropout Ratios 2012-2013 (Primary)')

ax[0, 1].bar(dr_2012['State/UT'], dr_2012['Upper Primary Total'])
ax[0, 1].set_title('Dropout Ratios 2012-2013 (Upper Primary)')

ax[1, 0].bar(dr_2012['State/UT'], dr_2012['Secondary Total'])
ax[1, 0].set_title('Dropout Ratios 2012-2013 (Secondary)')

ax[1, 1].bar(dr_2012['State/UT'], dr_2012['HS Total'])
ax[1, 1].set_title('Dropout Ratios 2012-2013 (HS)')

subplot

None of the usual things seem to work for me. I have tried ax.set_xticklabels and ax.tick_params. I have also tried looping through the ticks using ax.get_xticklabels and even that didn't work. It always gave me this error -

AttributeError: 'numpy.ndarray' object has no attribute 'set_xticklabels'/'get_xticklabels'/'tick_params'

I am at a loss. Why wouldn't it be working?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
queenkrazykat
  • 11
  • 1
  • 1
  • 1
  • what about `fig.xticks(dr_2012['State/UT'], rotation = "vertical")`? – M Z Aug 10 '20 at 13:11
  • Hey, in order to get better answers please follow this guide for posting questions on Stack Overflow https://stackoverflow.com/help/minimal-reproducible-example. That being said I think you need to set if for each plot in your subplot. – Tobias P. G. Aug 10 '20 at 13:11
  • 1
    Does this answer your question? [Rotate axis text in python matplotlib](https://stackoverflow.com/questions/10998621/rotate-axis-text-in-python-matplotlib) – DavidG Aug 10 '20 at 14:00

1 Answers1

7

Use tick_params on the AxesSubplot, but ax in your case is an np array of AxesSubplot objects.

Fix

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

Sample usage

import matplotlib.pyplot as plt
fig,ax =  plt.subplots(2,2)
import numpy as np
x = np.arange(1,5)
ax[0][0].plot(x,x*x)
ax[0][0].set_title('square')
ax[0][0].tick_params(axis='x', rotation=90)

ax[0][1].plot(x,np.sqrt(x))
ax[0][1].set_title('square root')
ax[0][1].tick_params(axis='x', rotation=90)

ax[1][0].plot(x,np.exp(x))
ax[1][0].set_title('exp')
ax[1][0].tick_params(axis='x', rotation=90)

ax[1][1].plot(x,np.log10(x))
ax[1][1].set_title('log')
ax[1][1].tick_params(axis='x', rotation=90)

plt.show()

Output:

enter image description here

Adriaan
  • 17,741
  • 7
  • 42
  • 75
mujjiga
  • 16,186
  • 2
  • 33
  • 51