Python:AttributeError:'str'对象没有属性'keys'

时间:2019-12-10 07:52:08

标签: python python-3.x plotly

我想用代码制作国家地图:

import chart_studio.plotly as py
import pandas as pd

df = pd.read_csv('MS.csv')

data = [ dict(
       type = 'choropleth',
       locations = df['CODE'],
       z = df['MS'],
       text = df['COUNTRY'],
       colorscale = [[0,"rgb(5, 10, 172)"],[0.35,"rgb(40, 60, 190)"],[0.5,"rgb(70, 100, 245)"],\
           [0.6,"rgb(00, 120, 245)"],[0.7,"rgb(06, 130, 247)"],[1,"rgb(255, 255, 259)"]],
       autocolorscale = False,
       reversescale = True,
       marker = dict(
           line = dict (
               color = 'rgb(180,180,180)',
               width = 0.5
           ) ),
       colorbar = dict(
           autotick = False,
           tickprefix = None,
           title = 'ASes'),
     ) ]

layout = dict(
   title = 'MS COUNTRY',
   geo = dict(
       showframe = False,
       showcoastlines = False,
       projection = dict(
           type = 'Mercator'
       )
   )
)

fig = dict( data=data, layout=layout )
py.plot( fig, validate=False, filename='d3-world-map' )

这会使用Python 3生成一个精美的地图,但是当我在spyder Anaconda上运行时,它给出了错误:

Traceback (most recent call last):

  File "<ipython-input-8-fe3c136d7898>", line 1, in <module>
    runfile('C:/charts/plot (2).py', wdir='C:/charts')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/charts/plot (2).py", line 38, in <module>
    py.plot( fig, validate=False, filename='d3-world-map' )

  File "C:\ProgramData\Anaconda3\lib\site-packages\chart_studio\plotly\plotly.py", line 265, in plot
    figure, grid = _extract_grid_from_fig_like(figure)

  File "C:\ProgramData\Anaconda3\lib\site-packages\chart_studio\plotly\plotly.py", line 1870, in _extract_grid_from_fig_like
    trace_dict, reference_trace, grid, path + "data.{}.".format(i)

  File "C:\ProgramData\Anaconda3\lib\site-packages\chart_studio\plotly\plotly.py", line 1800, in _extract_grid_graph_obj
    "{path}{prop}.".format(path=path, prop=prop),

  File "C:\ProgramData\Anaconda3\lib\site-packages\chart_studio\plotly\plotly.py", line 1800, in _extract_grid_graph_obj
    "{path}{prop}.".format(path=path, prop=prop),

  File "C:\ProgramData\Anaconda3\lib\site-packages\chart_studio\plotly\plotly.py", line 1782, in _extract_grid_graph_obj
    for prop in list(obj_dict.keys()):

AttributeError: 'str' object has no attribute 'keys'

我该如何解决?

1 个答案:

答案 0 :(得分:1)

没有适当的数据样本和对数据的全面了解,很难知道导致错误的原因。但是,使用较新版本的plotly,您应该能够使用以下示例在Spyder中显示可绘制的图形。尝试一下,看看是否可行,然后告诉我们您所找到的内容。

版本信息:

Python 3.7.0
Spyder 3.3.1
Plotly 4.2.0

代码:

import plotly.graph_objects as go
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')

fig = go.Figure(data=go.Choropleth(
    locations = df['CODE'],
    z = df['GDP (BILLIONS)'],
    text = df['COUNTRY'],
    colorscale = 'Blues',
    autocolorscale=False,
    reversescale=True,
    marker_line_color='darkgray',
    marker_line_width=0.5,
    colorbar_tickprefix = '$',
    colorbar_title = 'GDP<br>Billions US$',
))

fig.update_layout(
    title_text='2014 Global GDP',
    geo=dict(
        showframe=False,
        showcoastlines=False,
        projection_type='equirectangular'
    ),
    annotations = [dict(
        x=0.55,
        y=0.1,
        xref='paper',
        yref='paper',
        text='Source: <a href="https://www.cia.gov/library/publications/the-world-factbook/fields/2195.html">\
            CIA World Factbook</a>',
        showarrow = False
    )]
)

fig.show()

情节:

enter image description here