I am studying the mpld3 library and cannot figure out how to create 3D scatter plots. With Matplotlib, I would do:
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter([1],[5],[7]) plt.show()
Similarly, with mpld3 I tried (on a Jupyter laptop): import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import mpld3
mpld3.enable_notebook() fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter([1],[5],[7]) mpld3.display(fig)
and I get an error
TypeError: array([ 0.94, 1.06]) is not JSON serializable
Any ideas?
This is the complete error log:
Traceback (most recent call last) <ipython-input-26-54fc4a65da87> in <module>() 6 N = 50 7 ax.scatter([1],[5],[7]) ----> 8 mpld3.display(fig) /usr/local/lib/python2.7/dist-packages/mpld3/_display.pyc in display(fig, closefig, local, **kwargs) 288 if closefig: 289 plt.close(fig) --> 290 return HTML(fig_to_html(fig, **kwargs)) 291 292 /usr/local/lib/python2.7/dist-packages/mpld3/_display.pyc in fig_to_html(fig, d3_url, mpld3_url, no_extras, template_type, figid, use_http, **kwargs) 234 d3_url=d3_url, 235 mpld3_url=mpld3_url, --> 236 figure_json=json.dumps(figure_json), 237 extra_css=extra_css, 238 extra_js=extra_js) /usr/lib/python2.7/json/__init__.pyc in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, encoding, default, sort_keys, **kw) 241 cls is None and indent is None and separators is None and 242 encoding == 'utf-8' and default is None and not sort_keys and not kw): --> 243 return _default_encoder.encode(obj) 244 if cls is None: 245 cls = JSONEncoder /usr/lib/python2.7/json/encoder.pyc in encode(self, o) 205 # exceptions aren't as detailed. The list call should be roughly 206 # equivalent to the PySequence_Fast that ''.join() would do. --> 207 chunks = self.iterencode(o, _one_shot=True) 208 if not isinstance(chunks, (list, tuple)): 209 chunks = list(chunks) /usr/lib/python2.7/json/encoder.pyc in iterencode(self, o, _one_shot) 268 self.key_separator, self.item_separator, self.sort_keys, 269 self.skipkeys, _one_shot) --> 270 return _iterencode(o, 0) 271 272 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, /usr/lib/python2.7/json/encoder.pyc in default(self, o) 182 183 """ --> 184 raise TypeError(repr(o) + " is not JSON serializable") 185 186 def encode(self, o): TypeError: array([ 0.94, 1.06]) is not JSON serializable