Python Matplotlib子地块调整hspace,使标题和XLabel不';不重叠?

Python Matplotlib子地块调整hspace,使标题和XLabel不';不重叠?,python,matplotlib,Python,Matplotlib,例如,在matplotlib中有3行子图,一行的xlabel可以与下一行的标题重叠。人们不得不摆弄pl.subplots\u adjust(hspace),这很烦人 是否有防止重叠并适用于任何nrow的hspace配方 """ matplotlib xlabels overlap titles ? """ import sys import numpy as np import pylab as pl nrow = 3 hspace = .4 # of plot height, titles

例如,在matplotlib中有3行子图,一行的
xlabel
可以与下一行的标题重叠。人们不得不摆弄
pl.subplots\u adjust(hspace)
,这很烦人

是否有防止重叠并适用于任何nrow的
hspace
配方

""" matplotlib xlabels overlap titles ? """
import sys
import numpy as np
import pylab as pl

nrow = 3
hspace = .4  # of plot height, titles and xlabels both fall within this ??
exec "\n".join( sys.argv[1:] )  # nrow= ...

y = np.arange(10)
pl.subplots_adjust( hspace=hspace )

for jrow in range( 1, nrow+1 ):
    pl.subplot( nrow, 1, jrow )
    pl.plot( y**jrow )
    pl.title( 5 * ("title %d " % jrow) )
    pl.xlabel( 5 * ("xlabel %d " % jrow) )

pl.show()
我的版本:

  • matplotlib 0.99.1.1
  • Python 2.6.4
  • Mac OSX 10.4.11
  • 后端:
    Qt4Agg
    TkAgg
    =>Tkinter回调异常)

(关于许多额外的要点,有人能按照Tcl/Tk书中第17章“封隔器”的思路概述matplotlib的封隔器/隔套是如何工作的吗?

我觉得这很棘手,但有一些相关信息。它相当麻烦,需要找出单个元素(标签)占用的空间

更新: 该页面指出,
tight_layout()
函数是最简单的方法,它尝试自动更正间距

否则,它将显示获取各种元素(例如标签)的大小的方法,以便您可以更正轴元素的间距/位置。以下是上述常见问题页面中的一个示例,该示例确定非常宽的y轴标签的宽度,并相应地调整轴宽度:

import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10))
ax.set_yticks((2,5,7))
labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))

def on_draw(event):
   bboxes = []
   for label in labels:
       bbox = label.get_window_extent()
       # the figure transform goes from relative coords->pixels and we
       # want the inverse of that
       bboxi = bbox.inverse_transformed(fig.transFigure)
       bboxes.append(bboxi)

   # this is the bbox that bounds all the bboxes, again in relative
   # figure coords
   bbox = mtransforms.Bbox.union(bboxes)
   if fig.subplotpars.left < bbox.width:
       # we need to move it over
       fig.subplots_adjust(left=1.1*bbox.width) # pad a little
       fig.canvas.draw()
   return False

fig.canvas.mpl_connect('draw_event', on_draw)

plt.show()
导入matplotlib.pyplot作为plt
将matplotlib.transforms作为MTTransforms导入
图=plt.图()
ax=图添加_子批次(111)
ax.绘图(范围(10))
ax.集合点((2,5,7))
labels=ax.set_yticklabels(('really,really,really','long','labels'))
def on_draw(事件):
bboxes=[]
对于标签中的标签:
bbox=label.get\u window\u extent()
#图形变换从相对坐标->像素开始,我们
#你想要相反的结果吗
bboxi=bbox。逆_变换(图变形)
bboxes.append(bboxi)
#这是限制所有bbox的bbox,同样是相对的
#图形坐标
bbox=mtransforms.bbox.union(bboxes)
如果fig.Subplopars.left
您可以使用plt.subplot\u adjust更改子地块链接之间的间距

subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)

left  = 0.125  # the left side of the subplots of the figure
right = 0.9    # the right side of the subplots of the figure
bottom = 0.1   # the bottom of the subplots of the figure
top = 0.9      # the top of the subplots of the figure
wspace = 0.2   # the amount of width reserved for blank space between subplots
hspace = 0.2   # the amount of height reserved for white space between subplots

Jose发布的链接已经更新,pylab现在有一个自动执行此操作的
tight_layout()
函数(在matplotlib版本1.1.0中)


您可能想在matplotlib bugtracker上为此提交一个bug/wishlist条目您在
pl.show()
之前是否尝试过
pl.show()
以获得“自动”解决方案?@Sebastian Raschka,“用户警告:紧密布局:退回到Agg渲染器”,mac上的matplotlib 1.4.3。(问题是5年前的事了。)@denis Oops,对不起,当我被问到数据时,我真的没有注意到它……接受,但确实很麻烦——mttiw,比它更麻烦的是,链接消失了。这个答案事实上毫无用处。链接再次存在——说
tight\u layout()
现在是正确的选择,事实确实如此。
tight\u layout()
不能解释图的标题。请参阅以获得解决方案。问题是“必须摆弄pl.subplots\u adjust(hspace),这很烦人。”
tight\u layout()
不能解释figure suptitle。请参阅以获得解决方案。