2017-05-25 94 views
0

以下程序將用於大學物理研究。所以我正在研究一個涉及以矩形形式創建單元格的研究項目。現在我正在使用3d立方格。我的目標是讓更多這些彼此相鄰。創建立方體陣列

這裏是我當前的代碼(你們幫助過): 現在它只能製作一個立方體,可以在立方體網格的頂點上放置隨機點,並且它可以計算點之間的距離。我將如何讓matplotlib生成更多的這些?另外,我需要能夠在多個立方體上放置點,並且我應該能夠計算從立方體A中的點到立方體B中的點的距離。我是否可以通過僅在同一個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() 

回答

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

enter image description here

+0

對不起,我沒有說清楚。我認爲你可能沒有得到的部分是if/else語句。這些語句在立方體的頂點上產生隨機點。另外,你的立方體網格中兩種不同分佈點的用途是什麼?那些應該代表Unitcells?謝謝。 – Astupidhippo

+0

我不知道他們代表什麼。您要求提供一種方法來製作多個立方體,並且此答案提供了這種方法。我讓這兩個立方體不同,但這只是爲了展示如何使用參數,因爲我不知道(而且我也不需要知道)所有這些的目的。 – ImportanceOfBeingErnest

+0

好的。我想我現在明白這一點(尤其是參數)。謝謝您的幫助。 – Astupidhippo