2013-07-19 413 views
0

充分旋转后,我的条形图列的标签被切掉。这里是一个要说明发生的事情为例:防止x轴标签在matplotlib/pyplot中被切碎

import matplotlib.pyplot as plt 

x = [1,2,3] 
y = [2,3,4] 
s = ['long_label_000000000','long_label_000000001','long_label_000000002'] 
plt.bar(x,y) 
plt.xticks(range(len(s)),s, rotation=90) 
plt.show() 

我知道,执行以下操作将导致图表将按照画布大小自动调整:

from matplotlib import rcParams 
rcParams.update({'figure.autolayout':True}) 

然而,这种调整高度的图来容纳标签(包括如果标签足够大时产生压扁的图),并且我宁愿保持统一的图尺寸。

任何人都可以推荐一种方法来扩大画布的底部,如果标签被切碎?谢谢。

回答

1

初始化图形时,您可以使用figsize=(w,h)来控制图形大小。在addtion您可以手动与subplotsubplots_adjust控制你的轴位置:

w = 12 # width in inch 
h = 12 # height in inch 

fig = plt.figure(figsize=(w,h)) 

ax = fig.add_subplot(111) 
plt.subplots_adjust(bottom=0.25) 

x = [1,2,3] 
y = [2,3,4] 
s = ['long_label_000000000','long_label_000000001','long_label_000000002'] 
ax.bar(x,y) 
ax.set_xticks(range(len(s))) 
ax.set_xticklabels(s,rotation=90) 
plt.show() 
+0

没有“rcParams.update({‘figure.autolayout’:真})”行,这导致标签像以前一样得到切碎。随着这条线,图像会像以前一样被压扁。 – Lamps1829

+0

@ Lamps1829看我的编辑。如果那是过多的人工参与,我 - 不幸的是 - 出于想法。 – Schorsch

+1

@Schorsh:谢谢 - adjust_bottom()部分是个不错的主意。如果它可以与检测文本是否被切割的东西相结合,那将是理想的,但是我发现这比rcParams解决方案更适合。 – Lamps1829