如何概述多个状态?
How to outline multiple states?
我正在创建一个交互式地理热图仪表板。以下图为例。第一个图是输出。我如何勾勒出第二个图中所示的多个状态?我打算将美国各州划分为 10 个分区,并创建一个 select 的下拉菜单并显示具体的分区。
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,因此我将按照这些思路研究一些内容,因为这显然是部分的。
我正在创建一个交互式地理热图仪表板。以下图为例。第一个图是输出。我如何勾勒出第二个图中所示的多个状态?我打算将美国各州划分为 10 个分区,并创建一个 select 的下拉菜单并显示具体的分区。
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,因此我将按照这些思路研究一些内容,因为这显然是部分的。