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

查看:21
本文介绍了在 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) 终端内的 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 notebook 为例:

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 接口.Tk 和 tkinter 在大多数 Unix 平台以及 Windows 系统上都可用.

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 notebook 中获取嵌入的交互式图形 –这需要最新版本的 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天全站免登陆