Black Lives Matter. Please consider donating to Black Girls Code today.

How to make animated 3D scatter plot in plotly Python

My goal is to create an animation with my 3D data in plotly. I have 3 variables x,y,z for simplicity and I plot the 4th value depending on these x,y,z. I create a 3D scatter plot where the 4th dim sort to speak is the color like this:

from numpy import genfromtxt
import numpy as np
import plotly.io as pio
import plotly.express as px
pio.renderers.default = 'notebook'
import plotly.graph_objects as go
import math

import pandas as pd 
data = pd.read_csv("paramtp_1e-05_big.txt") 
data.head()
data = data.iloc[::10, :]

color_data = data['gopt'].astype(float).round(decimals=2)
color_data[color_data>= 10] = 10
color_data_nopt = data['nopt'].astype(float).round(decimals=3)
color_data_mc = data['mc'].astype(float).round(decimals=3)
color_data_P= data['P']
color_data_P[color_data_P >= 1] = 1


data= data.replace(np.nan, '', regex=True)
data.tail()

fig = px.scatter_3d(data, x='NpN0', y='s', z='mu',log_x=True, log_z=True,
              opacity = 0.5,      
              color=color_data,color_continuous_scale=px.colors.sequential.Viridis)
fig.add_trace(
    go.Scatter(
        mode='markers',
        marker=dict(
            size=1,
            opacity=0.5,
            
        ),
    )
)

fig.show()

Similarly to this wonderful animation: https://plotly.com/python/visualizing-mri-volume-slices/ I would like to slice up my data to isosurfaces with respect to any x,y,z coordinates.

As in the example they use images, I could not wrap my head around to create the same with my raw data.

Thank you in advance.

Hi @domeemod,
From your description it is not clear how the 3d points or their colors move/change from frame to frame. Please give more details on what you intend to illustrate with this animation, and eventually provide the data set if it has something special and the animation of randomly generated data
isn’t suitable for your purpose.

Hi!

Thank you for the quick reply. Randomly generated data is fine (my dataset is not really special). It has three variables and a 4th dependent variable (thus 3D (X,Y,Z) +1D (V) as color which is the dependent value). I would like to create an animation where I fix a coordinate lets say X. After I fixed an X value I could plot a 2D “slice” where I plot (Y,Z)->V. Or I can do that I fix Y, than I plot the (X,Z)->V plane or fix Z and plot (X,Y)->V. I would like to go through all of these planes one by one for gradually increasing or decreasing fixed values of one of these coordinates (X,Y or Z), most preferally with an interactive sliding bar as showed in that wonderful MRI scan animation.

Thank you in advance!

If needed here is my dataset though: https://drive.google.com/file/d/1Uv3V-pccg4kFgjtfjOtYS34OAgi2V7rD/view?usp=sharing
In the dataset col(0,1,2) are the X,Y,Z and any other column can be V…

@domeemod

You can define slices in a volume defined by your data as follows:

  • read data, x, y, z, v, as arrays;
  • calculate xm, xM =x.min(), x.max(), and similarly ym, yM, zm, zM;
  • define a 1d grid over the intervals [xm, xM], [ym, yM], [zm, zM]:
X = np.linspace(xm, xM, m)
Y = np.linspace(ym, yM, n)
Z = np.linspace(zm, zM, p)

where the number of points, m, n, p depends on the length of the coresponding interval.
The same m, n, p define the number of slices in each direction.

  • define a 3d meshgrid ( a volume) from the above 1d grids:
X, Y, Z = np.meshgrid(X, Y, Z)

Attn!!! X, Y, Z have the shape (n, m, p) NOT (m, n, p)!!!

  • Calculate by linear interpolation the values of the variable V at the above defined 3D grid,
    whose points are recorded by X, Y, Z;
V = scipy.interpolate.griddata((x, y, z), v, (X, Y, Z) , method='linear')

The array V has the same shape, like X, Y, Z.

After this construction you can animate x, y, or z-slices in the volume defined above.

Example: https://chart-studio.plotly.com/~empet/15840

Wow, thank you so much for this really detailed demo!