Python:TypeError:array([1.])不可JSON序列化
我想将python绘图转换为Html。我使用了参考示例,并将其更改为将绘图转换为Html页面。下面是我的代码:Python:TypeError:array([1.])不可JSON序列化,python,numpy,matplotlib,mpld3,Python,Numpy,Matplotlib,Mpld3,我想将python绘图转换为Html。我使用了参考示例,并将其更改为将绘图转换为Html页面。下面是我的代码: import matplotlib as plta plta.use('Agg') import matplotlib.pyplot as plt import numpy as np import mpld3 fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE')) N = 100 scatter = ax.scatter
import matplotlib as plta
plta.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import mpld3
fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'))
N = 100
scatter = ax.scatter(np.random.normal(size=N),
np.random.normal(size=N),
c=np.random.random(size=N),
s=1000 * np.random.random(size=N),
alpha=0.3,
cmap=plt.cm.jet)
ax.grid(color='white', linestyle='solid')
ax.set_title("Scatter Plot (with tooltips!)", size=20)
labels = ['point {0}'.format(i + 1) for i in range(N)]
tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels)
mpld3.plugins.connect(fig, tooltip)
html_graph = mpld3.fig_to_html(fig)
with open('plot.html', 'w') as the_file:
the_file.write(html_graph)
现在,当我运行上述代码时,它抛出如下错误:
/usr/local/lib/python2.7/dist-packages/matplotlib/cbook/deprecation.py:106: MatplotlibDeprecationWarning: The axisbg attribute was deprecated in version 2.0. Use facecolor instead.
warnings.warn(message, mplDeprecation, stacklevel=1)
Traceback (most recent call last):
File "d3tool.py", line 24, in <module>
html_graph = 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
/usr/local/lib/python2.7/dist packages/matplotlib/cbook/deprecation.py:106:MatplotlibDeprecationWarning:axisbg属性在2.0版中被弃用。改用facecolor。
warnings.warn(消息,mpldeprection,stacklevel=1)
回溯(最近一次呼叫最后一次):
文件“d3tool.py”,第24行,在
html\u graph=mpld3.fig到html(图)
文件“/usr/local/lib/python2.7/dist packages/mpld3/_display.py”,第251行,如图所示
图json=json.dumps(图json,cls=NumpyEncoder),
文件“/usr/lib/python2.7/json/_init__.py”,第250行,转储
排序键=排序键,**千瓦)。编码(obj)
文件“/usr/lib/python2.7/json/encoder.py”,第207行,在encode中
chunks=self.iterencode(o,\u one\u shot=True)
iterencode中的文件“/usr/lib/python2.7/json/encoder.py”,第270行
返回_iterencode(o,0)
默认情况下,文件“/usr/local/lib/python2.7/dist packages/mpld3/_display.py”第138行
返回json.JSONEncoder.default(self,obj)
默认情况下,文件“/usr/lib/python2.7/json/encoder.py”第184行
raise TypeError(repr(o)+“不可JSON序列化”)
TypeError:数组([1.])不可JSON序列化
调用fig_to_html()函数时,第24行出现错误。请帮助我。我已通过更改文件“mpld3/_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)
有关更多详细信息,请参阅。此问题目前在mpld3回购协议中公开。这里有一个链接:+1,+1感谢您花时间记录您自己的解决方案,并提供适当的参考。它通常非常有用,在这种情况下,比bug报告更有用。