我正在创建一个交互式地理热图仪表板。以下图为例。第一个图是输出。如何勾勒出第二图中所示的多个状态?我将把美国州划分为10个分区,并创建一个下拉菜单以选择并显示特定的分区。
import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')
fig = go.Figure(data=go.Choropleth(
locations=df['code'], # Spatial coordinates
z = df['total exports'].astype(float), # Data to be color-coded
locationmode = 'USA-states', # set of locations match entries in `locations`
colorscale = 'Reds',
colorbar_title = "Millions USD",
))
fig.update_layout(
title_text = '2011 US Agriculture Exports by State',
geo_scope='usa', # limite map scope to USA
)
fig.show()
答案 0 :(得分:0)
使用这里的物品,可以做一些接近您需要的事情。我知道此解决方案并非您所要的,因为我的答案中显示了相关区域内状态之间的边界。但是,它最接近您要查找的内容,我可以想到该如何做而无需调用其他软件包或工具。希望它可以帮助您找到更完整的解决方案。
其背后的基本思想是,我要添加一个具有选定状态的图层,将其alpha设置为0,然后将状态边界设置为给定的颜色。我的想法是,可能有一种方法可以对位置数据使用颜色映射,以克服出现线条的问题
import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')
fig = go.Figure(data=go.Choropleth(
locations=df['code'], # Spatial coordinates
#locations=['VA'],
z = df['total exports'].astype(float), # Data to be color-coded
locationmode = 'USA-states', # set of locations match entries in `locations`
colorscale = 'Reds',
colorbar_title = "Millions USD",
marker_line_color='white', # will still work if you put this back in black
))
fig.update_layout(
title_text = '2011 US Agriculture Exports by State',
geo_scope='usa', # limit map scope to USA
)
# to add these outlines"
# add layer of states to highlight
# turn their texture alpha to 0
# set their marker_line_color to a nice Blue
# and turn off the scale
chorpleth = go.Choropleth(
locationmode='USA-states',
z=[0,0,0,0,0,0,0,0,0,0,0,0],
locations=['ND','SD','NE','KS','MO','IA','MN','WI','MI','IL','IN','OH'],
colorscale = [[0,'rgba(0, 0, 0, 0)'],[1,'rgba(0, 0, 0, 0)']],
marker_line_color='Blue',
showscale = False,
)
fig.add_trace(chorpleth)
fig.show()
我可以想到的其他解决方案包括使用cartopy,因此我将沿着这些思路进行研究,因为这显然是不完整的。