防止x轴标签在matplotlib / pyplot中被切断

时间:2013-07-19 13:27:52

标签: python matplotlib

如果旋转充分,我的条形图列的标签就会被切掉。这是一个应该说明会发生什么的例子:

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 个答案:

答案 0 :(得分:1)

初始化图形时,您可以使用figsize=(w,h)控制图形大小。此外,您可以使用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()