OiO.lk Blog python How do I plot matrixes of data in 3d using Python?
python

How do I plot matrixes of data in 3d using Python?


I am working with OCT scans and I have 3d matrixes of data, the background has the value 0.0 and the rest is aligned in the middle.

How my current data looks in 2d plotting This image is an example of what I currently have, with the following shape/format

Tensor [25, 496, 512]

How do I plot this in 3d using python? I want to visualize the volume. My main problem is the background usually plots as black and all I can see is a black cube.

This is my current implementation:

def plot_oct_3d(scan):
    slices, height, width = scan.shape
    x, y, z = np.meshgrid(np.arange(width), np.arange(height), np.arange(slices))

    x_flat = x.flatten()
    y_flat = y.flatten()
    z_flat = z.flatten()
    values_flat = scan.flatten()

    mask = values_flat > 0.0
    x_plot = x_flat[mask]
    y_plot = y_flat[mask]
    z_plot = z_flat[mask]
    values_plot = values_flat[mask]

    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111, projection='3d')

    sc = ax.scatter(x_plot, y_plot, z_plot, c=values_plot, s=1, cmap='gray', alpha=0.6)

    ax.set_xlabel('Width')
    ax.set_ylabel('Height')
    ax.set_zlabel('Slices (Depth)')
    ax.set_title('3D Visualization of OCT Scans')

    plt.colorbar(sc, ax=ax, label="Intensity Value")
    plt.show()

if I pass my tensor after scanning an OCT (it was in e2e format), the scan comes out as a black box since the 0.0 values are still plotted. There is nothing wrong with loading the data from the .e2e file, and it seems to plot properly in 2d matplot as shown in the image above.

This is how my 3d plot looks like

What would you recommend I do?



You need to sign in to view this answers

Exit mobile version