0

ipython notebook 3.0.0

matplotlib 1.4.3

OS X 10.11.4

I am creating an interactive 3D scatter plot of a 3D data cube. I've included here a toy example that generates the same problems I am encountering trying to plot my data cube.

If I generate a matplot window outside of the notebook, when I manually close it (clicking the red x) it stalls with 'the wheel' until I force quit.

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

I've tried using mpld3 within the notebook but a non-interactive image displays along with the error

"TypeError: array([ 2., 20.]) is not JSON serializable"

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

Some quick research into JSON serialization has been non-fruitful.

What is the best way to create an interactive 3D matplotlib scatter plot that won't stall?

Thomas K
  • 39,200
  • 7
  • 84
  • 86
astromonerd
  • 907
  • 1
  • 15
  • 32

1 Answers1

0

In IPython, even if you aren't using the inline backend, it's best to use %matplotlib. This tells IPython and matplotlib to work together with eventloops and should help with the hang. To use the default GUI backend, use:

%matplotlib

Or to specify the qt backend:

%matplotlib qt

This avoids the need for plt.show() and the blocking of the kernel when plots are being drawn.

For best results, run this in he first cell of your notebook, on its own before any plotting commands.

minrk
  • 37,545
  • 9
  • 92
  • 87
  • 1
    Using `%matplotlib` does not yield a different result. I still am unable to close the external matplotlib window without it stalling until I force quit and kill the kernel. – astromonerd Jun 27 '16 at 01:24