【发布时间】:2019-02-26 17:14:39
【问题描述】:
我正在尝试使用 mpld3 来呈现堆栈图,但在 fig_to_html 尝试序列化时遇到了问题。看起来它遇到了这里描述的 NumPy 问题:
NumPy array is not JSON serializable
Traceback(最近一次调用最后一次):文件“main.py”,第 32 行,在 mpld3.show() 文件“/usr/local/lib/python3.6/site-packages/mpld3/_display.py”,第 358 行, 在节目中 html = fig_to_html(fig, **kwargs) 文件“/usr/local/lib/python3.6/site-packages/mpld3/_display.py”,第 251 行, 在 fig_to_html figure_json=json.dumps(figure_json, cls=NumpyEncoder), 文件 "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/init .py", 第 238 行,在转储中 **kw).encode(obj) 文件“/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py”, 第 199 行,在编码中 chunks = self.iterencode(o, _one_shot=True) 文件“/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py”, 第 257 行,在 iterencode 中 返回 _iterencode(o, 0) 文件“/usr/local/lib/python3.6/site-packages/mpld3/_display.py”,第 138 行, 在默认情况下 返回 json.JSONEncoder.default(self, obj) 文件“/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py”, 第 180 行,默认 o.class.name) TypeError: 'ndarray' 类型的对象不是 JSON 可序列化的
这是我的绘图代码供参考; plt.show() 工作正常。这似乎只是堆栈图的问题,常规图很好。
# get x,y,labels
# do plotting
fig, (ax1, ax2) = plt.subplots(2, 1)
#fig, (ax1) = plt.subplots(1, 1)
for yval in range(len(y)):
ax1.plot(x, y[yval], label=labels[yval])
ax1.legend(loc='upper left')
ax1.ticklabel_format(useOffset=False)
ax2.stackplot(x, y, labels=labels)
ax2.legend(loc='upper left')
ax2.ticklabel_format(useOffset=False)
#plt.show()
mpld3.show()
任何帮助表示赞赏。
【问题讨论】:
标签: mpld3