【问题标题】:Matplotlib window stalls using ipython notebookMatplotlib 窗口使用 ipython notebook 停止
【发布时间】:2016-06-25 14:51:10
【问题描述】:

ipython 笔记本 3.0.0

matplotlib 1.4.3

OS X 10.11.4

我正在创建一个 3D 数据立方体的交互式 3D 散点图。 我在这里包含了一个玩具示例,它会产生与我在绘制数据立方体时遇到的相同问题。

如果我在笔记本之外生成一个 matplot 窗口,当我手动关闭它(单击红色 x)时,它会随着“滚轮”停止,直到我强制退出。

#Generate matplot window outside of the notebook

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

#from matplot3d tutorial

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', -50, -25), ('b', '^', -30, -5)]:
    xs = randrange(n, 23, 32)
    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()

我尝试在笔记本中使用 mpld3,但显示非交互式图像以及错误

"TypeError: array([ 2., 20.]) 不是 JSON 可序列化的"

#Use mpld3 within notebook

import matplotlib.pyplot as plt
import numpy as np
import mpld3
from mpl_toolkits.mplot3d import Axes3D
mpld3.enable_notebook()

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', -50, -25), ('b', '^', -30, -5)]:
    xs = randrange(n, 23, 32)
    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')

对 JSON 序列化的一些快速研究没有取得成果。

创建不会停止的交互式 3D matplotlib 散点图的最佳方法是什么?

【问题讨论】:

    标签: matplotlib ipython-notebook jupyter-notebook mpld3


    【解决方案1】:

    在 IPython 中,即使您不使用内联后端,最好使用%matplotlib。这告诉 IPython 和 matplotlib 与事件循环一起工作,并且应该有助于解决问题。要使用默认的 GUI 后端,请使用:

    %matplotlib
    

    或者指定qt后端:

    %matplotlib qt
    

    这避免了在绘制绘图时需要plt.show() 和内核阻塞。

    为获得最佳效果,请在笔记本的第一个单元格中运行此命令,在任何绘图命令之前单独运行。

    【讨论】:

    • 使用%matplotlib 不会产生不同的结果。我仍然无法关闭外部 matplotlib 窗口而不停止,直到我强制退出并杀死内核。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 2014-02-06
    • 2017-03-19
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多