将可旋转三维绘图从Python导出到HTML

2024-09-30 02:16:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要将一个可旋转的3D绘图导出到HTML,就像WriteWebGL does in R,但是要从Python/matplotlib导出

在Jupyter笔记本中运行时,您可以生成如下交互式绘图:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
...
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens');

(此示例的源代码here

如前所述,用户可以旋转3D绘图。如何从Python中将这种交互性导出为HTML


Tags: inimportnumpy绘图matplotlibhtmlasnp
1条回答
网友
1楼 · 发布于 2024-09-30 02:16:07

它必须是matplotlib吗?你可以用plotly来做这件事

下面是一个简单的例子。这将修改名为test.html的现有空文件,然后可以在web浏览器中打开该文件以使用交互式三维打印

import plotly.graph_objects as go
import numpy as np
import plotly.express as px

# Helix equation
t = np.linspace(0, 20, 100)
x, y, z = np.cos(t), np.sin(t), t

fig = go.Figure(data=[go.Scatter3d(
    x=x,
    y=y,
    z=z,
    mode='markers',
    marker=dict(
        size=12,
        color=z,                # set color to an array/list of desired values
        colorscale='Viridis',   # choose a colorscale
        opacity=0.8
    )
)])

# tight layout
fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))
fig.write_html("test.html") #Modifiy the html file
fig.show()

相关问题 更多 >

    热门问题