import plotly.figure_factory as ff
import numpy as np
import pandas as pd
df_sample = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/laucnty16.csv')
df_sample['State FIPS Code'] = df_sample['State FIPS Code'].apply(lambda x: str(x).zfill(2))
df_sample['County FIPS Code'] = df_sample['County FIPS Code'].apply(lambda x: str(x).zfill(3))
df_sample['FIPS'] = df_sample['State FIPS Code'] + df_sample['County FIPS Code']
colorscale = ["#f7fbff", "#ebf3fb", "#deebf7", "#d2e3f3", "#c6dbef", "#b3d2e9", "#9ecae1",
"#85bcdb", "#6baed6", "#57a0ce", "#4292c6", "#3082be", "#2171b5", "#1361a9",
"#08519c", "#0b4083", "#08306b"
]
endpts = list(np.linspace(1, 12, len(colorscale) - 1))
fips = df_sample['FIPS'].tolist()
values = df_sample['Unemployment Rate (%)'].tolist()
fig = ff.create_choropleth(
fips=fips, values=values, scope=['usa'],
binning_endpoints=endpts, colorscale=colorscale,
show_state_data=False,
show_hover=True,
asp = 2.9,
title_text = 'USA by Unemployment %',
legend_title = '% unemployed'
)
fig.layout.template = None
fig.show()
你知道如何给这段代码添加标签吗?我需要的fips的细节,而悬停在Map上。
由于代码中使用了列表,不知道如何包含label和hover。
1条答案
按热度按时间enxuqcxy1#
figure factory
是过去的库,目前不推荐使用。根据参考中的示例创建所需的Map。首先,您需要geojson,我们将为县填充引入它。就业人数和非就业人数被添加为基本Map的悬停数据。作为标签的一个例子,您可以使用go.Scattergeo的文本模式将县名作为注解添加。Map仅限于加州,以便更好地理解标签。