Json serialization error using matplotlib mpld3 with LinkedBrush

Based on a comment from @snakecharmerb I forked from mpld3, entered the suggested fix, and pip installed from my new branch on github.

The fix is here: https://github.com/javadba/mpld3/tree/display_fix . It may be installed via:

python -m pip install --user "git+https://github.com/javadba/mpld3@display_fix"

It works well: the json serialization error is gone and the linkage among the 9 charts functions properly:

enter image description here


For me, the solution given here did not work.

I had a networkx graph to visualize:

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()

Then it gave the "JSON not serializable" error. I found the above link, and tried the fix. The fix essentially says that if the object is of type numpy.ndarray, then change it to list.

But the object type of G.nodes is networkx.classes.reportviews.NodeView, not numpy.ndarray; thus it wasn't working.

So, I modified the file _display.py to add import networkx and added the following 2 lines in the default function in class NumpyEncoder to make it work:

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

Now it works:

enter image description here