Maya
Maya

Reputation: 71

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

I want to make country map from code:

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' )

This produces a fine map with Python 3 but when I run on spyder Anaconda It is giving error:

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'

How can I solve this?

Upvotes: 0

Views: 1964

Answers (1)

vestland
vestland

Reputation: 61094

Without a proper data sample and complete knowledge of your data, it's hard to know what's the cause of the error. But with newer versions of plotly you should be able to show plotly figures in Spyder with examples like the one below. Try that, see if it works, and tell us what you find out.

Version info:

Python 3.7.0
Spyder 3.3.1
Plotly 4.2.0

Code:

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()

Plot:

enter image description here

Upvotes: 1

Related Questions