使用包裹的xticks或yticks标签调整matplotlib图周围的边距

时间:2017-03-31 07:06:42

标签: python matplotlib data-visualization

我想在matplotlib中创建一个包含一些相当长的刻度标签的图形。为了使标签适合合理的空间,我尝试使用matplotlib提供的包装:

plt.yticks([1],[a],wrap=True)

This works(output),然后刻度标签文本会延伸到图的边缘。当文本设置为换行时,如何在图形边缘和刻度标签文本之间放置边距?

尝试失败:

I tried to create the desired margin by expanding the bounding box(output)

f.savefig("stillnomargin.pdf",bbox_inches=f.bbox_inches.expanded(2,2))

但是,换行更改为将文本重新分配到新的图形边缘。

完整示例:

import matplotlib.pyplot as plt

labeltext = "This is a really long string that I'd rather have wrapped so that it"\
    " doesn't go outside of the figure, but if it's long enough it will go"\
    " off the top or bottom!"

a4_width =  8.27
a4_height = 11.69 

f = plt.figure()
plt.ylim([0,2])
plt.xlim([0,2])
plt.yticks([1],[labeltext],wrap=True)

f.set_size_inches(a4_width, a4_height)
plt.subplots_adjust(left=0.3) #leave space on left for large yticklabel


#  outputs full page, yticklabel text to edge
f.savefig("nomargin.pdf")

#  tried to expand bounding box to create margin, 
#  but yticklabel then rewraps to fills space to edge
f.savefig("stillnomargin.pdf",bbox_inches=f.bbox_inches.expanded(2,2))

0 个答案:

没有答案