0

I want to make 3D plot in python using plotly. Actually its the first step of an animation but first I need to know how to create a 3D scatter plot of it as shown below which is made in matplotlib. I have the dataset which looks like this.

enter image description here

Now, I want to animate it against the dates mentioned but the code I am getting on official site of plotly like this and most of them are similar.

import plotly.express as px
df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
              color='species')
fig.show()

Now, I'm not able to understand how should I assign X,Y and Z to get an animated plot. Because all the 13 columns I need in the plot against the date.

enter image description here

For reference I found this site Which said,

To get a 3D scatter plot you need to specify the trace as a plotly.graph_objs.Scatter3d object or as a dict with a type: 'scatter3d' key.

Feel free to share a full reproducible example if you’d like to work through more of the details.

But this wasn't enough.

Please help me with this. A link of proper code or guidance would be really helpful to how set variables to get a 3D plot animation. Thank you.

1 Answer 1

2

For updating existing plots in plotly, they have the FigureWidget interface. If you can operate in an environment where the widgets work (e.g. Jupyter notebook), then the following code will repeatedly update the data in an existing plot.

It doesn't look great as an animation. And if the data updates while you are rotating the view, it jerks back to the original position. So that's not great. But if you are okay with those limitations, this will get you started.

import plotly.express as px
df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
              color='species')

import threading
import time
import numpy as np
fw = go.FigureWidget(fig)

def do():
    while True:
        fw.data[0]['x'] = np.random.rand(len(fw.data[0]['x']))
        time.sleep(1)

t = threading.Thread(target=do)
t.start()

fw # run this in cell after which the plot will be generated.

This is how it looks. Note the unfortunate jumpiness during rotating the view. enter image description here

11
  • I am not working with the iris set. The data is mentioned above please Commented Apr 28, 2021 at 3:25
  • I think the dataset was not your problem as much as what approach to take. I gave an answer on what approach you could use.
    – William
    Commented Apr 28, 2021 at 3:26
  • Also, if that's what you wanted, your question was extremely unclear... Please make your question clearer.
    – William
    Commented Apr 28, 2021 at 3:28
  • I apologise for that @William . Your approach is indeed helpful. But if you see in the screenshot of the data there are 134 columns which I need in 3D against the dates. Please help me as I am not able to find a proper code or guidance on how I can do it. Would be really helpful. Thanks Commented Apr 28, 2021 at 3:32
  • 1
    It doesn't make sense that it needs to be a volumetric video. If the data is not 3-dimensional, then making it "volumetric" doesn't show any information.
    – William
    Commented Apr 28, 2021 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.