Reputation: 37
The following program will be used for college physics research. So I'm working on a research project that involves creating unitcells in rectangular form. For now I'm working with a 3d cubic grid. My goal is to make more of these adjacent with each other.
Here is my current code (which you guys helped with): Right now it only makes one cube, can place random points on the cubic grid's vertices, and it can calculate the distance between points. How would I get matplotlib to produce more of these? Also, I need to be able to put points on the multiple cubes and I should be able to calculate the distance from a point in Cube A to a point in Cube B. Would I be able to produce multiple cubes in the same matplotlib by just doing a while loop? Also, what would it look like using numpy arrays? I feel as though the numpy arrays would be easy to create but I can't quite wrap my head around it. Code I have so far:
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()
Upvotes: 0
Views: 1163
Reputation: 339280
I do have severe problems understanding the purpose of most of the code. What I can tell you is that you may of course put the cube creation in a function and call that function several times, possibly with different parameters, to obtain several scatter plots.
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()
Upvotes: 1