简体   繁体   中英

Convert four-dimensional numpy array to list of x, y, z, intensity

I have a numpy array created as follows

results = np.zeros((X, Y, Z))

Then I am setting values of the points in 3D space as follows (representative of density / intensity of that point)

results[x,y,z] = 5.0

I now want to visualize this data using the x,y,z coordinates and an intensity value (like opacity or size of a scatter plot). However I cannot figure out how to convert this into four lists of x, y, z, and intensity, for a 3D scatter plot. How do I do this?

i would do smth like this:

import numpy as np
import matplotlib.pyplot as plt


dots = np.random.randint(0, 2, size = (3, 3, 3))
dots *= np.random.randint(0, 2, size = (3, 3, 3))
dots *= np.arange(27).reshape(3, 3, 3)

x, y, z = np.where(dots!=0)
o = dots[x, y, z]

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
for i in range(len(x)):
    print(o[i]/27)
    ax.plot([x[i]], [y[i]], [z[i]], 'o', color=[0, 0, 0, float(o[i])/27])

output:

dots = 
[[[ 0  0  0]
  [ 0  0  0]
  [ 6  0  0]]

 [[ 0  0 11]
  [ 0 13  0]
  [15 16 17]]

 [[ 0  0  0]
  [21 22 23]
  [24  0  0]]]

在此处输入图像描述

My solution:

fig = plt.figure(figsize=(15, 15))
ax = fig.add_subplot(projection="3d")
plt.title("Spherical Potential Heatmap ($J = 32, simuls = 6.4M, E = 1, cutoff = 100$)", fontsize=18)
ax.xaxis.pane.fill = False
ax.yaxis.pane.fill = False
ax.zaxis.pane.fill = False
mask = base_array_e0 > 100
idx = np.arange(int(np.prod(base_array_e0.shape)))
x, y, z = np.unravel_index(idx, base_array_e0.shape)
plot = ax.scatter(x, y, z, c=base_array_e0.flatten(), s=10.0 * mask, edgecolor="face", alpha=0.15, marker="o", cmap="magma", linewidth=0)
color_bar = plt.colorbar(plot, ax = ax,fraction=0.036, pad=0.04)
color_bar.set_alpha(1)
color_bar.draw_all()
color_bar.set_label('Steps')
plt.savefig('random_walk_3d_energy_sphere_0.png', bbox_inches='tight');

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM