【问题标题】:Creating an array of cubes创建立方体数组
【发布时间】:2017-05-25 09:17:50
【问题描述】:

以下程序将用于大学物理研究。所以我正在研究一个涉及创建矩形单元格的研究项目。现在我正在使用 3d 立方网格。我的目标是让更多这些彼此相邻。

这是我当前的代码(你们帮助过): 现在它只做一个立方体,可以在立方体网格的顶点上放置随机点,并且可以计算点之间的距离。我如何让 matplotlib 产生更多这些?另外,我需要能够在多个立方体上放置点,并且我应该能够计算从立方体 A 中的一个点到立方体 B 中的一个点的距离。我可以在同一个 matplotlib 中生成多个立方体吗?做一个while循环?另外,使用 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()

【问题讨论】:

    标签: python arrays numpy matplotlib physics


    【解决方案1】:

    我在理解大部分代码的用途方面确实存在严重问题。我可以告诉你的是,你当然可以将立方体的创建放在一个函数中,并多次调用该函数,可能使用不同的参数,以获得多个散点图。

    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()
    

    【讨论】:

    • 抱歉不清楚。我认为您可能没有得到的部分是 if/else 语句。这些语句在立方体的顶点上产生随机点。另外,立方网格中两种不同的点分布有什么用?那些应该代表单元格吗?谢谢。
    • 我不知道它们代表什么。您要求一种制作多个立方体的方法,而这个答案提供了这种方法。我使这两个立方体不同,但这只是为了展示如何使用参数,因为我不知道(也不需要知道)所有这些的目的。
    • 好的。我想我现在明白了(尤其是参数)。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多