小编Jav*_*ith的帖子

从 Pandas 数据透视表生成 Plotly Heat Map

我已经搜索了这个主题几个小时了,但仍然无法让我的代码工作。我正在尝试从使用 Pandas 创建的数据透视表生成热图。我对编码很陌生,可能没有使用正确的术语,但我会尽力而为。

我的桌子看起来像这样:

在此处输入图片说明

它还有更多的行。我试图用 y 轴上的国家、x 上的 4 种所有权类型以及用作 z 值的数值生成一个情节热图。我遇到了很多错误,但我想我已经接近了,因为它到达我的最后一行并说“TypeError:'DataFrame' 类型的对象不是 JSON 可序列化的。” 我搜索了这个错误,但找不到任何我能理解的东西。我像这样设置了表格,但在 z、x 和 y 输入方面遇到了问题:

data = [go.Heatmap(z=[Country_Ownership_df[['Company Owned', 'Franchise', 'Joint Venture', 'Licensed']]],
                   y=[Country_Ownership_df['Country']],
                   x=['Company Owned', 'Franchise', 'Joint Venture', 'Licensed'],
                   colorscale=[[0.0, 'white'], [0.000001, 'rgb(191, 0, 0)'], [.001, 'rgb(209, 95, 2)'], [.005, 'rgb(244, 131, 67)'], [.015, 'rgb(253,174,97)'], [.03, 'rgb(249, 214, 137)'], [.05, 'rgb(224,243,248)'], [0.1, 'rgb(116,173,209)'], [0.3, 'rgb(69,117,180)'], [1, 'rgb(49,54,149)']])]

layout = go.Layout(
    margin = dict(t=30,r=260,b=30,l=260),
    title='Ownership',
    xaxis = dict(ticks=''),
    yaxis = dict(ticks='', nticks=0 )
) …
Run Code Online (Sandbox Code Playgroud)

python dictionary heat pandas plotly

3
推荐指数
2
解决办法
6873
查看次数

标签 统计

dictionary ×1

heat ×1

pandas ×1

plotly ×1

python ×1