Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
andreatramacere opened this issue Dec 5, 2017 · 8 comments · Fixed by bbudescu/mpld3#1 or #456
Closed

Comments

@andreatramacere
Copy link

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)

@nicewoong
Copy link

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

@donovan-h-parks
Copy link

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?

@ceprio
Copy link

ceprio commented Apr 25, 2018

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)

@bbudescu
Copy link
Contributor

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.

@jonashaag
Copy link

Still an issue with latest version

@JupyterJones
Copy link

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

@ckerr-IDM
Copy link

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

@Wakaru-Science
Copy link

@ceprio 's comment worked for me too

@vladh vladh closed this as completed in 36d3e0a Jun 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
10 participants