Matplotlib subplots_adjust hspace以便标题和xlabel不重叠? [英] Matplotlib subplots_adjust hspace so titles and xlabels don't overlap?

查看:516
本文介绍了Matplotlib subplots_adjust hspace以便标题和xlabel不重叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在matplotlib中有3行子图,其中一行的xlabels可以与下一行的标题重叠.人们不得不摆弄pl.subplots_adjust(hspace),这很烦人.

With, say, 3 rows of subplots in matplotlib, xlabels of one row can overlap the title of the next. One has to fiddle with pl.subplots_adjust(hspace), which is annoying.

hspace是否有防止重叠并适用于任何笔尖的食谱?

Is there a recipe for hspace that prevents overlaps and works for any nrow?

""" 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回调中的异常)
  • matplotlib 0.99.1.1,
  • Python 2.6.4,
  • Mac OSX 10.4.11,
  • backend: Qt4Agg (TkAgg => Exception in Tkinter callback)

(有很多要点,任何人都可以按照Tcl/Tk书第17章包装器"的概述来概述matplotlib的包装器/分隔器的工作原理吗?)

(For many extra points, can anyone outline how matplotlib's packer / spacer works, along the lines of chapter 17 "the packer" in the Tcl/Tk book?)

推荐答案

我觉得这很棘手,但是上面有一些信息

I find this quite tricky, but there is some information on it here at the MatPlotLib FAQ. It is rather cumbersome, and requires finding out about what space individual elements (ticklabels) take up...

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

Update: The page states that the tight_layout() function is the easiest way to go, which attempts to automatically correct spacing.

否则,它显示了获取各种元素(例如标签)大小的方法,因此您可以校正轴元素的间距/位置.这是上述FAQ页面中的示例,它确定了非常宽的y轴标签的宽度,并相应地调整了轴宽度:

Otherwise, it shows ways to acquire the sizes of various elements (eg. labels) so you can then correct the spacings/positions of your axes elements. Here is an example from the above FAQ page, which determines the width of a very wide y-axis label, and adjusts the axis width accordingly:

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 subplots_adjust hspace以便标题和xlabel不重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆