13

I am plotting the following chart :

enter image description here

with the following code:

fig, ax = plt.subplots(figsize=(20, 3))
mpf.candlestick_ohlc(ax,quotes, width=0.01)
ax.xaxis_date()
ax.xaxis.set_minor_locator(mpl.dates.HourLocator(interval=4) )
ax.xaxis.set_minor_formatter(mpl.dates.DateFormatter('%H:%M')) 
plt.xticks(rotation = 90)

plt.grid(True)
plt.show()

I would like to also rotate the minor ticks: How would i do that?

Subsidiary question is there a way to rotate both major and minor tick with a single command?

Serenity
  • 35,289
  • 20
  • 120
  • 115
jim jarnac
  • 4,804
  • 11
  • 51
  • 88

3 Answers3

14

You may rotate by code of one line plt.setp(ax.xaxis.get_minorticklabels(), rotation=90).

Serenity
  • 35,289
  • 20
  • 120
  • 115
5

While dealing with the problem myself, I discovered that you can also easily accomplish this with a single statement using the tick_params:

ax.tick_params(axis="x", which="both", rotation=45)

This will rotate labels on your x axis, and the which option allows you to choose between minor, major or both. In case you have multiple plots you will have to do this for every plot in the figure.

DannyVanpoucke
  • 100
  • 1
  • 8
2

By exploring a little, I discovered that ax.get_xminorticklabels() is a list with a text class element.

>>> print(type(ax.get_xminorticklabels()[0])) 
<class 'matplotlib.text.Text'>

And text can be rotated!

>>> for text in ax.get_xminorticklabels():
>>>     text.set_rotation(90)

xminorticklabels_rotate_example

You only have to be careful that they do not overlap.

Lucas
  • 6,869
  • 5
  • 29
  • 44