使用matplotlib mpld3和LinkedBrush时出现的Json序列化错误

10

mpld3(基于d3matplotlib)中的LinkedBrush示例http://mpld3.github.io/examples/linked_brush.html提供了以下代码示例:

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris

import mpld3
from mpld3 import plugins, utils


data = load_iris()
X = data.data
y = data.target

# dither the data for clearer plotting
X += 0.1 * np.random.random(X.shape)

fig, ax = plt.subplots(4, 4, sharex="col", sharey="row", figsize=(8, 8))
fig.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95,
                    hspace=0.1, wspace=0.1)

for i in range(4):
    for j in range(4):
        points = ax[3 - i, j].scatter(X[:, j], X[:, i],
                                      c=y, s=40, alpha=0.6)

# remove tick labels
for axi in ax.flat:
    for axis in [axi.xaxis, axi.yaxis]:
        axis.set_major_formatter(plt.NullFormatter())

# Here we connect the linked brush plugin
plugins.connect(fig, plugins.LinkedBrush(points))

mpld3.show()

公共网页上显示了链接输出的矩阵,但在本地运行时会出现json序列化错误:

Traceback (most recent call last):
  File "/git/scalatesting/src/main/python/mpld3_linkedbrush.py", line 34, in <module>
    mpld3.show()
  File "/usr/local/lib/python2.7/site-packages/mpld3/_display.py", line 358, in show
    html = fig_to_html(fig, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/mpld3/_display.py", line 251, in fig_to_html
    figure_json=json.dumps(figure_json, cls=NumpyEncoder),
  File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 251, in dumps
    sort_keys=sort_keys, **kw).encode(obj)
  File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/usr/local/lib/python2.7/site-packages/mpld3/_display.py", line 138, in default
    return json.JSONEncoder.default(self, obj)
  File "/usr/local/Cellar/python/2.7.14/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([ 1.]) is not JSON serializable

本地环境是

$pip show mpld3
Name: mpld3
Version: 0.3
Summary: D3 Viewer for Matplotlib
Home-page: http://mpld3.github.com
Author: Jake VanderPlas
Author-email: jakevdp@cs.washington.edu
License: BSD 3-clause
Location: /usr/local/lib/python2.7/site-packages
and
$python -V
Python 2.7.14

今天安装了 mpld3,是否存在 mpld3 的版本问题?有什么其他的建议吗?


1
似乎是这个错误的一个实例,同样的解决方案在https://dev59.com/XV8d5IYBdhLWcg3wlzH6中被建议 - 在连接插件之前调用points.tolist() - snakecharmerb
@snakecharmerb 随意将其作为答案。 - WestCoastProjects
2个回答

20
根据@snakecharmerb的评论,我从中派生出来,并输入了建议的修复程序,然后从github上的新分支上进行了pip安装。修复程序在这里:https://github.com/javadba/mpld3/tree/display_fix。可以通过以下方式安装:
python -m pip install --user "git+https://github.com/javadba/mpld3@display_fix"

它很有效: json序列化错误已经消失,9个图表之间的连接正常运作:

输入图像描述


我简直不敢相信他们还没有接受你的PR。这让我花了好几个小时才找到答案。顺便说一下,谢谢你! - Payam

1

对我来说,这里 给出的解决方案没有起作用。

我有一个 networkx 图形要可视化:

import matplotlib.pyplot as plt
import numpy as np
import mpld3

import networkx as nx
G = nx.path_graph(4)
pos = nx.spring_layout(G)

fig, ax = plt.subplots(subplot_kw=dict(facecolor='#EEEEEE'))
scatter = nx.draw_networkx_nodes(G, pos, ax=ax)
nx.draw_networkx_edges(G, pos, ax=ax)

labels = G.nodes()
tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels)
mpld3.plugins.connect(fig, tooltip)

mpld3.show()

然后它出现了“JSON不可序列化”的错误。我找到了上面的链接,并尝试了修复方法。修复方法基本上是说,如果对象是numpy.ndarray类型,则将其更改为列表。

但是G.nodes对象类型为networkx.classes.reportviews.NodeView而不是numpy.ndarray,因此无法工作。

因此,我修改了_display.py文件,添加了import networkx并在NumpyEncoder类的default函数中添加了以下2行以使其工作:

elif isinstance(obj,networkx.classes.reportviews.NodeView):
    return list(obj)

现在它可行:

enter image description here


1
这似乎表明,解决不同数据类型的问题需要特定于类的微调。感谢更新。 - WestCoastProjects

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接