2

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.

4
  • Does your error give you a specific line number where it occurs? Commented Jun 5, 2015 at 13:55
  • Edited to include the full error i get. Thanks Commented Jun 5, 2015 at 14:11
  • Hmm. I'm afraid I may not be able to help you. The only thing I can think to try (not sure why this would matter, but worth a shot, I suppose) is to change mpld3.show() to plt.show(), since that's how I've always "shown" plots in the past. Commented Jun 5, 2015 at 14:42
  • That's always worked. I just need to be able to save that and send to someone else in an interactive manner. Commented Jun 5, 2015 at 17:13

1 Answer 1

4

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.