我使用Python / Plotly构建了一个非常简单的Choropleth映射,该映射使用状态缩写作为位置。我正在尝试向地图添加注释,但是我可以找到的所有教程都使用x / y位置或lat / long位置。
理想情况下,我想使注释“填充”一个类似于单击事件的状态(as suggested in the second answer here-这是使用JS的一种替代方式,因为我还没有学到),但是似乎像解决方案将在状态上添加大注释。
所以我的问题是,有没有办法在不使用经/纬度的情况下将注释位置链接到状态位置?还是我必须为每个州创建纬度/经度?
这是我的代码:
import pandas as pd
import plotly.plotly as py
d = {'state': ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY",
"LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND",
"OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"],
'change': [-0.01, -0.17, -0.03, -0.02, -0.08, -0.03, -0.01, -0.17, -0.07, -0.11, -0.16, 0.20, -0.05, -0.07, -0.08,
-0.02, -0.04, -0.12, -0.03, -0.14, -0.01, -0.05, 0.09, -0.04, -0.13, 0.01, -1.75, -0.13, -0.01, -0.08,
-0.59, -0.05, -0.02, 0.45, 0.02, -0.05, -0.17, 0.11, -0.12, -0.07, 0.75, 0.01, -0.12, -0.22, 0.32,
-0.02, -0.03, 0.00, -0.18, -0.17]}
df = pd.DataFrame(data=d)
scl = [0.0, "rgb(242,240,247)"], [0.2, "rgb(218,218,235)"], [0.4, "rgb(188,189,220)"],
[0.6, "rgb(158,154,200)"], [0.8, "rgb(117,107,177)"], [1.0, "rgb(84,39,143)],
# df['text'] = df['state']
data = [dict(
type='choropleth',
colorscale=scl,
autocolorscale=False,
locations=df['state'],
z=df['change'].astype(float),
locationmode='USA-states',
# text=df['text'],
marker=dict(
line=dict(
color='rgb(255,255,255)',
width=2
)),
colorbar=dict(
title="Change in" + "<br>" + "Crime Rate")
)]
layout = dict(
title='Change in Juvenile Crime Rates, 2016-2017',
geo=dict(
scope='usa',
projection=dict(type='albers usa'),
showlakes=True,
lakecolor='rgb(255,255,255)'),
)
fig = dict(data=data, layout=layout)
py.plot(fig, filename='jj-crimerate-map')