【发布时间】: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