2013-07-13 34 views
1

我試圖複製矩陣鄰接可視化,如this D3.js example所示。請注意,每個單元格都被填充,導致每個單元格周圍出現白色邊框。如何在D3.js風格中填充鄰接矩陣單元格?

這是我到目前爲止有:

img = matplotlib.pyplot.imshow(m, interpolation='none') 
img.axes.xaxis.tick_top() 
img.axes.xaxis.set_ticks_position('none') 
img.axes.yaxis.set_ticks_position('none') 
img.axes.spines['top'].set_color('none') 
img.axes.spines['bottom'].set_color('none') 
img.axes.spines['left'].set_color('none') 
img.axes.spines['right'].set_color('none') 
matplotlib.pyplot.set_cmap('gray_r') 
matplotlib.pyplot.xticks(range(len(m)), G.nodes(), rotation='vertical') 
matplotlib.pyplot.yticks(range(len(m)), G.nodes(), rotation='horizontal') 

我已經研究過通過每個單元迭代的方式,併爲其他插值技術,但我真的想保持不插在所有,因爲我想保持細胞正方形。有沒有人試圖做到這一點?

+0

是否有可能爲你使用['pyplot.pcolor'(http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.pcolor),而不是'pyplot .imshow'?使用'pcolor',你可以用kwarg'edgecolor ='w''在每個單元格周圍獲得一個白色邊框。 – hooy

+0

是的!這樣可行。如果您在評論中添加答案,我會選擇它。謝謝。 –

回答

3

一個可能的解決方案是使用pcolor方法pyplot,因爲它接受kwarg edgecolor

import matplotlib.pyplot as plt 
import numpy as np 

x = np.arange(6) 
y = np.arange(6) 

X, Y = np.meshgrid(x, y) 

Z = np.random.rand(5, 5) 

ax = plt.subplot(111, aspect='equal') # To make the cells square 
ax.pcolor(X, Y, Z, 
      edgecolor='white',   # Color of "padding" between cells 
      linewidth=2)    # Width of "padding" between cells 


plt.show() 

enter image description here