user2592729
user2592729

Reputation: 459

Interactive python matplotlib

python noob here. I'm trying to recreate this example

from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt, mpld3
from matplotlib import colors 
from matplotlib.colors import colorConverter
import matplotlib.animation as animation
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

cc = lambda arg: colorConverter.to_rgba(arg, alpha=0.6)

xs = np.arange(0, 10, 0.4)
verts = []
zs = [0.0, 1.0, 2.0, 3.0]
for z in zs:
    ys = np.random.rand(len(xs))
    ys[0], ys[-1] = 0, 0
    verts.append(list(zip(xs, ys)))

poly = PolyCollection(verts, facecolors = [cc('r'), cc('g'), cc('b'),
                                           cc('y')])
poly.set_alpha(0.7)
ax.add_collection3d(poly, zs=zs, zdir='y')

ax.set_xlabel('X')
ax.set_xlim3d(0, 10)
ax.set_ylabel('Y')
ax.set_ylim3d(-1, 4)
ax.set_zlabel('Z')
ax.set_zlim3d(0, 1)
mpld3.show()

I wanted to save the interactive plot (and send it to someone who doesn't use python) so mpld3.show() seems like it could do the trick. Only I keep getting this error

    Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/mpld3/_display.py", line 338, in show
    html = fig_to_html(fig, **kwargs)
  File "/Library/Python/2.7/site-packages/mpld3/_display.py", line 236, in fig_to_html
    figure_json=json.dumps(figure_json),
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: array([  0.,  10.]) is not JSON serializable

I understand that this is because something is an np array not a list, but when I

type(verts) 

I get

<type 'list'>

So I'm not sure what that "array([ 0., 10.])" is/ how to fix this. Please include an explanation of how to extract/ manipulate objects of type

<class 'mpl_toolkits.mplot3d.art3d.Poly3DCollection'>

Excuse my noobiness. Thanks y'all.

Upvotes: 2

Views: 642

Answers (1)

omegamanda
omegamanda

Reputation: 398

According to the mpld3 github, "3D plots are not currently supported in mpld3", and thus produces the error you're seeing

I ran across your question because I am looking for the same solution (the ability to share an interactive 3D matplotlib plot with someone who doesn't have Python). I realize this is not an answer, but I thought it was still worth sharing? (I'm also a noob, so I can't even comment on your question)

Upvotes: 4

Related Questions