Rotate axis text in python matplotlib
我不知道如何在X轴上旋转文本。 这是一个时间戳记,因此随着样本数量的增加,它们越来越近,直到它们重叠。 我想将文本旋转90度,以使样本靠得更近,它们不会重叠。
下面是我所拥有的,除了我不知道如何旋转X轴文本外,它可以正常工作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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') |
这对我有用:
1 | plt.xticks(rotation=90) |
简单的方法
如此处所述,
您可以在绘制数据后调用它(即
1 | fig.autofmt_xdate() |
如果您需要进一步格式化标签,请查看上面的链接。
非日期时间对象
根据languitar的评论,我建议的非日期时间
1 2 | for tick in ax.get_xticklabels(): tick.set_rotation(45) |
尝试pyplot.setp。我认为您可以执行以下操作:
1 2 3 4 5 | x = range(len(time)) plt.xticks(x, time) locs, labels = plt.xticks() plt.setp(labels, rotation=90) plt.plot(x, delay) |
来自的Appart
1 | plt.xticks(rotation=90) |
这也是可能的:
1 | plt.xticks(rotation='vertical') |
我想出了一个类似的例子。同样,rotation关键字是..嗯,这是关键。
1 2 3 4 5 6 | 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 ) ; |
我的答案受到cjohnson318答案的启发,但我不想提供标签的硬编码列表。我想旋转现有标签:
1 2 | for tick in ax.get_xticklabels(): tick.set_rotation(45) |
如果使用
1 | plt.xticks(rotation=90) |
如果使用熊猫或海生动物进行绘图,则假设
1 | ax.set_xticklabels(ax.get_xticklabels(), rotation=90) |
完成上述操作的另一种方法:
1 2 | for tick in ax.get_xticklabels(): tick.set_rotation(45) |
如果要对轴对象施加旋转,最简单的方法是使用
1 | ax.tick_params(axis='x', labelrotation=90) |
Matplotlib文档参考在这里。
当您有一个由
这里有许多"正确"的答案,但由于我认为一些细节中遗漏了一些,因此我将再添加一个。 OP要求旋转90度,但我将更改为45度,因为当您使用非零或90度的角度时,还应该更改水平对齐方式;否则,您的标签将偏离中心并产生误导性(我猜很多来这里的人都想将轴旋转到90以外的位置)。
最简单/最少的代码
选项1
1 | plt.xticks(rotation=45, ha='right') |
如前所述,如果您宁愿采用面向对象的方法,那可能也不是所希望的。
选项2
另一种快速方法(该方法适用于日期对象,但似乎可以在任何标签上使用;不过建议不要这样做):
1 | fig.autofmt_xdate(rotation=45) |
-
fig = plt.figure() -
fig, ax = plt.subplots() -
fig = ax.figure
面向对象/直接与
选项3a
如果您有标签列表:
1 2 | labels = ['One', 'Two', 'Three'] ax.set_xticklabels(labels, rotation=45, ha='right') |
选项3b
如果要从当前图中获取标签列表:
1 2 3 4 | # Unfortunately you need to draw your figure first to assign the labels, # otherwise get_xticklabels() will return empty strings. plt.draw() ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right') |
选项4
与上述类似,但手动循环。
1 2 3 | for label in ax.get_xticklabels(): label.set_rotation(45) label.set_ha('right') |
选项5
我们仍然在这里使用
1 | plt.setp(ax.get_xticklabels(), rotation=45, ha='right') |
选项6
此选项很简单,但是AFAIK不能以这种方式设置标签水平对齐,因此,如果角度不为90,则另一个选项可能会更好。
1 | ax.tick_params(axis='x', labelrotation=45) |
编辑:
我们正在讨论这个确切的"错误",并且可能会为
https://github.com/matplotlib/matplotlib/issues/13774
1 2 | import pylab as pl pl.xticks(rotation = 90) |
这将取决于您要绘制的内容。
1 2 3 4 5 6 7 8 9 | 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) |
对于给您一个轴对象的大熊猫和海豚:
1 2 3 4 5 6 7 8 9 | 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) |
如果需要旋转标签,还可能需要更改字体大小,则可以使用
要将x轴标签旋转到90度
对于ax.get_xticklabels()中的刻度:
tick.set_rotation(45)