在 IPython 或 Jupyter Notebook 中显示可旋转的 3D 图

Displaying rotatable 3D plots in IPython or Jupyter Notebook

(Mac OSX 10.10.5)

我可以从 matplotlib 网站 mplot3d the example code for a 3D scatter plot scatter3d_demo.py 复制,但是该图呈现为静态图像。我无法单击图表并动态旋转以查看 3D 绘制数据。

我已经使用示例代码实现了静态 3D 图 - 使用终端内的 (a) ipython,终端内的 (b) ipython 笔记本,以及 (c) ipython 从 Anaconda 启动器启动笔记本。

我认为我缺少一些非常基本的步骤作为假设知识。

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

作为 ipython 笔记本中的示例:

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

谁能认出我遗漏了什么?

正在查看 Python 3.3.6 文档,第 25.1 节也许是 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 程序的开发有关,所以我不确定这是否相关。 (正确,解决方案不需要这个。)

使用 %matplotlib notebook 而不是 %matplotlib inline 在 IPython notebook 中获取嵌入式交互式图形——这需要最新版本的 matplotlib (1.4+) 和 IPython (3.0 +).

对于Windows(Windows 8.1 对我来说),你可以使用

%matplotlib inline  
%matplotlib notebook
%pylab

相反。

注意:您必须同时执行所有三个命令,否则您将收到内核死机错误,然后笔记本电脑将自动重启。

对于像我这样不太熟悉的人,快速回答是:

%matplotlib(以上导入)(用于新 window 中的交互式图表)

导入matplot...等等

%matplotlib notebook(用于 JupyterNotebook 本身的图形交互

进口等...

进口等...

在 Windows 上,我可以通过以下方式启动笔记本,使情节以交互模式显示:

from matplotlib import use
use("Qt5Agg")
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
...

如果需要,请关闭笔记本电脑并重新启动它。

添加后:

%matplotlib inline  
%matplotlib notebook
%pylab

如果所有内容仍然是 2D,您只需使用此选项(第二张图片上的红色部分)来拖动和旋转图形,您的 3D 图像就会显示出来:

之前:

之后:

我正在使用 Windows 10 和 Jupyter Notebook 6.0.2。

对于 Colab 环境,我发现 HTML() 函数最有用:

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from numpy.random import rand
from IPython.display import HTML
from matplotlib import animation

m = rand(3,3) # m is an array of (x,y,z) coordinate triplets

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

for i in range(len(m)): # plot each point + it's index as text above
  x = m[i,0]
  y = m[i,1]
  z = m[i,2]
  label = i
  ax.scatter(x, y, z, color='b')
  ax.text(x, y, z, '%s' % (label), size=20, zorder=1, color='k')

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

def animate(frame):
  ax.view_init(30, frame/4)
  plt.pause(.001)
  return fig

anim = animation.FuncAnimation(fig, animate, frames=200, interval=50)
HTML(anim.to_html5_video())