python - Matplotlib subplots_adjust hspace 使标题和 xlabels 不重叠?

标签 python matplotlib

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

hspace 是否有防止重叠并适用于任何 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 回调异常)

(对于许多额外的点,任何人都可以按照 Tcl/Tk 书中第 17 章“打包器”的内容概述 matplotlib 的打包器/间隔器的工作原理吗?)

最佳答案

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

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.tight_layout

http://matplotlib.org/users/tight_layout_guide.html#plotting-guide-tight-layout

关于python - Matplotlib subplots_adjust hspace 使标题和 xlabels 不重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2418125/

相关文章:

python - 如何在 Matplotlib 上绘制两个数据点之间的水平线?

python - 从重复数组中绘制 python 绘图

python - Matplotlib:如何从数组的列中绘制多条线,但给它们一个标签?

python - 如何通过 conda-build 配方安装 pip 包?

python - 数组的总大小必须保持不变

python - 另存为 .pdf 时,Matplotlib 图会失去透明度

python - 绘制 NetworkX Girvan-Newman 算法找到的社区的树状图

python - 为神秘主义者提供不平等/约束的向量

python - 名称 'set_hour' 未定义

python - 在Python中从SPSS访问标签时如何处理特殊字符?