从熊猫数据库的3D散点图上键入错误

时间:2019-07-17 15:52:04

标签: python plotly

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

如果我使用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

1 个答案:

答案 0 :(得分:0)

证明我数据框中的数字采用某种无法解释的格式。通过将所有条目转换为浮点数,我能够解决此问题。谢谢您的所有答复。