plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
16.1k stars 2.54k forks source link

provide way to get current camera position coordinates #3057

Open nshyam97 opened 3 years ago

nshyam97 commented 3 years ago

Currently displaying plotly using a streamlit application in Python and want the user to be able to rotate and move a 3D Scatter plot to a desired camera position before writing the image to disk from the camera position that they have changed it to?

Spent a couple of hours trying to find solutions to this but I am assuming this is not possible at time of writing, will this be possible at all? Is it possible to integrate this currently if someone were to pick it up (I am happy to give it a crack if I am pointed in the right direction)?

Maybe an on_change method when the camera position is changed?

Thanks

empet commented 3 years ago

If you did not set scene.camera.eye in your code, then its default position is camera_eye=dict(x=1.25, y=1.25, z=1.25). To rotate it at another position you should define a function that rotates a 3d point about zaxis with theta radians. Thus you get a new position. At this new position, (a, b, c), you can eventually take (a, b, c-0.25), to look at your plot from a smaller height.

nshyam97 commented 3 years ago

Understood, the problem is I don't know the final position the user will rotate to and they could rotate it in whatever way they see fit and will be different for each user and each time the user runs the app. When writing the image via fig.write_image it just saves the image with the eye position at its default position rather than the view that the user is seeing on the screen.

I don't think there's a current ability within the plotly code to access the current eye position unless there is an easy way to calculate it on the fly? If you know where in the code this could be implemented I can take a look?

empet commented 3 years ago

I understood that you wanted to save the plot as an image, via plotly.io write_image(fig, "filename ext"). My suggested solution is for such a purpose.

Plotly.py sends to plotly.js a json version of your fig description and it's plotly.js that controls the user interaction with the plot. This is a one way communication and you cannot retrieve the camera.eye position.

nicolaskruchten commented 3 years ago

I believe @nshyam97 is thinking about things in a dynamic context with widgets and the intent is to capture and store user interaction via streamlit or Dash or in a Jupyter notebook. In such cases, there is a reverse path for communication. I believe that we write everything into the layout and fire relayout events that you can catch and process. If the camera eye etc is not correct in the layout, this is a bug and can be submitted in the Plotly.js repo as an issue.

nshyam97 commented 3 years ago

@empet @nicolaskruchten Yes sorry, I was trying to describe what Nicolas was saying. Will take a look at the relayout events. I don't mind if it doesn't automatically save the modified eye position, but if I can get the new coordinates, I can perform what @empet suggested. Did not realise that plotly.py sent out relayout events, thank you!

EDIT: Are there any docs regarding relayout that I can take a look at?

empet commented 3 years ago

@nshyam97 Here is a short tutorial on how relayout works: https://chart-studio.plotly.com/~empet/15608.

musm commented 2 years ago

I'm also trying to do the same. Was a solution ever figured out for this issue?

gvwilson commented 3 months ago

Hi - we are trying to tidy up the stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for several years, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. Thanks for your help - @gvwilson

kerim371 commented 3 months ago

Hi,

I've faced the same problem. I need to export everything is displayed in the figure to the picture but the exported figure has the default camera settings (i.e. user interaction doesn't change camera).

I believe there should be an easy way to do that.

gvwilson commented 3 months ago

Thanks @kerim371 - I'll reopen the issue and add it to our backlog, but I don't think anyone will be able to get to it any time soon. If you'd like to submit a PR, we'd be very happy to review it. Cheers - @gvwilson