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