1

I'm trying to use mpld3 to render a stack plot and am running into an issue when fig_to_html tries to serialize. It looks like it's running into the issue with NumPy described here:

NumPy array is not JSON serializable

Traceback (most recent call last): File "main.py", line 32, in mpld3.show() File "/usr/local/lib/python3.6/site-packages/mpld3/_display.py", line 358, in show html = fig_to_html(fig, **kwargs) File "/usr/local/lib/python3.6/site-packages/mpld3/_display.py", line 251, in fig_to_html figure_json=json.dumps(figure_json, cls=NumpyEncoder), File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/init.py", line 238, in dumps **kw).encode(obj) File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 257, in iterencode return _iterencode(o, 0) File "/usr/local/lib/python3.6/site-packages/mpld3/_display.py", line 138, in default return json.JSONEncoder.default(self, obj) File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 180, in default o.class.name) TypeError: Object of type 'ndarray' is not JSON serializable

Here's my plotting code for reference; plt.show() works fine. This only seems to be an issue with the stackplot, the regular plot is fine.

# get x,y,labels

# do plotting
fig, (ax1, ax2) = plt.subplots(2, 1)
#fig, (ax1) = plt.subplots(1, 1)
for yval in range(len(y)):
    ax1.plot(x, y[yval], label=labels[yval])

ax1.legend(loc='upper left')
ax1.ticklabel_format(useOffset=False)

ax2.stackplot(x, y, labels=labels)
ax2.legend(loc='upper left')
ax2.ticklabel_format(useOffset=False)

#plt.show()
mpld3.show()

Any help appreciated.

Matt White
  • 11
  • 2
  • Does this answer your question? [Json serialization error using matplotlib mpld3 with LinkedBrush](https://stackoverflow.com/questions/47380865/json-serialization-error-using-matplotlib-mpld3-with-linkedbrush) – Mohamed Taher Alrefaie Feb 10 '20 at 12:13

1 Answers1

0

Look like this is a duplicate of: MPLD3 with Python error

which you may need since mpld3 is not maintained anymore.

Matt White
  • 11
  • 2