在IPython或Jupyter Notebook中显示可旋转的3D图 [英] Displaying rotatable 3D plots in IPython or Jupyter Notebook

查看:699
本文介绍了在IPython或Jupyter Notebook中显示可旋转的3D图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(Mac OSX 10.10.5)

(Mac OSX 10.10.5)

我可以从matplotlib网站 mplot3d 复制3D散点图的示例代码 scatter3d_demo.py ,但是该图会渲染作为静态图片.我无法单击该图并动态旋转以查看3D绘制的数据.

I can reproduce from the matplotlib website mplot3d the example code for a 3D scatter plot scatter3d_demo.py, however the plot renders as a static image. I can not click on the graph and dynamically rotate to view the 3D plotted data.

我已使用示例代码实现了静态3D绘图-使用(a)在Terminal中运行的ipython笔记本,(b)在终端中运行的ipython笔记本,以及(c)从Anaconda启动器启动的ipython笔记本.

I have achieved the static 3D plot using the example code - using (a) ipython from within Terminal, (b) ipython notebook from within terminal, and (c) ipython notebook launched from the Anaconda launcher.

我认为我缺少一些非常基本的步骤,即假定的知识.

I think I am missing some very basic step as assumed knowledge.

在过去的学习中,绘图已打开一个具有图形查看器的GUI Python应用程序. (下面显示的代码中的解决方案2对此进行了打开.)也许我需要知道将输出图导出到该显示方法的代码? (是的,请使用%matplotlib(仅)作为第一行,而不使用内联或笔记本,如下面的代码块中的注释所示.)

In past learning, plotting has opened a GUI Python App which has a graph viewer. (Solution 2 in code shown below opens this.) Perhaps I need to know the code to export the output graph to that display method? (Yes, use %matplotlib (only) as first line without inline or notebook as shown in comments in code block below.)

以ipython笔记本为例:

As an example in ipython notebook:

    # These lines are comments
    # Initial setup from an online python notebook tutorial is below. 
    # Note the first line "%matplotlib inline" this is how the tutorial has it.
    # Two solutions 1. use: "%matplotlib notebook" graphs appear dynamic in the notebook.
    #               2. use: "%matplotlib" (only) graphs appear dynamic in separate window. 
    #    ( 2. is the best solution for detailed graphs/plots. )

    %matplotlib inline  
    import pandas as pd
    import numpy as np
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D

    pd.set_option('html',False)
    pd.set_option('max_columns',30)
    pd.set_option('max_rows',10)


    # What follows is a copy of the 3D plot example code.
    # Data is randomly generated so there is no external data import.

    def randrange(n, vmin, vmax):
        return (vmax-vmin)*np.random.rand(n) + vmin

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    n = 100
    for c, m, zl, zh in [('r', 'o', -60, -25), ('b', '^', -30, -5)]:
        xs = randrange(n, 23, 50)
        ys = randrange(n, 0, 100)
        zs = randrange(n, zl, zh)
        ax.scatter(xs, ys, zs, c=c, marker=m)

    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')

    plt.show()

有人可以识别我所缺少的吗?

Can someone identify what I am missing?

请参阅Python 3.3.6文档,第25.1节,也许是tkinter包...

Looking at Python 3.3.6 documentation, section 25.1perhaps the tkinter package ...

tkinter软件包("Tk接口")是Tk GUI工具包的标准Python接口.在大多数Unix平台以及Windows系统上都可以使用Tk和tkinter.

The tkinter package ("Tk interface") is the standard Python interface to the Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, as well as on Windows systems.

不过,我认为这与GUI程序的开发有关,因此我不确定这是否相关. (正确,解决方案不需要这样做.)

I think though, this relates to development of GUI programs so I am not sure this is relevant. (Correct, this was not needed for the solution.)

推荐答案

使用%matplotlib notebook代替%matplotlib inline在IPython笔记本中获取嵌入式交互式图形–这需要最新版本的matplotlib(1.4+)和IPython(3.0 +).

Use %matplotlib notebook instead of %matplotlib inline to get embedded interactive figures in the IPython notebook – this requires recent versions of matplotlib (1.4+) and IPython (3.0+).

这篇关于在IPython或Jupyter Notebook中显示可旋转的3D图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆