我想为6个城镇之间的距离绘制一个距离矩阵图。A1到A3,B1到B3。
我计算了类似A1-B1,A1-B2的距离.同样.A3-B3和一个一维数组,我得到了6个城镇之间距离的一维numpy数组。
np.array(R)
[ 3.00 2.50 1.00 3.3192 2.383 2.7128 3.8662 3.6724 3.5112]
现在我想要一个距离矩阵格式的图,它应该像下图所示的那样。
这只是一个有代表性的数据。我有很多值,所以需要python程序。任何建议或样例脚本都会有帮助。致以问候。
发布于 2015-09-11 03:07:20
看来你自己也有很大的优势。您可以通过将轴标签更改为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()
并将创建以下情节:
发布于 2015-09-11 02:26:14
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()
https://stackoverflow.com/questions/32503308
复制相似问题