簡體   English   中英

Python-具有該數據的曲面圖的2 / 3D散點圖

[英]Python - 2/3D scatter plot with surface plot from that data

使用:[python] [numpy] [matplotlib]因此,我有一個3D數組來創建散點圖,並制作一個* 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的球,其余點的計算方式如下:(周圍點的平均值,沒有對角線):

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(像這個 ),這就是為什么我再次詢問。 也可以將其顯示為很多子圖,每個子圖都有一個表面。

注意:我的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.

創建最小,完整和可驗證的示例以使幫助更加容易這對您很有幫助

對我來說,仍然不清楚您是如何計算潛力的,也不是如何生成表面的,因此我已經包含了一些瑣碎的函數。

以下代碼將生成色點的3D散點圖和帶有顏色平均值的曲面。

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

def fn(x, y):
    """Custom fuction to determine the colour (potential?) of the point"""
    return (x + y) / 2  # use average as a placeholder

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

size = 11  # range 0 to 10
# Make the 3D grid
X, Y, Z = np.meshgrid(np.arange(0, size, 1),
                      np.arange(0, size, 1),
                      np.arange(0, size, 1))

# calculate a colour for point(x,y,z)
zs = np.array([fn(x, y) for x, y in zip(np.ravel(X), np.ravel(Y))])
ZZ = zs.reshape(X.shape)  # this is used below

# create the surface
xx, yy = np.meshgrid(np.arange(0, size, 1), np.arange(0, size, 1))
# Calcule the surface Z value, e.g. average of  the colours calculated above
zzs = np.array([np.average(ZZ[x][y]) for x, y in zip(np.ravel(xx), np.ravel(yy))])
zz= zzs.reshape(xx.shape)

cube = ax.scatter(X, Y, Z, zdir='z', c=zs, cmap=plt.cm.rainbow)
surf = ax.plot_surface(xx, yy, zz, cmap=plt.cm.rainbow) 
cbar = fig.colorbar(cube, shrink=0.6, aspect=5) # Add a color bar

plt.show()

生成的圖像如下所示: 3D散射和表面

編輯:使用您的其他代碼,我能夠復制您的多維數據集。

然后使用以下代碼生成一個曲面:

xx, yy = np.meshgrid(np.arange(0, size, 1), np.arange(0, size, 1))
#define potential range
min_p = 1.0
max_p = 4.0

zz = np.zeros((size, size))
for i in range(size):  # X
    for j in range(size):  # Y
        for k in range(size):  # Z
            p = potential[k,j,i]
            if min_p < p < max_p:
                zz[j][i] = p # stop at the first element to meet the conditions
                break # break to use the first value in range

然后繪制此表面:

surf = ax.plot_surface(xx, yy, zz, cmap=plt.cm.rainbow) 

注意:包括vmin和vmax關鍵字args以保持相同的比例,我已將它們省略了,以使表面偏差更明顯。 我還將多維數據集上的alpha設置為0.2,以便更輕松地查看表面。

具有Surface Take 2的多維數據集圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM