【问题标题】:mpld3: stackplot causes serialization errormpld3:stackplot导致序列化错误
【发布时间】: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


【解决方案1】:

看起来这是一个副本: MPLD3 with Python error

您可能需要它,因为不再维护 mpld3。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-08
    • 2021-06-11
    • 2018-05-03
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多