Python mpld3三维散点图
我正在探索mpld3库,不知道如何创建三维散点图。 使用Matplotlib,我将执行以下操作:Python mpld3三维散点图,python,matplotlib,mpld3,Python,Matplotlib,Mpld3,我正在探索mpld3库,不知道如何创建三维散点图。 使用Matplotlib,我将执行以下操作: 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() 类似地,我尝试使用mpld3(在Jupyter笔记本中):
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()
类似地,我尝试使用mpld3(在Jupyter笔记本中):
将matplotlib.pyplot作为plt导入
从mpl_toolkits.mplot3d导入Axes3D
导入mpld3
mpld3.enable_notebook()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter([1],[5],[7])
mpld3.display(fig)
我得到了错误
TypeError: array([ 0.94, 1.06]) is not JSON serializable
有什么想法吗
这是完整的错误日志:
()
6 N=50
7最大散射([1]、[5]、[7])
---->8 mpld3.显示器(图)
/usr/local/lib/python2.7/dist-packages/mpld3//\u display.pyc在显示中(图、closefig、local、**kwargs)
288如果关闭图:
289 plt.关闭(图)
-->290返回HTML(图到图HTML(图,**kwargs))
291
292
/usr/local/lib/python2.7/dist-packages/mpld3//u display.pyc in fig_to_html(fig,d3_url,mpld3_url,无额外内容,模板类型,figid,使用http,**kwargs)
234 d3_url=d3_url,
235 mpld3_url=mpld3_url,
-->236 figure_json=json.dumps(figure_json),
237额外的css=额外的css,
238额外_js=额外_js)
/转储中的usr/lib/python2.7/json/_init___u;.pyc(obj、skipkeys、sure_ascii、check_circular、allow_nan、cls、indent、separator、encoding、default、sort_key、**kw)
241 cls为无,缩进为无,分隔符为无,且
242编码=='utf-8',默认值为无且不排序(U键且不为kw):
-->243返回默认编码器编码(obj)
244如果cls为无:
245 cls=JSONEncoder
/编码中的usr/lib/python2.7/json/encoder.pyc(self,o)
例外情况没有那么详细。列表调用应该大致相同
206#相当于“”join()将执行的PySequence#Fast。
-->207 chunks=self.iterencode(o,\u one\u shot=True)
208如果不存在(块,(列表,元组)):
209块=列表(块)
/iterencode中的usr/lib/python2.7/json/encoder.pyc(self,o,_one_shot)
268 self.key\u分隔符、self.item\u分隔符、self.sort\u键、,
269个自助滑雪者(一杆)
-->270返回码(o,0)
271
272定义生成iterencode(标记、默认、编码器、缩进、浮动、,
/默认情况下的usr/lib/python2.7/json/encoder.pyc(self,o)
182
183 """
-->184 raise TypeError(repr(o)+“不可JSON序列化”)
185
186 def编码(自身,o):
TypeError:数组([0.94,1.06])不可JSON序列化
对不起,这不是一个完整的答案,而是一条很长的评论 我可以通过编辑mpld3来消除错误。_display.py如下所示:
class NumpyEncoder(json.JSONEncoder):
""" Special json encoder for numpy types """
def default(self, obj):
try:
obj=obj.tolist()
return json.JSONEncoder.encode(self, obj)
except AttributeError:
pass
if isinstance(obj, (numpy.int_, numpy.intc, numpy.intp, numpy.int8,
numpy.int16, numpy.int32, numpy.int64, numpy.uint8,
numpy.uint16,numpy.uint32, numpy.uint64)):
return int(obj)
elif isinstance(obj, (numpy.float_, numpy.float16, numpy.float32,
numpy.float64)):
return float(obj)
return json.JSONEncoder.default(self, obj)
但是,它会在笔记本中生成一个带有错误消息的空绘图:
Javascript error adding output!
TypeError: t.map is not a function
See your browser Javascript console for more details.
看起来mpld3不是为支持3D而设计的…不幸的是,mpld3目前不支持3D打印。它只支持2D打印。请查看以下链接: