Json serialization error using matplotlib mpld3 with LinkedBrush

后端 未结 2 764
南方客
南方客 2021-01-02 05:15

The mpld3 (matplotlib on d3) example for LinkedBrush http://mpld3.github.io/examples/linked_brush.html provides the foll

相关标签:
2条回答
  • 2021-01-02 05:31

    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:

    0 讨论(0)
  • 2021-01-02 05:35

    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:

    0 讨论(0)
提交回复
热议问题