Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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') 
like image 436
tMC Avatar asked Jun 12 '12 14:06

tMC


People also ask

How do I rotate axis labels in MatPlotLib?

Rotate X-Axis Tick Labels in Matplotlib There are two ways to go about it - change it on the Figure-level using plt. xticks() or change it on an Axes-level by using tick. set_rotation() individually, or even by using ax. set_xticklabels() and ax.

How do I change the direction of my axis in MatPlotLib?

In Matplotlib we can reverse axes of a graph using multiple methods. Most common method is by using invert_xaxis() and invert_yaxis() for the axes objects. Other than that we can also use xlim() and ylim(), and axis() methods for the pyplot object.


1 Answers

This works for me:

plt.xticks(rotation=90) 
like image 92
scottlittle Avatar answered Sep 17 '22 21:09

scottlittle