2015-09-10 148 views
1

我想繪製6個城鎮之間距離的距離矩陣圖。 A1到A3和B1到B3。一維數組的距離矩陣

我已經計算出了像A1-B1,A1-B2 ....的距離.... A3-B3和我得到了一維數組 我得到了一個1D numpy數組,用於6個城鎮之間的距離。

np.array(R) 
[ 3.00 2.50 1.00 3.3192 2.383 2.7128 3.8662 3.6724 3.5112] 

現在我想繪製一個距離矩陣格式的圖,看起來應該如下圖所示。

Each cell is colors according to its value.

它僅僅是一個代表性的數據。我有很多值,所以需要python程序。 任何建議或示例python matplotlib腳本將有所幫助。 此致敬禮。

+2

#2是不是代碼寫作服務 - 你嘗試過什麼等等遠? –

+0

@SimonGibbons 我知道。 我得到了不同數組中不同列的值。 正在嘗試... –

+2

我會建議展示您的代碼並解釋您當前被卡住的位置。這會讓我們更容易幫助你。 – cel

回答

1

看起來就像你最自己的方式。您可以通過將軸標籤更改爲A1, A2,...並打印其中的每個單元格的值來清理繪圖,使其更像您想要的。

腳本的清理版本低於:

import numpy as np 
import matplotlib.pyplot as plt 
R = np.array ([3.00, 2.50, 1.00, 3.3192, 2.383, 2.7128, 3.8662, 3.6724, 3.5112]) 

# Calculate the shape of the 2d array 
n = int(np.sqrt(R.size)) 
C = R.reshape((n,n)) 

# Plot the matrix 
plt.matshow(C,cmap="Reds") 

ax = plt.gca() 

# Set the plot labels 
xlabels = ["B%d" % i for i in xrange(n+1)] 
ylabels = ["A%d" % i for i in xrange(n+1)] 
ax.set_xticklabels(xlabels) 
ax.set_yticklabels(ylabels) 

#Add text to the plot showing the values at that point 
for i in xrange(n): 
    for j in xrange(n): 
     plt.text(j,i, C[i,j], horizontalalignment='center', verticalalignment='center') 

plt.show() 

而且將創建以下情節:

enter image description here

0
import numpy as np 
from matplotlib.pylab import * 
R = np.array ([3.00, 2.50, 1.00, 3.3192, 2.383, 2.7128, 3.8662, 3.6724, 3.5112]) 
C = np.split(R, 3) 
print(C) 
matshow(C,cmap=cm.gray) 
plt.show() 

enter image description here