小编say*_*kar的帖子

TypeError:Interval 类型的对象在 Plotly、Python 中不可 JSON 序列化

我已将连续变量 x 转换为区间。我有一个带有数值的变量。数据框是:

data = {'x':[(-0.001, 7.0], (7.0, 19.0], (19.0, 97.0], (97.0, 817.0]],
        'y':[769.0, 810.0,757.0,652.0]}

# Create DataFrame      
df = pd.DataFrame(data)  
df 
Run Code Online (Sandbox Code Playgroud)

这两个变量的数据类型都是float64. 此外,变量“x”的描述如下:

Name: x, dtype: category
Categories (4, interval[float64]): [(-0.001, 7.0] < (7.0, 19.0] < (19.0, 97.0] < (97.0, 817.0]]
Run Code Online (Sandbox Code Playgroud)

现在,我使用绘图来绘制这两个变量之间的关系:

# figure
plot_data = [
    go.Scatter(
        x = df['x'],
        y = df['y'])]

plot_layout = go.Layout(title=' Relationship between x and y')               
fig = go.Figure(data=plot_data, layout=plot_layout)
pyoff.iplot(fig)
Run Code Online (Sandbox Code Playgroud)

但显示的错误是:

TypeError: Object of type Interval is not JSON serializable …
Run Code Online (Sandbox Code Playgroud)

python-3.x pandas plotly

8
推荐指数
1
解决办法
8329
查看次数

标签 统计

pandas ×1

plotly ×1

python-3.x ×1