在Plotly的Scattergeo情节中制作传奇

时间:2017-03-13 20:36:23

标签: python matplotlib plot legend plotly

我正在绘制一些类似于第一个找到here示例的数据(美国机场地图)。然而,不是绘制一个标尺我正在绘制二进制特征(假设一种颜色超过15k飞行,一种颜色低于15k飞行)。我看过文档,但如果我想做这种情节,就找不到传说的方法。有谁知道怎么做?

1 个答案:

答案 0 :(得分:1)

您可以根据您的条件指定颜色,例如

color = np.where(df['Set'] > 15000, 'red', 'green')

但是你不会有一个很好的传奇。

另一种方法是添加两个图,每个条件一个。

enter image description here

import pandas as pd
import plotly

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

data = dict(
        type = 'scattergeo',
        locationmode = 'USA-states', 
        mode = 'markers'
        )

data_high = data.copy()
data_high['lon'] = df[df['cnt']  > 10000 ]['long']
data_high['lat'] = df[df['cnt']  > 10000 ]['lat']
data_high['marker'] = dict(color = 'red')
data_high['name'] = '> 10000'

data_low = data.copy()
data_low['lon'] = df[df['cnt']  <= 10000 ]['long']
data_low['lat'] = df[df['cnt']  <= 10000 ]['lat']
data_low['marker'] = dict(color = 'green')
data_low['name'] = '<= 10000'

layout = dict(
        geo = dict(
            scope = 'usa',
            projection = dict(type='albers usa'),
        ),
    )

fig = dict(data=[data_high, data_low], layout=layout)
plotly.offline.plot(fig)