Mpld3: bug in fig_to_html, TypeError: array([ 1.]) is not JSON serializable

Created on 5 Dec 2017  路  8Comments  路  Source: mpld3/mpld3

It happens when adding colorbar to 2D images generated with imshow, only on Mac.

mpld3.__version__='0.3.1.dev1' (failing also in previous versions)

matplotlib.__version__='2.1.0'

import matplotlib.pyplot as plt, mpld3 import numpy as np fig,ax=plt.subplots(1,1) im=ax.imshow(np.zeros((100,100))) fig.colorbar(im, ax=ax) mpld3.fig_to_html(fig)

bug possibly fixed

Most helpful comment

I found a fix to this one: you can edit the _display.py file found in Lib\site-packages\mpld3 and replace the NumpyEncoder class by this one:

class NumpyEncoder(json.JSONEncoder):
    """ Special json encoder for numpy types """

    def default(self, obj):
        if isinstance(obj, (numpy.int_, numpy.intc, numpy.intp, numpy.int8,
            numpy.int16, numpy.int32, numpy.int64, numpy.uint8,
            numpy.uint16,numpy.uint32, numpy.uint64)):
            return int(obj)
        elif isinstance(obj, (numpy.float_, numpy.float16, numpy.float32, 
            numpy.float64)):
            return float(obj)
        try: # Added by ceprio 2018-04-25
            iterable = iter(obj)
        except TypeError:
            pass
        else:
            return list(iterable)
         # Let the base class default method raise the TypeError
        return json.JSONEncoder.default(self, obj)

All 8 comments

  • I got the same error.
  • What is your python version ?

I am also getting the same error. I'm using Python 2.7.4, mpld3 v0.2, and matplotlib v2.1.2. I believe the issue is with matplotlib. Are there plans to update mpld3 to be compatible with the latest version of matplotlib?

I found a fix to this one: you can edit the _display.py file found in Lib\site-packages\mpld3 and replace the NumpyEncoder class by this one:

class NumpyEncoder(json.JSONEncoder):
    """ Special json encoder for numpy types """

    def default(self, obj):
        if isinstance(obj, (numpy.int_, numpy.intc, numpy.intp, numpy.int8,
            numpy.int16, numpy.int32, numpy.int64, numpy.uint8,
            numpy.uint16,numpy.uint32, numpy.uint64)):
            return int(obj)
        elif isinstance(obj, (numpy.float_, numpy.float16, numpy.float32, 
            numpy.float64)):
            return float(obj)
        try: # Added by ceprio 2018-04-25
            iterable = iter(obj)
        except TypeError:
            pass
        else:
            return list(iterable)
         # Let the base class default method raise the TypeError
        return json.JSONEncoder.default(self, obj)

For matplotlib version 2.2.3, I found it to be enough to convert linewidths from numpy arrays to lists in mpld3.mpld3renderer.MPLD3Renderer.draw_path_collection.

Still an issue with latest version

@jonashaag @ceprio 's commented on Apr 26, 2018 above, just now fixed it with me.

Hi @jonashaag and @JupyterJones , could you confirm if this is also issue on https://github.com/sciris/mpld3?

@ceprio 's comment worked for me too

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dmiller7115 picture dmiller7115  路  5Comments

ajasja picture ajasja  路  17Comments

jakevdp picture jakevdp  路  4Comments

ledmonster picture ledmonster  路  4Comments

arnaudrenaud picture arnaudrenaud  路  4Comments