2013-03-17 18 views
1

我使用matplotlib的ImageGrid輔助類創建了一個圖。這裏的代碼如下:如何爲使用ImageGrid創建的整個圖創建單軸標籤

from mpl_tookits.axes_grid1 import ImageGrid 
from matplotlib.pyplot import * 

fig = figure(figsize=(20, 12), dpi=300) 
grid = ImageGrid(fig, 111, nrows_ncols=(3, 4), axes_pad=1, aspect=False) 
for gridax, (i, sub) in zip(grid, enumerate(eyelink_data)): 
    subnum = i + start_with 

    # format data 
    xdat = sub['x'][(sub['in_trl'] == True) & (sub['x'].notnull()) & (sub['y'].notnull())] 
    ydat = sub['y'][(sub['in_trl'] == True) & (sub['x'].notnull()) & (sub['y'].notnull())] 

    # plot 
    gridax.hist2d(xdat, ydat, bins=[np.linspace(-.005, .005, num=1000), np.linspace(-.005, .005, num=1000)]) 
    gridax.plot(0, 0, 'ro') # origin 

show() 

我看着this cookbook page有關如何爲整個數字單x和y標籤信息,但這些食譜好像只適用於標準的次要情節標準的數字。

我也看了一下mpl_toolkits.axes_grid1.axes_divider.LocatableAxes的一些方法,但沒有什麼能跳出來。 同上mpl_toolkits.axes_grid1.axes_grid.ImageGrid中的方法。

有誰知道我可以用ImageGrid來做這個嗎?

+0

此刻在matplotlib中沒有對此功能的直接支持。您可以使用「文本」或軸標籤進行遊戲。不過,這裏有一個[github上的PR](https://github.com/matplotlib/matplotlib/pull/1519)。 – 2013-03-18 10:14:38

+0

@FrancescoMontesano,你能解釋一下我可能會如何處理'text'或'axis'嗎?我應該看什麼? – blz 2013-03-18 10:39:47

+0

我會盡量在稍後提出答案。順便說一下:您使用的是哪個版本的matplotlib。在v 1.2.0中,'mpl_tookits.axes_grid1'中沒有'ImageGrid':有一個'AxisGrid' – 2013-03-18 10:48:52

回答

2

this github pull request我嘗試了可能的實現數字寬x和y標籤(我很高興,我不是想有這樣的功能只有一個。

此代碼重述要點如果交互工作,或者想在一個函數/類來封裝公關執行和產生this figure。測試針對matplotlib主進賬今日(2013年3月18日)。

from mpl_toolkits.axes_grid1 import ImageGrid 
import matplotlib.pyplot as plt 

fig = plt.figure(figsize=(20, 12)) 
grid = ImageGrid(fig, 111, nrows_ncols=(3, 4), axes_pad=1, aspect=False) 

#get the extent of the largest box containing all the axes/subplots 
extents = np.array([a.get_position().extents for a in grid]) #all axes extents 
bigextents = np.empty(4) 
bigextents[:2] = extents[:,:2].min(axis=0) 
bigextents[2:] = extents[:,2:].max(axis=0) 

#text to mimic the x and y label. The text is positioned in the middle 
labelpad=0.08 #distance between the external axis and the text 
xlab_t = fig.text((bigextents[2]+bigextents[0])/2, bigextents[1]-labelpad, 'x label', 
    horizontalalignment='center', verticalalignment = 'bottom') 
ylab_t = fig.text(bigextents[0]-labelpad, (bigextents[3]+bigextents[1])/2, 'y label', 
    rotation='vertical', horizontalalignment = 'left', verticalalignment = 'center') 

,你可以擺脫舊標籤用的[xy]lab_t.set_visible(False)繪製新標籤之前(有一個text.remove()方法但尚未實施)。

採用這種方法有幾個需要注意的地方:

  1. plt.tight_layout工作:這是由於這樣的事實,這種方法是針對subplots不向matplotlib.Figure對象

  2. 幾個月前有一個想法/公關包括所有artists當保存一個數字bbox_inches='tight',但我不知道這個狀態。