mpld3
的{{1}} matplotlib
d3
}示例
http://mpld3.github.io/examples/linked_brush.html提供了以下代码示例:
LinkedBrush
虽然公共网页显示了链接输出的矩阵,但在本地运行时会出现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
今天安装了$python -V
Python 2.7.14
。是否存在mpld3
版本问题?还有其他建议吗?
答案 0 :(得分:9)
根据来自mpld3
的@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 serialization
错误消失,9个图表之间的联系正常运行:
答案 1 :(得分:1)
对我来说,给定here的解决方案不起作用。
我有一个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
,则将其更改为list。
但是G.nodes
的对象类型是networkx.classes.reportviews.NodeView
,而不是numpy.ndarray
;因此它不起作用。
因此,我修改了文件_display.py
以添加import networkx
,并在类default
的{{1}}函数中添加了以下两行以使其起作用:
NumpyEncoder
现在可以使用了