-1

I want to rotate the automatically-generated x-axis labels of my plot in order to prevent the labels from overlapping:

I have been advised that matplotlib.pyplot.setp is worth a look, but I don't know how to use it in conjunction with labels that are generated automatically.

How can I rotate these labels?

x = [element[0] for element in eventDuplicates]
y = [element[1] for element in eventDuplicates]
figure = matplotlib.pyplot.figure()
figure.suptitle("event duplicates", fontsize = 20)
matplotlib.pyplot.scatter(x, y, s = 1, alpha = 1)
axes = matplotlib.pyplot.gca()
axes.yaxis.set_major_formatter(FormatStrFormatter("%.0f"))
axes.xaxis.set_major_formatter(FormatStrFormatter("%.0f"))
axes.set_xlim(left = 0)
#axes.set_ylim(bottom = 0)
#matplotlib.pyplot.setp(rotation = 90) # <--- insert magic here
matplotlib.pyplot.xlabel("run numbers")
matplotlib.pyplot.ylabel("event numbers")
matplotlib.pyplot.savefig("diagnostic_event_duplicates_scatter.png")
d3pd
  • 7,935
  • 24
  • 76
  • 127

1 Answers1

2

Something like this?

import numpy as np
import matplotlib.pylab as pl

import matplotlib as mpl
mpl.rcParams['axes.formatter.useoffset'] = False

x = np.arange(10000000, 10000010, 1)
y = np.cos(x)

pl.figure()
pl.subplot(121)
pl.plot(x,y)

pl.subplot(122)
pl.plot(x,y)
locs, labels = pl.xticks()
pl.setp(labels, rotation=90)

enter image description here

Bart
  • 9,825
  • 5
  • 47
  • 73