Matplotlib subplots_调整 hspace 以便标题和 xlabel 不重叠?
例如,对于 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 的打包器/间隔器是如何工作的?)
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.
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()
My versions:
- matplotlib 0.99.1.1,
- Python 2.6.4,
- Mac OSX 10.4.11,
- backend:
Qt4Agg
(TkAgg
=> Exception in Tkinter callback)
(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?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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 #绘图指南紧密布局
The link posted by Jose has been updated and pylab now has a
tight_layout()
function that does this automatically (in matplotlib version 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
我发现这很棘手,但有一些信息
更新:
该页面指出,
tight_layout()
函数是最简单的方法,它会尝试自动更正间距。否则,它显示了获取各种元素(例如标签)大小的方法,以便您可以纠正轴元素的间距/位置。以下是上述常见问题解答页面中的示例,它确定非常宽的 y 轴标签的宽度,并相应地调整轴宽度:
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...
Update:
The page states that the
tight_layout()
function is the easiest way to go, which attempts to automatically correct spacing.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: