Plotly Map上的散点图

时间:2018-03-04 16:32:32

标签: python jupyter-notebook plotly

我试图在情节世界地图上显示散点图。 代码在jupyter笔记本中运行。

这是代码

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)

这是数据中对象的一个​​例子

{'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'},

但结果是地图显示时没有绘制任何形状。

1 个答案:

答案 0 :(得分:4)

Mercator应该是'mercator'

纬度和经度必须是列表:

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

以下是工作示例:

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)