I want to make a .html with a plot of some patches in a patch collection.
Im running the example from "https://matplotlib.org/examples/api/patch_collection.html"
Could someone please help?
thanks,
i get this error:
Traceback (most recent call last):
File "ttt.py", line 55, in
htmlfil.write(mpld3.fig_to_html(fig))
File "/usr/local/lib/python2.7/dist-packages/mpld3/_display.py", line 251, in fig_to_html
figure_json=json.dumps(figure_json, cls=NumpyEncoder),
File "/usr/lib/python2.7/json/__init__.py", line 250, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/usr/local/lib/python2.7/dist-packages/mpld3/_display.py", line 138, in default
return json.JSONEncoder.default(self, obj)
File "/usr/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: array([ 1.]) is not JSON serializable
I was having a similar error. It seems matplotlib.collections.pathcollection
did not sanitise its dictionary enough, leaving many parameters in the form of numpy instances. Currently mpld3 relies on a "NumpyEncoder" to serialise these numpy instances, and the encoder would complain if the instance array
is not listed.
Fix:
Modifiy _display.py
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)
elif isinstance(obj,(numpy.ndarray,)): #### This is the fix
return obj.tolist()
return json.JSONEncoder.default(self, obj)
Thanks shouldsee! Your fix solved my save_html problem. I was getting "array is not JSON serializable html" when trying to save a scatter plot.
It seems to me that this issue hasn't been solved in the latest release? I've got the latest version and had to fix it manually too. Thanks anyway @shouldsee !
I'll merge a pull request if someone creates one. Bonus points if the automatic tests all pass!
Suggested fix also resolved my issue.
Since javadba did not make a PR out of their commit I duplicated it.
This is still an issue with
matplotlib==2.2.2
mpld3==0.3
I fix it manually by overwriting mpld3._display.NumpyEncoder in my program
#mpld3 hack
class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
import numpy as np
if isinstance(obj, np.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
from mpld3 import _display
_display.NumpyEncoder = NumpyEncoder
Is there a production fix for this as yet? I introduced mpld3 into a Python project and am getting the same errors. Cheers!
This fix should be resolved in master. There has not been a release of mpl3d in almost 2 years. i do not know when/if there will be another as it is not actively unmaintained...
To use the mpld3 master branch
You can do something like
pip install git+git://github.com/mpld3/mpld3@master#egg=mpld3
or add
git+git://github.com/mpld3/mpld3@master#egg=mpld3
to your requirements.txt
file
@aflaxman @jakevdp perhaps it would be worth considering archiving this repository to indicate more clearly that the project is unmaintained:
https://help.github.com/articles/archiving-a-github-repository/
I did this for bokeh/bkcharts
@cliffckerr and company are planning to revive this project! if that has changed, i think archiving is a great idea.
I have missed the days with mpld3+ Django. Not sure I will be of any help but please keep this alive!
Sent from my iPhone
On 14 Jul 2018, at 00:01, Abraham Flaxman <[email protected]notifications@github.com> wrote:
@cliffckerrhttps://github.com/cliffckerr and company are planning to revive this project! if that has changed, i think archiving is a great idea.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/mpld3/mpld3/issues/434#issuecomment-404974279, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AHqXbA3CO-1c_aiOuNnJMbFdUtE_5j1Eks5uGSaZgaJpZM4P4Xoe.
Thanks all. I did not realise the code was unmaintained. It sounded perfect for my needs.
Cheers, Mark.
@addinall @aflaxman @bryevdv We are indeed reviving it! We are currently working on our fork (https://github.com/optimamodel/mpld3) but once everything is stable we'll be merging back here.
👍
Similar issue exists with fig_to_dict() as well. The returned dict cannot be serialized to json due to included ndarray unless right encoder is passed. One way to ovecome the issue is by using the internal NumpyEncoder class itself. For eg.
from mpld3._display import NumpyEncoder
json.dumps(mpld3.fig_to_dict(fig), cls=NumpyEncoder)
This is still an issue with
matplotlib==2.2.2 mpld3==0.3
I fix it manually by overwriting mpld3._display.NumpyEncoder in my program
#mpld3 hack class NumpyEncoder(json.JSONEncoder): def default(self, obj): import numpy as np if isinstance(obj, np.ndarray): return obj.tolist() return json.JSONEncoder.default(self, obj) from mpld3 import _display _display.NumpyEncoder = NumpyEncoder
This is still an issue with
matplotlib==2.2.2 mpld3==0.3
I fix it manually by overwriting mpld3._display.NumpyEncoder in my program
#mpld3 hack class NumpyEncoder(json.JSONEncoder): def default(self, obj): import numpy as np if isinstance(obj, np.ndarray): return obj.tolist() return json.JSONEncoder.default(self, obj) from mpld3 import _display _display.NumpyEncoder = NumpyEncoder
Thank you very much!
You´re a genius!
And I know that because I barely know anything of python and I was able to fix it myself.
Most helpful comment
I was having a similar error. It seems
matplotlib.collections.pathcollection
did not sanitise its dictionary enough, leaving many parameters in the form of numpy instances. Currently mpld3 relies on a "NumpyEncoder" to serialise these numpy instances, and the encoder would complain if the instancearray
is not listed.Fix:
Modifiy
_display.py