plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

provide way to get current camera position coordinates

Open nshyam97 opened this issue 4 years ago • 11 comments

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

nshyam97 avatar Feb 01 '21 15:02 nshyam97

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.

empet avatar Feb 01 '21 17:02 empet

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?

nshyam97 avatar Feb 02 '21 14:02 nshyam97

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.

empet avatar Feb 02 '21 14:02 empet

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.

nicolaskruchten avatar Feb 02 '21 14:02 nicolaskruchten

@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?

nshyam97 avatar Feb 02 '21 16:02 nshyam97

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

empet avatar Feb 02 '21 17:02 empet

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

musm avatar Jan 28 '22 16:01 musm

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

gvwilson avatar Jul 03 '24 16:07 gvwilson

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.

kerim371 avatar Jul 09 '24 09:07 kerim371

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

gvwilson avatar Jul 09 '24 15:07 gvwilson

Yeah, this kind of feature would be really nice because in that case users could rotate 3D visualization as necessary, get the camera matrix, perform for example orthographic projection and leverage the resulting image to measure objects in 2d screen

AndresNamm avatar Nov 02 '25 11:11 AndresNamm