ChatGPT解决这个技术问题 Extra ChatGPT

Rotate axis text in python matplotlib

I can't figure out how to rotate the text on the X Axis. Its a time stamp, so as the number of samples increase, they get closer and closer until they overlap. I'd like to rotate the text 90 degrees so as the samples get closer together, they aren't overlapping.

Below is what I have, it works fine with the exception that I can't figure out how to rotate the X axis text.

import sys

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import datetime

font = {'family' : 'normal',
        'weight' : 'bold',
        'size'   : 8}

matplotlib.rc('font', **font)

values = open('stats.csv', 'r').readlines()

time = [datetime.datetime.fromtimestamp(float(i.split(',')[0].strip())) for i in values[1:]]
delay = [float(i.split(',')[1].strip()) for i in values[1:]]

plt.plot(time, delay)
plt.grid(b='on')

plt.savefig('test.png')
See this answer for important stuff about aligning the rotated labels with ha (horizontal align)

s
scottlittle

This works for me:

plt.xticks(rotation=90)

In case anyone else is using mpld3, and is frustrated about none of these solutions working. Rotation is not supported in mpld3.
Extra points for making these stay inside the figure/window. Mine are half outside.
Call this after plotting. Ie, first ax.plot(...) then plt.xticks(rotation=90)
add plt.tight_layout() to let them stay inside the figure
Just figured it out. Activate the axis with plt.sca(ax) , and use this answer.
g
getup8

Many "correct" answers here but I'll add one more since I think some details are left out of several. The OP asked for 90 degree rotation but I'll change to 45 degrees because when you use an angle that isn't zero or 90, you should change the horizontal alignment as well; otherwise your labels will be off-center and a bit misleading (and I'm guessing many people who come here want to rotate axes to something other than 90).

Easiest / Least Code

Option 1

plt.xticks(rotation=45, ha='right')

As mentioned previously, that may not be desirable if you'd rather take the Object Oriented approach.

Option 2

Another fast way (it's intended for date objects but seems to work on any label; doubt this is recommended though):

fig.autofmt_xdate(rotation=45)

fig you would usually get from:

fig = plt.gcf()

fig = plt.figure()

fig, ax = plt.subplots()

fig = ax.figure

Object-Oriented / Dealing directly with ax

Option 3a

If you have the list of labels:

labels = ['One', 'Two', 'Three']
ax.set_xticks([1, 2, 3])
ax.set_xticklabels(labels, rotation=45, ha='right')

In later versions of Matplotlib (3.5+), you can just use set_xticks alone:

ax.set_xticks([1, 2, 3], labels, rotation=45, ha='right')

Option 3b

If you want to get the list of labels from the current plot:

# Unfortunately you need to draw your figure first to assign the labels,
# otherwise get_xticklabels() will return empty strings.
plt.draw()
ax.set_xticks(ax.get_xticks())
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right')

As above, in later versions of Matplotlib (3.5+), you can just use set_xticks alone:

ax.set_xticks(ax.get_xticks(), ax.get_xticklabels(), rotation=45, ha='right')

Option 4

Similar to above, but loop through manually instead.

for label in ax.get_xticklabels():
  label.set_rotation(45)
  label.set_ha('right')

Option 5

We still use pyplot (as plt) here but it's object-oriented because we're changing the property of a specific ax object.

plt.setp(ax.get_xticklabels(), rotation=45, ha='right')

Option 6

This option is simple, but AFAIK you can't set label horizontal align this way so another option might be better if your angle is not 90.

ax.tick_params(axis='x', labelrotation=45)

Edit: There's discussion of this exact "bug" but a fix hasn't been released (as of 3.4.0): https://github.com/matplotlib/matplotlib/issues/13774


You could add to option 2: fig = plt.gcf() - great answer BTW, it helped me immensely.
Also, the Option 6 bug is annoyingly still present in v3.4.0.
Options 3a, 3b seem to be discontinued: they raise UserWarning (this one: stackoverflow.com/questions/63723514 )
@creanion thanks I've updated so that those options shouldn't give warnings anymore (and added new syntax for Matplotlib 3.5+)
r
ryanjdillon

Easy way

As described here, there is an existing method in the matplotlib.pyplot figure class that automatically rotates dates appropriately for you figure.

You can call it after you plot your data (i.e.ax.plot(dates,ydata) :

fig.autofmt_xdate()

If you need to format the labels further, checkout the above link.

Non-datetime objects

As per languitar's comment, the method I suggested for non-datetime xticks would not update correctly when zooming, etc. If it's not a datetime object used as your x-axis data, you should follow Tommy's answer:

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

This is best way because it is concise, it operates on existing automatically generated ticks, and it adjusts plot margins so that it's readable. Other methods let the ticks go off the page, requiring further action (such as plt.tight_layouts()) to adjust them.
In case you have used a locator and formatter, the "other" method might break their effect if applied afterwards.
o
opiethehokie

Try pyplot.setp. I think you could do something like this:

x = range(len(time))
plt.xticks(x,  time)
locs, labels = plt.xticks()
plt.setp(labels, rotation=90)
plt.plot(x, delay)

Traceback (most recent call last): File "plotter.py", line 23, in <module> plt.setp(time, rotation=90) File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 183, in setp ret = _setp(*args, **kwargs) File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 1199, in setp func = getattr(o, funcName) AttributeError: 'datetime.datetime' object has no attribute 'set_rotation'
I think I missed a step earlier. Does the edited example help? I'm far from a matplotlib expert though, there could be a better way.
This doesn't seem to work at all- The image (on my machine) gets completely messed up. black lines all over the output image... Almost looks like a bar code. its very odd.
Which version of matplotlib was this? With 1.2.0, setp(subplot(111).get_xticklabels(), rotation=90) works fine with the Qt4Agg backend. In my case, it gets reset by plt.hist(), so I call it after, though plt.plot() doesn't reset it, so you should be ok.
@tMC With mpl 1.2.0, I copied your code (with some dummy data), added locs, labels = plt.xticks() plt.setp(labels, rotation=90) just before plt.plot(time, delay) and it worked fine.
M
Matthew

Appart from

plt.xticks(rotation=90)

this is also possible:

plt.xticks(rotation='vertical')

It works, but the height of the plot have some problem
plt.xticks(rotation=45) plt.savefig(nomefile,dpi=150, optimize=True) plt.clf()
c
cjohnson318

I came up with a similar example. Again, the rotation keyword is.. well, it's key.

from pylab import *
fig = figure()
ax = fig.add_subplot(111)
ax.bar( [0,1,2], [1,3,5] )
ax.set_xticks( [ 0.5, 1.5, 2.5 ] )
ax.set_xticklabels( ['tom','dick','harry'], rotation=45 ) ;

This looks like it works, but it shows the time as seconds since the epoch- not the timestamp from datetime
k
kiril

If you want to apply rotation on the axes object, the easiest way is using tick_params. For example.

ax.tick_params(axis='x', labelrotation=90)

Matplotlib documentation reference here.

This is useful when you have an array of axes as returned by plt.subplots, and it is more convenient than using set_xticks because in that case you need to also set the tick labels, and also more convenient that those that iterate over the ticks (for obvious reasons)


In addition to that, this solution also works in pandas dataframe's plot function since it returns axes object as well. Check here. Quite handy function. pd_dataframe.plot().tick_params(axis='x', labelrotation=90).
Only issue with this is that I can't figure out how to horizontally align the labels correctly if using something like labelrotation=30.
M
Manavalan Gajapathy

If using plt:

plt.xticks(rotation=90)

In case of using pandas or seaborn to plot, assuming ax as axes for the plot:

ax.set_xticklabels(ax.get_xticklabels(), rotation=90)

Another way of doing the above:

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

All those methods have been shown already in other answers. What is this answer adding here?
@ImportanceOfBeingErnest It's meant to help those using pandas or seaborn, and not matplotlib directly, for plotting.
for tick in ax.get_xticklabels(): tick.set_rotation(45) did the trick for me.
Thanks, I was using plt.xticks(rotation=45) to no avail until I came across this answer
T
Tommy

My answer is inspired by cjohnson318's answer, but I didn't want to supply a hardcoded list of labels; I wanted to rotate the existing labels:

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

This doesn't seem to work any more. There's no effect whatsoever.
R
Roberto Marzocchi

The simplest solution is to use:

plt.xticks(rotation=XX)

but also

# Tweak spacing to prevent clipping of tick-labels
plt.subplots_adjust(bottom=X.XX)

e.g for dates I used rotation=45 and bottom=0.20 but you can do some test for your data


I
Ishan Tomar
import pylab as pl
pl.xticks(rotation = 90)

Q
Quinnan Gill

To rotate the x-axis label to 90 degrees

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

I like how short your answer is, but what you posted is not valid Python code. It's not clear to me how to interpret it as valid python code.
A
Ajean

It will depend on what are you plotting.

import matplotlib.pyplot as plt

 x=['long_text_for_a_label_a',
    'long_text_for_a_label_b',
    'long_text_for_a_label_c']
y=[1,2,3]
myplot = plt.plot(x,y)
for item in myplot.axes.get_xticklabels():
    item.set_rotation(90)

For pandas and seaborn that give you an Axes object:

df = pd.DataFrame(x,y)
#pandas
myplot = df.plot.bar()
#seaborn 
myplotsns =sns.barplot(y='0',  x=df.index, data=df)
# you can get xticklabels without .axes cause the object are already a 
# isntance of it
for item in myplot.get_xticklabels():
    item.set_rotation(90)

If you need to rotate labels you may need change the font size too, you can use font_scale=1.0 to do that.