Python & Matplotlib: Make 3D plot interactive in Jupyter Notebook

后端 未结 6 1202
臣服心动
臣服心动 2020-12-22 18:22

I use Jupyter Notebook to make analysis of datasets. There are a lot of plots in the notebook, and some of them are 3d plots.

I\'m wondering if it is possible

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-22 19:06

    A solution I came up with is to use a vis.js instance in an iframe. This shows an interactive 3D plot inside a notebook, which still works in nbviewer. The visjs code is borrowed from the example code on the 3D graph page

    A small notebook to illustrate this: demo

    The code itself:

    from IPython.core.display import display, HTML
    import json
    
    def plot3D(X, Y, Z, height=600, xlabel = "X", ylabel = "Y", zlabel = "Z", initialCamera = None):
    
        options = {
            "width": "100%",
            "style": "surface",
            "showPerspective": True,
            "showGrid": True,
            "showShadow": False,
            "keepAspectRatio": True,
            "height": str(height) + "px"
        }
    
        if initialCamera:
            options["cameraPosition"] = initialCamera
    
        data = [ {"x": X[y,x], "y": Y[y,x], "z": Z[y,x]} for y in range(X.shape[0]) for x in range(X.shape[1]) ]
        visCode = r"""
           
           
           
    """ htmlCode = "" display(HTML(htmlCode))

提交回复
热议问题