plotly / plotly.py

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

graph_objs.volume and .isosurface not rendering non-cuboid #3933

Open yyyxam opened 2 years ago

yyyxam commented 2 years ago

I'm trying to use plotly in a dash app to render a cuboid, that's sheared to the y-axis.=it has a parallelogram as ground surface. It consists of individual points, each with a density value. plotly_express.scatter_3d renders the individual points correctly, but only has "2 views" when rotating it:

With both, I'm losing a lot of information on the insides of my structure. I think scatter_3d doesn't allow for

Best looking solution would be graph_objs.Volume or .isosurface, but they have an issue.

So far, the coordinates data had to be arranged to form an orthogonal cuboid, to render a volume. My test data is not the same y-sheared cuboid I want to achieve, but it illustrates the issue of both graph_objs.

Test Data: Using plotly_express.scatter_3d() for comparison, the following dataframe shows both Figures rendering the expected result:

DF = pd.DataFrame(data={
    'x': [0, 0, 0, 0, 0, 0, 1,1,1,1,1,1],
    'y': [0,0,0,1,1,1,0,0,0,1,1,1],
    'z': [0,1,2,0,1,2,0,1,2,0,1,2],
    'val': [1,1,1,1,1,1,1,1,1,1,1,1]})

scatter plot: points

volume plot: points volume

When removing 2 points at (0/1/2) & (1/1/2) -

DF = pd.DataFrame(data={
    'x': [0, 0, 0, 0, 0, 1,1,1,1,1],
    'y': [0,0,0,1,1,0,0,0,1,1],
    'z': [0,1,2,0,1,0,1,2,0,1],
    'val': [1,1,1,1,1,1,1,1,1,1]})

scatter plot: scattermissing

volume plot: vol missing

scatter_3d figure:

px.scatter_3d(
    DF,
    x='x',
    y='y',
    z='z',
    color='val',
    opacity=0.5,
    color_continuous_scale=px.colors.sequential.Inferno_r,
    range_color=[df.min()['val'], df.max()['val']],
    template=templ)

opacity doesn't take lists/ranges, so I can't use DF.val there.

Volume figure:

go.Figure(data=go.Volume(
    x=DF.x,
    y=DF.y,
    z=DF.z,
    value=DF.val,
    opacity=0.3,
    surface_count=17,
    colorscale=px.colors.sequential.Inferno_r
))

I played around with surface_count, but it doesn't seem to be causing the problem.

My 2 points being:

empet commented 2 years ago

A go.Volume carves into a 3d grid defined on a paralelipiped to get a number of surfaces. From your long story I deduced that you want to plot the boundary of a particular 3D volume. The boundary faces can be defined as triangles and plotted as a go.Mesh3d, not as an isosurface or volume. Could you, please, post a manual plot of what you want to get via Plotly? You have 8 points, but it is unclear how are they connected to get the edges of your 3d "cuboid".

yyyxam commented 2 years ago

Hope it wasn't too lengthy.

I can only plot what I want to get with px.scatter3d: sheared

It's a cell showing density fields. The data is of an unsheared cell and I manually shear the coordinates. The sheared data can not be used to plot isosurfaces/volumes. Only plottable as dots via scatter_3d

The unsheared data works for a volume-plot: vol

It's >8000 coordinates, which is why I wanted to simplify by using my testdata.

To answer your question: No edges are defined explicitly. Only by the order of coordinates

empet commented 2 years ago

What was the role of the eight points in the previous post? From given data (eight points and vals, constant, and equal to 1) it's obvious that you cannot define a go.Volume. As long as I don't know exactly your initial data, I cannot suggest a solution. How is defined the density on your volume, as a function or is it discretized and read from a file?

empet commented 2 years ago

If you post the vertices of your sheared parallelipiped (cuboid) I could realize if the volume can be defined by a few isosurfaces defined as it's suggested in this discussion: https://github.com/plotly/plotly.py/issues/2635. Note that a cuboid is defined by its vertices and edges. The vertices are given as an array of shape (8, 3), and edges as tuples (i, j). (2,4), for example, is the edge connecting the vertices in the rows 2 and 4 of the array.

gvwilson commented 4 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 a while, 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. Alternatively, if it's a request for tech support, please post in our community forum. Thank you - @gvwilson

yyyxam commented 3 months ago

Apologies for letting this collect dust and reappearing now, but I've recently been working on the topic again and just received the notification. Also apologies for posting this here, as it is more related to plotly.js. This open issue perfectly describes the problem I was and am facing. I just wanted to leave this here for the sake of completeness, but don't expect a response.