【问题标题】:Export rotable 3D plots from Python to HTML将可旋转的 3D 绘图从 Python 导出到 HTML
【发布时间】:2020-09-28 14:28:46
【问题描述】:

我需要将可旋转的 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?

【问题讨论】:

    标签: python matplotlib jupyter-notebook


    【解决方案1】:

    必须是matplotlib吗?您可以为此使用 plotly。

    这是一个简单的例子。这会修改一个名为 test.html 的现有空文件,然后您可以在 Web 浏览器中打开该文件以使用交互式 3D 绘图。

    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()
    

    【讨论】:

    • 不错的解决方案。我仍然会寻找一个只依赖于 matplotlib 的,以减少依赖。
    猜你喜欢
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 2019-03-16
    相关资源
    最近更新 更多