python - 来自熊猫数据库的 3D 散点图的类型错误
问题描述
当我尝试在 plotly 的新更新版本 plotly 4.0 上绘制 pandas 数据框中的数据时出现错误。网站上的示例在我运行它们时运行良好,但当我尝试将数据基于我创建的数据框时却没有。我在 Jupyter Notebook 环境中工作,我也尝试了使用 go.Scatter3d 方法的 Plot 并得到了同样的错误。
如果我使用打印它,我的数据df
框看起来像这样print(df)
X Y Z
0 0 -5 0
1 5 0 0
2 10 5 0
3 15 10 0
4 0 -5 5
5 5 0 5
6 10 5 5
7 15 10 5
8 0 -5 10
9 5 0 10
10 10 5 10
11 15 10 10
12 0 -5 15
13 5 0 15
14 10 5 15
15 15 10 15
16 0 -10 0
17 5 -5 0
18 10 0 0
19 15 5 0
20 0 -10 5
21 5 -5 5
22 10 0 5
23 15 5 5
24 0 -10 10
25 5 -5 10
26 10 0 10
27 15 5 10
28 0 -10 15
29 5 -5 15
30 10 0 15
31 15 5 15
32 0 -15 0
33 5 -10 0
34 10 -5 0
35 15 0 0
36 0 -15 5
37 5 -10 5
38 10 -5 5
39 15 0 5
40 0 -15 10
41 5 -10 10
42 10 -5 10
43 15 0 10
44 0 -15 15
45 5 -10 15
46 10 -5 15
47 15 0 15
下面是我的绘图方法df
,
import plotly.express as px
fig = px.scatter_3d(df, x='X', y='Y', z='Z')
fig.show()
我收到一条很长的错误消息,结尾是,
TypeError: Object of type Zero is not JSON serializable
我一直在寻找解决此问题的方法,但找不到,我将不胜感激。
下面的整个错误消息,
TypeError Traceback (most recent call last)
<ipython-input-10-41b61849b7ea> in <module>
2
3 fig = px.scatter_3d(data_frame = df, x='X', y='Y', z='Z')
----> 4 fig.show()
c:\users\nihal\appdata\local\programs\python\python37\lib\site-packages\plotly\basedatatypes.py in show(self, *args, **kwargs)
2652 import plotly.io as pio
2653
-> 2654 return pio.show(self, *args, **kwargs)
2655
2656 def to_json(self, *args, **kwargs):
c:\users\nihal\appdata\local\programs\python\python37\lib\site-packages\plotly\io\_renderers.py in show(fig, renderer, validate, **kwargs)
372
373 # Mimetype renderers
--> 374 bundle = renderers._build_mime_bundle(fig_dict, renderers_string=renderer, **kwargs)
375 if bundle:
376 if not ipython_display:
c:\users\nihal\appdata\local\programs\python\python37\lib\site-packages\plotly\io\_renderers.py in _build_mime_bundle(self, fig_dict, renderers_string, **kwargs)
292 setattr(renderer, k, v)
293
--> 294 bundle.update(renderer.to_mimebundle(fig_dict))
295
296 return bundle
c:\users\nihal\appdata\local\programs\python\python37\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
92
93 json_compatible_fig_dict = json.loads(
---> 94 to_json(fig_dict, validate=False, remove_uids=False)
95 )
96
c:\users\nihal\appdata\local\programs\python\python37\lib\site-packages\plotly\io\_json.py in to_json(fig, validate, pretty, remove_uids)
54 opts["separators"] = (",", ":")
55
---> 56 return json.dumps(fig_dict, cls=PlotlyJSONEncoder, **opts)
57
58
c:\users\nihal\appdata\local\programs\python\python37\lib\json\__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
236 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
237 separators=separators, default=default, sort_keys=sort_keys,
--> 238 **kw).encode(obj)
239
240
c:\users\nihal\appdata\local\programs\python\python37\lib\site-packages\_plotly_utils\utils.py in encode(self, o)
42
43 # this will raise errors in a normal-expected way
---> 44 encoded_o = super(PlotlyJSONEncoder, self).encode(o)
45
46 # now:
c:\users\nihal\appdata\local\programs\python\python37\lib\json\encoder.py in encode(self, o)
197 # exceptions aren't as detailed. The list call should be roughly
198 # equivalent to the PySequence_Fast that ''.join() would do.
--> 199 chunks = self.iterencode(o, _one_shot=True)
200 if not isinstance(chunks, (list, tuple)):
201 chunks = list(chunks)
c:\users\nihal\appdata\local\programs\python\python37\lib\json\encoder.py in iterencode(self, o, _one_shot)
255 self.key_separator, self.item_separator, self.sort_keys,
256 self.skipkeys, _one_shot)
--> 257 return _iterencode(o, 0)
258
259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
c:\users\nihal\appdata\local\programs\python\python37\lib\site-packages\_plotly_utils\utils.py in default(self, obj)
111 except NotEncodable:
112 pass
--> 113 return _json.JSONEncoder.default(self, obj)
114
115 @staticmethod
c:\users\nihal\appdata\local\programs\python\python37\lib\json\encoder.py in default(self, o)
177
178 """
--> 179 raise TypeError(f'Object of type {o.__class__.__name__} '
180 f'is not JSON serializable')
181
TypeError: Object of type Zero is not JSON serializable
解决方案
原来我的数据框中的数字是某种无法解释的格式。我能够通过将所有条目转换为浮点数来解决这个问题。谢谢大家的回复。
推荐阅读
- python-3.x - 解释 gis 中的边界框
- postgresql - 使用 JDBC 在 Postgres 中存储大型 JSONB 文件
- r - 如何删除 R 中 2 行或更多行中具有相同值的行
- actionscript-3 - AS3 将船舶限制在可见舞台区域
- php - 如何从对象中删除值
- oauth-2.0 - 无需解释 HTML/Javascript 即可替代 OAuth 2.0 ROPC
- bash - EOF 中的 grep 命令似乎没有在远程主机上执行 [UNIX BASH]
- tcpsocket - 我应该使用什么端口号?
- reactjs - 如何删除表格中的特定行?
- python - 根据特定名称对数据框中的行进行分组