制作美国热图?

2024-05-23 17:54:58 发布

您现在位置:Python中文网/ 问答频道 /正文

''

perstate = df[df['State'] != '']['State'].value_counts().to_dict()
data = [dict(
        type = 'choropleth',
        autocolorscale = False,
        colorscale = 'Blues',
        reversescale = True,
        locations = list(perstate.keys()),
        locationmode = 'USA-states',
        text = list(perstate.values()),
        z = list(perstate.values()),
        marker = dict(
            line = dict(
                color = 'rgb(255, 255, 255)',
                width = 2)
            ),
        )]

layout = dict(
         title = 'Bachelor contestants by State',
         geo = dict(
             scope = 'usa',
             projection = dict(type = 'albers usa'),
             countrycolor = 'rgb(255, 255, 255)',
             showlakes = True,
             lakecolor = 'rgb(255, 255, 255)')
         )

figure = dict(data = data, layout = layout)
iplot(figure)

''

这给了我以下信息: 类型错误
回溯(最近一次呼叫)

<ipython-input-294-8cdc028c4fa9> in <module>
      7         colorscale = 'Blues',
      8         reversescale = True,
----> 9         locations = list(perstate.keys()),
     10         locationmode = 'USA-states',
     11         text = list(perstate.values()),

TypeError: 'list' object is not callable

今天早些时候还有用,我什么都没变。为什么会这样?我用的是卡格尔的笔记本。我试着把‘State’列作为状态的缩写,我想做一个热图


Tags: truedfdatatypergbdictliststate
1条回答
网友
1楼 · 发布于 2024-05-23 17:54:58

基于此错误,很可能您已经将名称list重新定义为变量名,因此当您调用list()将变量强制转换为list类型时,Python解释器实际上正在尝试将重新定义的变量作为函数调用

示例:

print(list("123")) # prints ["1", "2", "3"]

现在,如果定义一个名为list的变量,则会发生以下情况:

list = ["redefining", "list"]
print(list("123")) # TypeError: 'list' object is not callable

Python认为您试图将变量作为函数调用

要解决此问题,只需将list重命名为另一个名称。完整的代码还没有发布,但您可能至少在某个地方重新定义了list

相关问题 更多 >