2010-03-10 86 views
32

例如,在matplotlib中有3排子圖,一行的xlabels可以與下一個的標題重疊。人們不得不撥弄pl.subplots_adjust(hspace),這很煩人。Matplotlib subplots_adjust hspace標題和xlabel不重疊?

是否有配方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,
  • 後端:Qt4AggTkAgg =>異常中Tkinter回調)

(For許多額外的點,任何人都可以概述matplotlib的打包器/間隔器如何工作,沿着Tcl/Tk書中的第17章「打包器」?)

+2

你可能想提交bug追蹤http://sourceforge.net/tracker/?group_id=80706這個錯誤/心願項在matplotlib – 2010-03-18 01:16:11

+2

你試過'PL。 'pl.show()'之前的'tight_layout()'是否爲「自動」解決方案? – Sebastian 2015-07-07 04:15:52

+0

@Sebastian Raschka,「UserWarning:tight_layout:回退到Agg呈現器」,matplotlib 1.4.3在mac上。 (這個問題是5年前的。) – denis 2015-07-10 11:31:42

回答

16

我覺得這很棘手,但有一些關於它的信息here at the MatPlotLib FAQ 。這是相當麻煩的,需要找出什麼空間中的各個元素(ticklabels)佔用...

更新: 頁面狀態的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() 
+0

接受,但確實麻煩 - mttiw,比它的價值更麻煩 – denis 2010-04-02 10:45:49

+0

鏈接離開了。這個答案事實上變得毫無用處。 – jojo 2015-07-20 16:09:43

+1

鏈接再次存在 - 說'tight_layout()'現在是要走的路,確實是這樣。 – Demis 2016-01-04 01:11:15

39

可以使用plt.subplots_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 
+3

這個問題說:「人們不得不擺弄pl.subplots_adjust(hspace),討厭。」 – denis 2011-07-01 08:23:16