MPLD3 出现 Python 错误
MPLD3 with Python error
我试图复制在这个网站上找到的例子:
http://mpld3.github.io/examples/scatter_tooltip.html
但我收到以下错误:Object of type 'ndarray' is not JSON serializable
我不知道我需要更改什么。
代码如下:
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)
mpld3.show()
准确的错误是:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2017.3.1\helpers\pydev\pydev_run_in_console.py", line 52, in run_file
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2017.3.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/Main/PycharmProjects/Macrobond_API/scenario testing.py", line 22, in <module>
mpld3.show()
File "C:\Users\Main\AppData\Local\Programs\Python\Python36-32\lib\site-packages\mpld3\_display.py", line 358, in show
html = fig_to_html(fig, **kwargs)
File "C:\Users\Main\AppData\Local\Programs\Python\Python36-32\lib\site-packages\mpld3\_display.py", line 251, in fig_to_html
figure_json=json.dumps(figure_json, cls=NumpyEncoder),
File "C:\Users\Main\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 238, in dumps
**kw).encode(obj)
File "C:\Users\Main\AppData\Local\Programs\Python\Python36-32\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\Main\AppData\Local\Programs\Python\Python36-32\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Users\Main\AppData\Local\Programs\Python\Python36-32\lib\site-packages\mpld3\_display.py", line 138, in default
return json.JSONEncoder.default(self, obj)
File "C:\Users\Main\AppData\Local\Programs\Python\Python36-32\lib\json\encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'ndarray' is not JSON serializable
针对此错误提出了一个未解决的问题,link 是
https://github.com/mpld3/mpld3/pull/435
一种解决方法是编辑 mpld3 模块中的 mpld3/_display.py
文件,编辑将在 default
函数
下方进行
之前:
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)
return json.JSONEncoder.default(self, obj)
之后:
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,)): # add this line
return obj.tolist() # add this line
return json.JSONEncoder.default(self, obj)
基本上你刚刚添加了
elif isinstance(obj, (numpy.ndarray,)): # add this line
return obj.tolist() # add this line
我试图复制在这个网站上找到的例子: http://mpld3.github.io/examples/scatter_tooltip.html
但我收到以下错误:Object of type 'ndarray' is not JSON serializable
我不知道我需要更改什么。
代码如下:
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)
mpld3.show()
准确的错误是:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2017.3.1\helpers\pydev\pydev_run_in_console.py", line 52, in run_file
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2017.3.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/Main/PycharmProjects/Macrobond_API/scenario testing.py", line 22, in <module>
mpld3.show()
File "C:\Users\Main\AppData\Local\Programs\Python\Python36-32\lib\site-packages\mpld3\_display.py", line 358, in show
html = fig_to_html(fig, **kwargs)
File "C:\Users\Main\AppData\Local\Programs\Python\Python36-32\lib\site-packages\mpld3\_display.py", line 251, in fig_to_html
figure_json=json.dumps(figure_json, cls=NumpyEncoder),
File "C:\Users\Main\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 238, in dumps
**kw).encode(obj)
File "C:\Users\Main\AppData\Local\Programs\Python\Python36-32\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\Main\AppData\Local\Programs\Python\Python36-32\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Users\Main\AppData\Local\Programs\Python\Python36-32\lib\site-packages\mpld3\_display.py", line 138, in default
return json.JSONEncoder.default(self, obj)
File "C:\Users\Main\AppData\Local\Programs\Python\Python36-32\lib\json\encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'ndarray' is not JSON serializable
针对此错误提出了一个未解决的问题,link 是 https://github.com/mpld3/mpld3/pull/435
一种解决方法是编辑 mpld3 模块中的 mpld3/_display.py
文件,编辑将在 default
函数
之前:
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)
return json.JSONEncoder.default(self, obj)
之后:
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,)): # add this line
return obj.tolist() # add this line
return json.JSONEncoder.default(self, obj)
基本上你刚刚添加了
elif isinstance(obj, (numpy.ndarray,)): # add this line
return obj.tolist() # add this line