2

I am trying to show a scatter plot on a plotly world map. The code runs in a jupyter notebook.

Here is the code

mpis = []
colors = ["rgb(0,116,217)","rgb(255,65,54)","rgb(133,20,75)","rgb(255,133,27)","lightgrey"]
for i in range(len(mpi)):
    mpis.append(
        dict(
        type = 'scattergeo',
        #locationmode = 'world',
        lon = mpi['lon'][i],
        lat = mpi['lat'][i],
        text = str(mpi['MPI'][i]),
        marker = dict(
            size = 10,# mpi['MPI'][i]*100,
            color = colors[i%len(colors)],
            line = dict(width=0.5, color='rgb(40,40,40)'),
            sizemode = 'area'
        ),)

    )

layout = go.Layout(
    title = 'MPI',
    geo = dict(
            scope='world',
            #projection=dict( type = 'Mercator'),
            showland = True,
            landcolor = 'rgb(217, 217, 217)',
            subunitwidth=1,
            countrywidth=1,
            subunitcolor="rgb(255, 255, 255)",
            countrycolor="rgb(255, 255, 255)"
        ),)

fig = dict( data=mpis, layout=layout ) #fig =  go.Figure(layout=layout, data=mpis)
iplot( fig, validate=False)

This is an example of object in the data

{'lat': 36.734772499999998,
  'lon': 70.811995299999978,
  'marker': {'color': 'rgb(0,116,217)',
   'line': {'color': 'rgb(40,40,40)', 'width': 0.5},
   'size': 10,
   'sizemode': 'area'},
  'text': '',
  'type': 'scattergeo'},

but the result is the map is shown without any shape drawn.

2
  • 1
    can you show how to use the object in the code? I assume that this object is set to mpi; however, in your code you have mpi['lon'][i] and 'lon' in your object is a number, thus cannot be indexed. Commented Mar 7, 2018 at 9:32
  • you can find it in this kaggle kernel kaggle.com/asindico/kiva-borrowers-welfare Commented Mar 8, 2018 at 19:08

1 Answer 1

4
+50

Mercator should be 'mercator'

Lattitude and longtitude must be lists:

'lat': ['36.734772499999998'],
'lon': ['70.811995299999978'],

Here is working example:

import plotly.plotly as py
import plotly.graph_objs as go
from plotly import tools
from plotly.offline import iplot, init_notebook_mode
init_notebook_mode()


mpis = [{'lat': ['36.7347725'],
  'lon': ['70.8119953'],
  'marker': {'color': 'rgb(0,116,217)',
   'line': {'color': 'rgb(40,40,40)', 'width': 0.5},
   'size': 38.700000000000003,
   'sizemode': 'diameter'},
  'text': '0.387',
  'type': 'scattergeo'},
]


layout = go.Layout(
    title = 'MPI',
    showlegend = True,
    geo = dict(
            scope='world',
            projection=dict( type = 'natural earth'),
            showland = True,
            landcolor = 'rgb(217, 217, 217)',
            subunitwidth=1,
            countrywidth=1,
            subunitcolor="rgb(255, 255, 255)",
            countrycolor="rgb(255, 255, 255)"
        ),)

fig =  go.Figure(layout=layout, data=mpis)
iplot( fig, validate=False)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.