简体   繁体   中英

How is the plotly 3D volume plot generated?

I've come across the following code in an example of the plotly 3D volume plots:

import plotly.graph_objects as go
import numpy as np


x1 = np.linspace(-4, 4, 9) 
y1 = np.linspace(-5, 5, 11) 
z1 = np.linspace(-5, 5, 11) 

X, Y, Z = np.meshgrid(x1, y1, z1)

values = (np.sin(X**2 + Y**2))/(X**2 + Y**2)

fig = go.Figure(data=go.Volume(
    x=X.flatten(),
    y=Y.flatten(),
    z=Z.flatten(),
    value=values.flatten(),
    opacity=0.1,
))

fig.show()

As I understand it, the 'x', 'y' and 'z' are what form the base 3D mesh cube grid and I presume that the 'value' forms the actual shape within the 3D mesh. The issue is I don't understand exactly how the 'value' field is being used to form the inner volume and the documentation doesn't seem to be at all helpful.

I initially believed that each row (or column) in the 3D values array represented a point within the mesh, but from experimentation that doesn't seem to be what's happening. Can someone please explain how this works?

For the sake of my drawing abilities, let's talk about this in 2D.

Essentially what is happening is that you are creating a mesh and you are assigning a value for a given combination of coordinates.

Say you start with a 10x10 grid where all values are zero.

零网格

If you want to create a "Cube" within this 2D space you can do something like this:

在此处输入图像描述

Where the values X[3:6] and Y[3:6] are set to 1.

This will give you a uniform volume throughout.

Say you want to make the centre of the cube more "concentrated" to signify something like the temperature of the centre of something.

You would just set to value to a higher int.

立方体

Now take this exact same concept and extend it to a 3D dimension and you've got yourself a 3D Volume. Flattening the values list there will give you a list which holds a value for every point in your 3D mesh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM