2011-12-04 6 views
0

matplotlib:http://www.scipy.org/Cookbook/Matplotlib/HintonDiagramsでヒントダイアグラムを作成する方法に関するこの例が見つかりました。matplotlibのヒントダイアグラムをサブテンプレートに追加する際に、軸ラベルを壊さずに追加する

私はので、私はそう

import matplotlib.pyplot as plt 
import numpy as N 

def hintonForTwo(W1, W2, maxWeight=None): 

    height, width = W1.shape 
    if not maxWeight: 
     maxWeight = 2**N.ceil(N.log(N.max(N.abs(W1)))/N.log(2)) 

    fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, sharey=True) 

    hintonSubPlot(ax1, W1, maxWeight) 
    ax1.set_yticks(N.arange(height)) 
    # the input matrices will have row height of 7 
    # and years 1981, 1986... up to 2011 are supposed to be the ytick labels 
    ax1.set_yticklabels(tuple(range(1981,2012,5))) 
    ax1.set_aspect('equal') 
    ax1.set_axis_off() 
    hintonSubPlot(ax2, W2, maxWeight) 
    return fig, ax1, ax2 

def hintonSubPlot(F, W, maxWeight): 
    height, width = W.shape 
    F.fill(N.array([0,width,width,0]),N.array([0,0,height,height]),'white') 

    for x in xrange(width): 
     for y in xrange(height): 
       _x = x+1 
      _y = y+1 
      w = W[y,x] 
      if w > 0: 
       _blobAlt(F, _x - 0.5, height - _y + 0.5, min(1,w/maxWeight),'black') 
      elif w < 0: 
       _blobAlt(F, _x - 0.5, height - _y + 0.5, min(1,-w/maxWeight),'gray') 
    return F 

def _blobAlt(F, x,y,area,colour): 
    hs = N.sqrt(area)/2 
    xcorners = N.array([x - hs, x + hs, x + hs, x - hs]) 
    ycorners = N.array([y - hs, y - hs, y + hs, y + hs]) 
    F.fill(xcorners, ycorners, colour, edgecolor=colour) 

dim = (7,50) 
X1 = N.ones(dim) * N.nan 
X2 = N.ones(dim) * N.nan 

rm = N.random.random(dim) 
X1[rm > 2.0/3] = 1 
X1[rm < 1.0/3] = -1 

rm = N.random.random(dim) 
X2[rm > 2.0/3] = 1 
X2[rm < 1.0/3] = -1 

f, a, b = hintonForTwo(X1, X2) 
f.savefig('tmp.png') 

のようにそれを書き直した。しかし、私は修正することはできません。このにはいくつかの問題がある目盛りのラベルで複数の図とy軸を必要:

  1. 場合do ax1.set_axis_off()、ytickラベルを取得できません
  2. 軸をオンのままにすると、下位のサブ図の周囲に大きな空白の矩形が表示されます
  3. 軸をオフに設定しても、アッパーsubfigと私は

は、どのように私はytickラベルを取得しますが、subfigs周囲および上部と下部との間に大きなギャップのない大きな白い長方形なくてはならないが、狭いことができます下subfigとの間に大きなギャップがありますか?

+0

を、あなたのコードが不完全ですあなたにテスト済みの解決策を教えてください – joaquin

+0

@joaquinコードを追加してコピー/貼り付けと実行が可能になりました。 – JasonMond

答えて

0

あなたのコードはまだ不完全です。しかし、あなただけの、サブプロット間の間隔を調整し、選択いくつかのレベルを削除したい場合は、あなたが試すことがあります。あなたの姿を再現し、しようとするかのうないように、

import numpy as np 
import pylab as p 


fig = p.figure() 
fig.subplots_adjust(left=0.145, bottom=0.13, right=.87, top=.975, wspace=.18, hspace=.10) 

x=np.arange(1,10,0.01) 
ax1=fig.add_subplot(211) 
ax1.plot(x,np.sin(x),'ro') 
ax1.set_ylabel('sin(x)') 

ax2=fig.add_subplot(212,sharex=ax1,sharey=ax1) 
ax2.plot(x,np.cos(x),'bs') 
ax2.set_xlabel('x') 
ax2.set_ylabel('cos(x)') 

for label in ax1.get_xticklabels(): 
    label.set_visible(False) 

p.show() 

Plot