在2D平面中,我有一组由其nxn坐标定义的(x,y)点。对于每个点,我都希望将某些量绘制为3D曲面。如何为每个点分配图中的相关值?我问,因为我弄得一团糟。

我会更好地解释自己。我有:


平面中10x10=100点的位置字典:


dict1={0:(0, 0), 1:(0, 1), 2:(0, 2), ..., 99:(9,9)}


将与所述点相关联的值的字典:


dict2=OrderedDict([(0, 369670), (1, 370622), (2, 267034), ..., (99, 217500)])


dict1dict2的合并,其中每个值都与正确的点相关联,每个点均基于其坐标进行标记:


merged_dict={dict1[k]: v for k, v in dict2.items()}

merged_dict={(0,0):369670, (0,1):370622, (0,2):267034, ..., (9,9): 217500}

预期的3D图的点坐标为来自merged_dict的X和Y和Z。这是我的尝试:

#3D plot
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

fig = plt.figure()
ax = fig.gca(projection='3d')
inds=[(0, 0), (0, 1), (0, 2), ..., (9,9)] #The coordinates of each point -> len(inds)=100

X=[]
for k in range(len(inds)):
    X.append(int(inds[k][0]))
Y=X

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

merged_dict = {dict1[k]: v for k, v in dict2.items()}

Z = merged_dict.values()

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.winter, linewidth=0, antialiased=True)

ax.set_zlim(0, 900000)

ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

plt.show()


我得到的情节是错误的,因为它具有混乱的表面。预期的结果是在点(4,4),(5,4),(4,5),(5,5)对应的对称钟形表面峰值超过800,000。相反,结果使我认为Z值未与相关的X,Y坐标正确关联。如何解决这个问题?
python - Python:根据点值进行一致的3D绘图-LMLPHP

编辑

这些是涉及的实际数据:

In[1]: merged_dict
Out[1]:
{(0, 0): 369670,
 (0, 1): 370622,
 (0, 2): 267034,
 (0, 3): 169500,
 (0, 4): 116014,
 (0, 5): 116014,
 (0, 6): 169500,
 (0, 7): 267034,
 (0, 8): 370622,
 (0, 9): 369670,
 (1, 0): 370622,
 (1, 1): 491950,
 (1, 2): 456750,
 (1, 3): 370180,
 (1, 4): 308118,
 (1, 5): 308118,
 (1, 6): 370180,
 (1, 7): 456750,
 (1, 8): 491950,
 (1, 9): 370622,
 (2, 0): 267034,
 (2, 1): 456750,
 (2, 2): 542718,
 (2, 3): 554980,
 (2, 4): 543588,
 (2, 5): 543588,
 (2, 6): 554980,
 (2, 7): 542718,
 (2, 8): 456750,
 (2, 9): 267034,
 (3, 0): 169500,
 (3, 1): 370180,
 (3, 2): 554980,
 (3, 3): 689848,
 (3, 4): 759272,
 (3, 5): 759272,
 (3, 6): 689848,
 (3, 7): 554980,
 (3, 8): 370180,
 (3, 9): 169500,
 (4, 0): 116014,
 (4, 1): 308118,
 (4, 2): 543588,
 (4, 3): 759272,
 (4, 4): 888268,
 (4, 5): 888268,
 (4, 6): 759272,
 (4, 7): 543588,
 (4, 8): 308118,
 (4, 9): 116014,
 (5, 0): 116014,
 (5, 1): 308118,
 (5, 2): 543588,
 (5, 3): 759272,
 (5, 4): 888268,
 (5, 5): 888268,
 (5, 6): 759272,
 (5, 7): 543588,
 (5, 8): 308118,
 (5, 9): 116014,
 (6, 0): 169500,
 (6, 1): 370180,
 (6, 2): 554980,
 (6, 3): 689848,
 (6, 4): 759272,
 (6, 5): 759272,
 (6, 6): 689848,
 (6, 7): 554980,
 (6, 8): 370180,
 (6, 9): 169500,
 (7, 0): 267034,
 (7, 1): 456750,
 (7, 2): 542718,
 (7, 3): 554980,
 (7, 4): 543588,
 (7, 5): 543588,
 (7, 6): 554980,
 (7, 7): 542718,
 (7, 8): 456750,
 (7, 9): 267034,
 (8, 0): 370622,
 (8, 1): 491950,
 (8, 2): 456750,
 (8, 3): 370180,
 (8, 4): 308118,
 (8, 5): 308118,
 (8, 6): 370180,
 (8, 7): 456750,
 (8, 8): 491950,
 (8, 9): 370622,
 (9, 0): 369670,
 (9, 1): 370622,
 (9, 2): 267034,
 (9, 3): 169500,
 (9, 4): 116014,
 (9, 5): 116014,
 (9, 6): 169500,
 (9, 7): 267034,
 (9, 8): 370622,
 (9, 9): 369670}

最佳答案

我猜在Y=X行中有问题。 X, Y是列表。编写Y=X时,表示Y是对X的引用。您需要进行复制,即:

Y = []
numpy.copy(Y,X)


测试此变体并写下会发生的情况。否则,发布merged_dict的数据以测试实值上的图。

更新:

看图,对吗?
数组Z必须是2d数组,但是在您的代码中是1d列表。

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

fig = plt.figure()
ax = fig.gca(projection='3d')

X = np.arange(0,10,1)
Y = np.arange(0,10,1)
X, Y = np.meshgrid(X, Y)
merged_dict = {(0, 0): 369670,
 (0, 1): 370622,
 (0, 2): 267034,
 ...
 (9, 8): 370622,
 (9, 9): 369670}

Z = np.array(merged_dict.values()).reshape(10,10)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.winter, linewidth=0, antialiased=True)
ax.set_zlim(0, 900000)

plt.show()


python - Python:根据点值进行一致的3D绘图-LMLPHP

更新2:

发生问题是因为您将数据存储在dict中,但是dict未按索引排序。要将值加载到数组Z

Z = np.zeros((10,10))

for key in merged_dict:
    i = key[0]
    j = key[1]
    Z[i][j] = int(merged_dict[key])


结果,您得到:

python - Python:根据点值进行一致的3D绘图-LMLPHP

10-08 15:11