创建多维数据集数组

2024-09-30 06:23:05 发布

您现在位置:Python中文网/ 问答频道 /正文

以下课程将用于大学物理研究。所以我正在做一个研究项目,这个项目包括创建矩形的单元单元。现在我正在处理一个三维立方体网格。我的目标是让彼此更接近。在

这是我当前的代码(你们帮了忙): 现在它只生成一个立方体,可以在立方体网格的顶点上放置随机点,并且可以计算点之间的距离。我如何让matplotlib生产更多这样的产品?另外,我需要能够在多个立方体上放置点,并且我应该能够计算从立方体a中的一个点到立方体B中的一个点的距离。我是否能够通过做一个while循环在同一个matplotlib中生成多个立方体?另外,使用numpy数组会是什么样子? 我觉得numpy数组很容易创建,但我不能完全围绕它。 到目前为止我掌握的代码:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
from matplotlib.patches import FancyArrowPatch 
from mpl_toolkits.mplot3d import proj3d

parameter = np.arange(0,11,1)
xx, yy, zz = np.meshgrid(parameter, parameter, parameter)
valuesrange = np.zeros((11, 11, 11))
valuesrange2 = np.zeros((11, 11, 11))

count = 0

while (count < 2):
xint = np.random.randint(0,2)
yint = np.random.randint(0,2)
zint = np.random.randint(0,2)

if xint > 0:
    xint = np.random.randint(10,11, 22)  
else:
    xint = np.random.randint(0,1, 22)

if yint >0:
    yint = np.random.randint(10,11, 22)  
else: 
    yint = np.random.randint(0,1, 22)

if zint > 0:
    zint = np.random.randint(10,11, 22)  
else:
    zint = np.random.randint(0,1, 22)
count = count + 1
print(xint, yint, zint)
xint2 = np.random.randint(0,2)
yint2 = np.random.randint(0,2)
zint2 = np.random.randint(0,2)

if xint2 > 0:
    xint2 = np.random.randint(10,11, 22)  
else:
    xint2 = np.random.randint(0,1, 22)

if yint2 >0:
    yint2 = np.random.randint(10,11, 22)  
else: 
    yint2 = np.random.randint(0,1, 22)

if zint2 > 0:
    zint2 = np.random.randint(10,11, 22)  
else:
    zint2 = np.random.randint(0,1, 22)
print (count)
print(xint2, yint2, zint2)
distance = ((xint2-xint)**2 + (yint2 - yint)**2 + (zint2 - zint)**2)**.5
print ('distance:')
print (distance)

#xint = np.random.randint(0, 11, 22)
#yint = np.random.randint(0, 11, 22)
#zint = np.random.randint(0, 11, 22)
#distance formula = ((x2-x1)**2 + (y2 - y1)**2 + (z2 - z1)**2)**.5 
valuesrange[[xint, yint, zint]]= np.random.random(22)
valuesrange[[xint, yint, zint]]= np.random.random(22)
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
im = ax.scatter(xx, yy, zz, c = valuesrange, cmap=plt.cm.spectral_r, 
edgecolor = 'none', alpha = .7)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

plt.colorbar(im)
fig.show()

#plt.show()

Tags: importifcountnppltrandomelseprint
1条回答
网友
1楼 · 发布于 2024-09-30 06:23:05

我在理解大部分代码的用途时遇到了严重的问题。我可以告诉你的是,你当然可以把立方体的创建放在一个函数中,然后调用这个函数几次,可能用不同的参数,得到几个散点图。在

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

def make_cube(ax, n=10, offset=[0,0,0]):
    parameter = np.arange(0,n,1)
    xx, yy, zz = np.meshgrid(parameter, parameter, parameter)
    valuesrange = np.zeros((n,n,n))
    valuesrange = np.random.rand(n,n,n)
    x = xx+offset[0]; y=yy+offset[1];  z=zz+offset[2]
    sc = ax.scatter(x, y, z, c = valuesrange, cmap=plt.cm.spectral_r, vmin=0, vmax=1,
                    edgecolor = 'none', alpha = .7)
    return sc

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

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

sc1 = make_cube(ax,n=6)
sc2 = make_cube(ax,n=4, offset=[8,7,4])
# or use a loop:
#for i in range(4):
#    sc1 = make_cube(ax,n=i)

plt.colorbar(sc1)
plt.show()

enter image description here

相关问题 更多 >

    热门问题