【问题标题】:Displaying rotatable 3D plots in IPython or Jupyter Notebook在 IPython 或 Jupyter Notebook 中显示可旋转的 3D 图
【发布时间】:2016-01-30 21:25:35
【问题描述】:

(Mac OSX 10.10.5)

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

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

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

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

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

有人能找出我缺少的东西吗?

查看 Python 3.3.6 文档,第 25.1 节可能是 tkinter 包 ...

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

不过,我认为这与 GUI 程序的开发有关,所以我不确定这是否相关。 (正确,解决方案不需要。)

【问题讨论】:

  • 尝试添加对ax.mouse_init()的呼叫
  • OK,在 plt.show() 之前在哪里?
  • 我不确定这是否重要,但这就是我要说的地方。
  • 好的,谢谢。我在这里试过了,没有用。我刚刚在我原来的问题中添加了一个可能的解决方案的新方向。
  • @Matt Nice tidy 编辑了这个问题。谢谢。我想这是我加入 SO 的第一篇文章。

标签: macos matplotlib plot jupyter-notebook ipython


【解决方案1】:

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

【讨论】:

  • 是的,就是这样。感谢 jakevdp。对于没有使用 ipython notebook 的经验,并使用在线示例学习的其他人,在将 %matplotlib inline 更改为 %matplotlib notebook 时,也可以转到菜单 Cell,然后选择 Run All。
  • 冒着增加原始问题的风险,您知道如何在自己的独立窗口中打开这样的图表。一些包含大量数据的图表需要很大才能进行全面检查。能够做到这一点,并让它们动态、可旋转,将是最好的解决方案。这个 ipython notebook 环境中是否存在这种情况?
  • 如果您只是单独调用%matplotlib,它将使用您系统的默认后端(假设在安装时已正确配置)并在新窗口中打开该图。
  • 是的,又是这样。我将更新问题以包含此解决方案。
  • 使用%pylabnot recommended。请改用%matplotlib 和显式导入。
【解决方案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())

【讨论】:

    【解决方案3】:

    对于 Windows(我是 Windows 8.1),您可以使用

    %matplotlib inline  
    %matplotlib notebook
    %pylab
    

    改为。

    注意:你必须同时执行所有三个命令,否则你会得到一个kernal dead错误,然后笔记本会自动重启。

    【讨论】:

      【解决方案4】:

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

      %matplotlib(高于导入)(用于新窗口中的交互式图表)

      导入matplot...等

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

      导入等...

      导入等...

      【讨论】:

      • 查看 OP 问题的顶部
      【解决方案5】:

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

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

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

      【讨论】:

      • 这是在 Pycharm 上对我有用的唯一解决方案。谢谢!
      【解决方案6】:

      添加后:

      %matplotlib inline  
      %matplotlib notebook
      %pylab
      

      如果一切仍为 2D,您只需使用此选项(在第二张图像上为红色)拖动和旋转图形,您的 3D 图像就会显示出来:

      之前:

      之后:

      我使用的是 Windows 10 和 Jupyter Notebook 6.0.2。

      【讨论】:

        猜你喜欢
        • 2018-10-03
        • 2015-03-09
        • 2018-11-06
        • 1970-01-01
        • 1970-01-01
        • 2012-05-17
        • 1970-01-01
        • 1970-01-01
        • 2020-09-21
        相关资源
        最近更新 更多