【发布时间】:2018-01-01 20:23:26
【问题描述】:
使用:[python] [numpy] [matplotlib] 所以我有一个 3D 数组来创建一个散点图,制作一个 n * n * n 立方体。这些点具有由颜色表示的不同电位值。
size = 11
z = y = x = size
potential = np.zeros((z, y, x))
Positive = 10
Negative = -10
""" ------- Positive Polo --------- """
polox = poloy = poloz = [1,2]
polos=[polox,poloy,poloz]
polop = [list(x) for x in np.stack(np.meshgrid(*polos)).T.reshape(-1,len(polos))] # Positive polos list
for coord in polop:
potential[coord] = Positive
""" ------- Negative Polo --------- """
polo2x = polo2y = polo2z = [size-3,size-2]
polos2=[polo2x,polo2y,polo2z]
polon = [list(x) for x in np.stack(np.meshgrid(*polos2)).T.reshape(-1,len(polos2))] # Negative polos list
for coord in polon:
potential[coord] = Negative
我在开始时有 2 个值 -10 和 10 的 polo,其余点的计算方式如下:(周围点的平均值,没有对角线):
for z in range(1,size):
for y in range(1,size):
for x in range(1,size):
if [z,y,x] in polop:
potential[z,y,x] = Positive # If positive polo, keeps potential
elif [z,y,x] in polon:
potential[z,y,x] = Negative # If negative polo, keeps potential
elif z!=size-1 and y!=size-1 and x!=size-1: # Sets the potential to the mean potential of neighbors
potential[z][y][x] = (potential[z][y][x+1] + potential[z][y][x-1] + potential[z][y+1][x] + potential[z][y-1][x] + potential[z+1][y][x] + potential[z-1][y][x]) / 6
对于外部单元格:
for z in range(0,size):
for y in range(0,size):
for x in range(0,size):
potential[z,y,0] = potential[z,y,2]
potential[z,0,x] = potential[z,2,x]
potential[0,y,x] = potential[2,y,x]
if z == size-1:
potential[size-1,y,x] = potential[size-3,y,x]
elif y == size-1:
potential[z,size-1,x] = potential[z,size-3,x]
elif x == size-1:
potential[z,y,size-1] = potential[z,y,size-3]
我需要的是显示连接具有相同值区间“相同颜色”(例如从 0 到 2.5)的点的表面。
我知道有很多这样的问题,但是我无法适应我的代码,它要么不显示(例如this)要么不是同一个问题,要么不是python(作为this one),这就是我再次询问的原因。 它也可以显示为许多子图,每个子图都有一个表面。
注意:我的 3D 数组是这样的,如果我键入 print(potential[1,1,1]),它会显示该单元格的值,如下图所示,为 10。这就是我用于显示颜色。
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
z,y,x = potential.nonzero()
cube = ax.scatter(x, y, z, zdir='z', c=potential[z,y,x], cmap=plt.cm.rainbow) # Plot the cube
cbar = fig.colorbar(cube, shrink=0.6, aspect=5) # Add a color bar which maps values to colors.
【问题讨论】:
标签: python numpy matplotlib plot