如何旋转matplotlib图的自动生成的x轴标签?

时间:2015-11-20 19:49:42

标签: python matplotlib axis overlap labels

我想旋转我的绘图的自动生成的x轴标签,以防止标签重叠:

我被告知matplotlib.pyplot.setp值得一看,但我不知道如何将其与自动生成的标签结合使用。

如何旋转这些标签?

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")

1 个答案:

答案 0 :(得分:2)

这样的东西?

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