pythonmatplotlibmpld3

How to fix Object of type ndarray is not JSON serializable while using mpld3?


Python mpld3 '.show()' Gives me this error "Object of type ndarray is not JSON serializable"

I know that there are many duplicates of this question here, however, I haven't been able to find a recent one. This seems to be a common problem with mpld3.

Using Matplotlib version 3.1.3

Using mpld3 version 0.3

I'm also using Anaconda and did a conda upgrade --all and a conda update --all and still get this error.

Here is some code to reproduce the error. [It's really simple]

import matplotlib.pyplot as plt
import mpld3

fig, ax = plt.subplots()
x, y = [[1,2,3],[4,3,2]]

ax.scatter(x, y)

mpld3.show()

Any idea on a quick fix? Or if someone more knowing than I, could post this somewhere where it can be addressed appropriately?

The code above is just a bare bones example to show that there are no numpy arrays being used in the plot creation itself. It is no way a representation of what I'm trying to do [Show a matplotlib figure on a webpage in a simple way]. I know that matplotlib uses numpy internally, so it must be some issue with mpld3 parsing the figure properties dictionary.


Solution

  • I had the same issue as you and I was able to reproduce with your code. I was just testing a fresh installed mpld3 when I encountered this error.

    What I did to fix it is to uninstall mpld3 (which I had installed with pip) and re-install it with conda. Then it worked. Checking version with conda list gives me the same versions of both package as you have. I remember reading that one should not play with both pip and conda for python package managing. I always forget to apply to this rule.

    Hope it will fix it for you.