python - Plotly 上 Pandas 数据库的 3D 散点类型错误

标签 python plotly

当我尝试在新更新版本的plotly、plotly 4.0 上绘制 pandas 数据框中的数据时,出现错误。 website 中的示例当我运行它们时可以完美地工作,但当我尝试将数据基于我创建的数据框时则不能。我正在 Jupyter Notebook 环境中工作,我还尝试了使用 go.Scatter3d 方法进行绘图并得到了相同的错误。

如果我使用 print(df) 打印它,我的数据框 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

最佳答案

结果我的数据框中的数字采用了某种无法解释的格式。我能够通过将所有条目转换为 float 来解决此问题。感谢大家的回复。

关于python - Plotly 上 Pandas 数据库的 3D 散点类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57079887/

相关文章:

python - 无法从 pandas 转到 dask 数据帧,内存错误

python - 使用请求模块导出 csv

python - 正则表达式: How to name a group using match result

R plotly : aspectmode ='cube' not making axes equal in 3D slcatter plot

r - 如何更改 R 中空白的千位分隔符?

python - 在命令行中运行 python 脚本不会打印任何输出

python - WSGI 与 PYthon/Django : serving alternating old and new versions of app 的怪异

python - 如何使用 Plotly-Dash 调整 slider 和选择器的范围

javascript - Node Js 上的 Potli - "socket hang up and read ECONNRESET"

r - 将 ggplot2 转换为 ggplotly 时缺少 geom_rect