Tuesday, March 13, 2018

Scatter Plot on Plotly Map

Leave a Comment

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.

1 Answers

Answers 1

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) 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment