如何勾勒多个州的轮廓?
问题描述
我正在创建一个交互式地理热图仪表板。以下面的数字为例。第一个地块是输出。如图2所示,我如何勾勒出多个州的轮廓?我将把美国州划分为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()
解决方案
您可以使用这里的资源做一些与您需要的事情相近的事情。我认识到,这一解决方案并不完全是你正在寻找的,因为我的答复中显示了有关区域内各州之间的边界。然而,这是我能想到的最接近于您所寻找的东西,而无需调用其他包或工具的方法。希望它能帮助您找到更完整的解决方案。
其基本思想是添加一个具有选定状态的层,将它们的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,所以我将沿着这些思路来研究一些东西,因为这无疑是部分的。
相关文章