在美国等值区域地图上显示州边界,但不显示县边界

问题描述 投票:0回答:1

我有一些美国县级数据,我想要一张显示州边界但不显示县边界的分区统计图。按照这个解决方案,我最终得到了这个:

如您所见,州边界似乎分层在数据下方。我解决这个问题的尝试是制作第二个带有州边界和透明色标的分区统计图,并将其与数据分层在地图顶部。首先这是只有边框的图:

state_borders = px.choropleth(df,
   geojson=counties,
   locations='fips',
   color='increase_12yr',
   color_continuous_scale=["rgba(1,1,1,0)" ,"rgba(1,0,0,0)"],
   range_color=(0, 350),
    center = {"lat": 37.0902, "lon": -95.7129},
    scope='usa',
    basemap_visible=False
)
state_borders.update_traces(marker_line_width=0, marker_opacity=1)
state_borders.update_geos(
showsubunits=True, subunitcolor="black"
state_borders.show()
)

到目前为止一切顺利,但是当我尝试将其作为跟踪添加到原始地图时,我最终得到以下结果:

这是完整的代码,减去我的数据特定内容:

with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response)

fig = px.choropleth(df,
   geojson=counties,
   locations='fips',
   color='increase_12yr',
   color_continuous_scale="Reds",
   range_color=(0, 350),
    center = {"lat": 37.0902, "lon": -95.7129},
    scope='usa',
    basemap_visible=False
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

state_borders = px.choropleth(df,
   geojson=counties,
   locations='fips',
   color='increase_12yr',
   color_continuous_scale=["rgba(1,1,1,0)" ,"rgba(1,0,0,0)"],
   range_color=(0, 350),
    center = {"lat": 37.0902, "lon": -95.7129},
    scope='usa',
    basemap_visible=False
)
state_borders.update_traces(marker_line_width=0, marker_opacity=1)
state_borders.update_geos(showsubunits=True, subunitcolor="black")

fig.add_trace(state_borders.data[0])

fig.show()

那么有人知道如何将州边界分层在我的数据之上吗?

python pandas plotly choropleth
1个回答
0
投票

原始答案未显示,因为县边界设置为 0。您可以将线条粗细设置为 1 或更大,并指定线条颜色。我已将边界颜色设置为白色。

from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response)

import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
                   dtype={"fips": str})

import plotly.express as px

fig = px.choropleth(df, geojson=counties, locations='fips', color='unemp',
                           color_continuous_scale="Viridis",
                           range_color=(0, 12),
                           scope="usa",
                           labels={'unemp':'unemployment rate'},                 
                          )
fig.update_traces(marker_line_width=1, marker_line_color='white', marker_opacity=0.8)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.update_geos(
showsubunits=True, subunitcolor="black"
)
fig.show()

© www.soinside.com 2019 - 2024. All rights reserved.