首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python-2/3D散点图与该数据的表面图

Python-2/3D散点图与该数据的表面图
EN

Stack Overflow用户
提问于 2018-01-01 20:23:26
回答 1查看 3.5K关注 0票数 2

使用:,所以我有一个3D数组来创建散点图,生成n*n*n个立方体。这些点有不同的潜在值表示的颜色。

代码语言:javascript
复制
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

我有两个值-10和10开始,其余的点计算如下:(周围点的平均数,没有对角线):

代码语言:javascript
复制
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

以及外层细胞:

代码语言:javascript
复制
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(电位1,1,1),它会显示该单元格的值,正如您在下面的图像中所看到的,它是10。这就是我用来显示颜色的。

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

发布于 2018-01-02 03:15:29

创建一个Minimum, Complete and Verifiable Example以使帮助更容易,这对您是有益的。

我仍然不清楚你是怎么计算你的潜力的,也不知道你是如何生成你的曲面的,所以我包含了一些琐碎的函数。

下面的代码将生成彩色点的三维散点图和颜色平均值的曲面。

代码语言:javascript
复制
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()

生成的图像如下所示:

编辑:使用您的附加代码,我可以复制您的多维数据集。

然后使用以下代码生成曲面:

代码语言:javascript
复制
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

然后绘制这个表面:

代码语言:javascript
复制
surf = ax.plot_surface(xx, yy, zz, cmap=plt.cm.rainbow) 

注:包括vmin和vmax关键字args,以保持相同的比例,我忽略了那些,所以表面偏差是更明显的。我还将立方体上的alpha设置为0.2,以便更容易地看到表面。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48052969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档