Rotate axis text in python matplotlib -
i can't figure out how rotate text on x axis. time stamp, number of samples increase, closer , closer until overlap. i'd rotate text 90 degrees samples closer together, aren't overlapping.
below have, works fine exception can't figure out how rotate x axis text.
import sys import matplotlib matplotlib.use('agg') import matplotlib.pyplot 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())) in values[1:]] delay = [float(i.split(',')[1].strip()) in values[1:]] plt.plot(time, delay) plt.grid(b='on') plt.savefig('test.png')
easy way
as described here, there existing method in matplotlib.pyplot figure class automatically rotates dates appropriately figure.
you can call after plot data (i.e.ax.plot(dates,ydata) :
fig.autofmt_xdate() if need format labels further, checkout above link.
non-datetime objects
as per languitar's comment, method suggested non-datetime xticks not update correctly when zooming, etc. if it's not datetime object used x-axis data, should follow tommy's answer:
for tick in ax.get_xticklabels(): tick.set_rotation(45)
Comments
Post a Comment