在Python中使用 Plotly 绘制 3D 体积图
Plotly 是一个Python库,用于设计图形,尤其是交互式图形。它可以绘制各种图形和图表,如直方图、条形图、箱线图、散布图等等。它主要用于数据分析和财务分析。 Plotly 是一个交互式可视化库。
Plotly 中的体积图
体积图是带有 go.volume 的图,它显示了许多用于渲染体积的部分透明等值面。 go 的 opacityscale 参数。体积会产生深度效果并生成更好的体积渲染。三维体积可视化是一种允许人们观察和操作 3D 体积数据的方法。它根据由多边形和线近似的表面和边缘来表示 3D 对象。
Syntax: plotly.graph_objects.3d_Volume(arg=None, autocolorscale=None, caps=None, cauto=None, cmax=None, cmid=None, cmin=None, coloraxis=None, colorbar=None, colorscale=None, contour=None, customdata=None, customdatasrc=None, flatshading=None, hoverinfo=None, hoverinfosrc=None, hoverlabel=None, hovertemplate=None, hovertemplatesrc=None, hovertext=None, hovertextsrc=None, ids=None, idssrc=None, isomax=None, isomin=None, legendgroup=None, lighting=None, lightposition=None, meta=None, metasrc=None, name=None, opacity=None, opacityscale=None, reversescale=None, scene=None, showlegend=None, showscale=None, slices=None, spaceframe=None, stream=None, surface=None, text=None, textsrc=None, uid=None, uirevision=None, value=None, valuesrc=None, visible=None, x=None, xsrc=None, y=None, ysrc=None, z=None, zsrc=None, **kwargs)
Parameters:
x – Sets the X coordinates of the vertices on X axis.
y – Sets the Y coordinates of the vertices on Y axis.
z – Sets the Z coordinates of the vertices on Z axis.
value – Sets the 4th dimension (value) of the vertices.
例子:
Python3
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()
Python3
import plotly.graph_objects as go
import plotly.express as px
import numpy as np
df = px.data.tips()
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,
caps= dict(x_show=False, y_show=False, z_show=True),
))
fig.show()
Python3
import plotly.graph_objects as go
import plotly.express as px
import numpy as np
df = px.data.tips()
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,
caps= dict(x_show=False, y_show=True, z_show=False),
))
fig.show()
Python3
import plotly.graph_objects as go
import plotly.express as px
import numpy as np
df = px.data.tips()
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(),
isomin=-0.5,
isomax=0.5,
value=values.flatten(),
opacity=0.1,
opacityscale=[[-0.5, 1], [-0.2, 0], [0.2, 0], [0.5, 1]],
colorscale='RdBu'
))
fig.show()
输出:
在体积图中自定义上限
在情节中,为了更清晰地显示内表面,可以移除盖子。大写默认是可见的。
示例 1:
Python3
import plotly.graph_objects as go
import plotly.express as px
import numpy as np
df = px.data.tips()
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,
caps= dict(x_show=False, y_show=False, z_show=True),
))
fig.show()
输出:
示例 2:
Python3
import plotly.graph_objects as go
import plotly.express as px
import numpy as np
df = px.data.tips()
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,
caps= dict(x_show=False, y_show=True, z_show=False),
))
fig.show()
输出:
自定义不透明度比例
在 plotly 中,可以定义自定义不透明度比例,将标量值与不透明度值成比例地映射。该值从 0-1 给出,最大不透明度由 opacity 关键字提供。
例子:
Python3
import plotly.graph_objects as go
import plotly.express as px
import numpy as np
df = px.data.tips()
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(),
isomin=-0.5,
isomax=0.5,
value=values.flatten(),
opacity=0.1,
opacityscale=[[-0.5, 1], [-0.2, 0], [0.2, 0], [0.5, 1]],
colorscale='RdBu'
))
fig.show()
输出: