语言:Python

我将此数据作为数据帧存储在名为:grouped_fips_pop的变量中

    FIPS    County  County Pop 2022 Formated_FIPS
0   53001   Adams   20961.0 0500000US53001
1   53003   Asotin  22508.0 0500000US53003
2   53005   Benton  212791.0    0500000US53005
3   53007   Chelan  79926.0 0500000US53007
4   53009   Clallam 77805.0 0500000US53009
5   53011   Clark   516779.0    0500000US53011
6   53013   Columbia    4026.0  0500000US53013
7   53015   Cowlitz 111956.0    0500000US53015
8   53017   Douglas 44192.0 0500000US53017
9   53019   Ferry   7448.0  0500000US53019
10  53021   Franklin    98678.0 0500000US53021
11  53023   Garfield    2363.0  0500000US53023
12  53025   Grant   101311.0    0500000US53025
13  53027   Grays Harbor    77038.0 0500000US53027
14  53029   Island  86625.0 0500000US53029
15  53031   Jefferson   33589.0 0500000US53031
16  53033   King    2266789.0   0500000US53033
17  53035   Kitsap  277673.0    0500000US53035
18  53037   Kittitas    45189.0 0500000US53037
19  53039   Klickitat   23271.0 0500000US53039
20  53041   Lewis   85370.0 0500000US53041
21  53043   Lincoln 11601.0 0500000US53043
22  53045   Mason   68166.0 0500000US53045
23  53047   Okanogan    43127.0 0500000US53047
24  53049   Pacific 24113.0 0500000US53049
25  53051   Pend Oreille    14179.0 0500000US53051
26  53053   Pierce  927380.0    0500000US53053
27  53055   San Juan    18662.0 0500000US53055
28  53057   Skagit  131179.0    0500000US53057
29  53059   Skamania    12460.0 0500000US53059
30  53061   Snohomish   840079.0    0500000US53061
31  53063   Spokane 549690.0    0500000US53063
32  53065   Stevens 48229.0 0500000US53065
33  53067   Thurston    298758.0    0500000US53067
34  53069   Wahkiakum   4688.0  0500000US53069
35  53071   Walla Walla 61890.0 0500000US53071
36  53073   Whatcom 230677.0    0500000US53073
37  53075   Whitman 47619.0 0500000US53075
38  53077   Yakima  257001.0    0500000US53077

我在下面运行了这个:

import plotly.express as px
import requests
import json
import pandas as pd

# Load county GeoJSON data
r = requests.get('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json')
counties = json.loads(r.text)

# Filter counties GeoJSON data to include only Washington state
target_states = ['53']
counties['features'] = [f for f in counties['features'] if f['properties']['STATE'] in target_states]

grouped_fips_pop['County Pop 2022'] = grouped_fips_pop['County Pop 2022'].astype(int)

# Create choropleth map for Washington state
fig = px.choropleth(grouped_fips_pop, 
                    geojson=counties, 
                    locations='FIPS', 
                    color='County Pop 2022',
                    color_continuous_scale='Viridis',
                    range_color=(grouped_fips_pop['County Pop 2022'].min(), grouped_fips_pop['County Pop 2022'].max()),
                    scope='usa',
                    labels={'County Pop 2022': 'Population'},
                    hover_name= 'County'
                   )



less_50K = grouped_fips_pop[grouped_fips_pop['County Pop 2022'] < 50000]
b50K_100K = grouped_fips_pop[(grouped_fips_pop['County Pop 2022'] >= 50000) & (grouped_fips_pop['County Pop 2022'] < 100000)]
high_100K = grouped_fips_pop[grouped_fips_pop['County Pop 2022'] >= 100000]

less_50K['County Pop 2022'] = less_50K['County Pop 2022'].astype(int)
b50K_100K['County Pop 2022'] = b50K_100K['County Pop 2022'].astype(int)
high_100K['County Pop 2022'] = high_100K['County Pop 2022'].astype(int)

# Update layout and display the map
fig.update_layout(title_text='2022 Population in Washington State',
                  title_x=0.5,  # Center the title horizontally
                  title_y=0.9,  # Position the title closer to the top
                  geo=dict(projection_scale=5, center={'lat': 47.5, 'lon': -120}, projection_type='albers usa'),  # Use Albers USA projection
                  margin={'r': 0, 't': 20, 'l': 0, 'b': 0},  # Reduce top margin to move title closer to the map
                  updatemenus=[
                      dict(
                          buttons=list([
                              dict(
                                  args=[{"z": [grouped_fips_pop['County Pop 2022']]}],
                                  label="All",
                                  method="restyle"
                              ),
                              dict(
                                  args=[{"z": [less_50K['County Pop 2022']]}],
                                  label="Less than 50,000",
                                  method="restyle"  
                              ),
                              dict(
                                  args=[{"z": [b50K_100K['County Pop 2022']]}],
                                  label="50,000 - 100,000",
                                  method="restyle"
                              ),
                              dict(
                                  args=[{"z": [high_100K['County Pop 2022']]}],
                                  label="More than 100,000",
                                  method="restyle"
                              )
                          ]),
                          direction="down",
                          pad={"r": 10, "t": 10},
                          showactive=True,
                          x=0.1,
                          xanchor="left",
                          y=1.1,
                          yanchor="top"
                      )
                  ]
                 )

fig.show()

它在 map 上画出了充满人口的县.当我 Select 了"切换"选项时,它会扰乱所有信息.例如:在所有选项上,县"国王"给我2M的人口,当我 Select 的选项小于50K人口,它显示我县"国王"突出显示,这不应该,因为它高于50K,而不是低于50K,它显示与人口4688.

我的问题是,我能做些什么来解决这个问题?我try 了一点冲刺,但没有成功.

推荐答案

map 显示不正确的原因是县几何与县人口不成一对一关系,因此显示不正确.所以我认为几何形状需要调整到这个群体,以匹配数据表中给出的人口数据.最简单的方法是将符合条件的数据提供给当前数据框,否则将该值赋为None,然后隐藏映射.

less_50K = grouped_fips_pop.copy()
less_50K['County Pop 2022'] = less_50K['County Pop 2022'].apply(lambda x: x if x < 50000 else None) 
b50K_100K = grouped_fips_pop.copy()
b50K_100K['County Pop 2022'] = b50K_100K['County Pop 2022'].apply(lambda x: x if x >= 50000 and x < 100000 else None) 
high_100K = grouped_fips_pop.copy()
high_100K['County Pop 2022'] = high_100K['County Pop 2022'].apply(lambda x: x if x >= 100000 else None) 

# Create choropleth map for Washington state
fig = px.choropleth(grouped_fips_pop, 
                    geojson=counties, 
                    locations='FIPS', 
                    color='County Pop 2022',
                    color_continuous_scale='Viridis',
                    range_color=(grouped_fips_pop['County Pop 2022'].min(), grouped_fips_pop['County Pop 2022'].max()),
                    scope='usa',
                    labels={'County Pop 2022': 'Population'},
                    hover_name= 'County'
                   )

# Update layout and display the map
fig.update_layout(title_text='2022 Population in Washington State',
                  title_x=0.5,  # Center the title horizontally
                  title_y=0.9,  # Position the title closer to the top
                  geo=dict(projection_scale=5, center={'lat': 47.5, 'lon': -120}, projection_type='albers usa'),  # Use Albers USA projection
                  margin={'r': 0, 't': 20, 'l': 0, 'b': 0},  # Reduce top margin to move title closer to the map
                  updatemenus=[
                      dict(
                          buttons=list([
                              dict(
                                  args=[{"z": [grouped_fips_pop['County Pop 2022']]}],
                                  label="All",
                                  method="restyle"
                              ),
                              dict(
                                  args=[{"z": [less_50K['County Pop 2022']]}],
                                  label="Less than 50,000",
                                  method="restyle"  
                              ),
                              dict(
                                  args=[{"z": [b50K_100K['County Pop 2022']]}],
                                  label="50,000 - 100,000",
                                  method="restyle"
                              ),
                              dict(
                                  args=[{"z": [high_100K['County Pop 2022']]}],
                                  label="More than 100,000",
                                  method="restyle"
                              )
                          ]),
                          direction="down",
                          pad={"r": 10, "t": 10},
                          showactive=True,
                          x=0.1,
                          xanchor="left",
                          y=1.1,
                          yanchor="top"
                      )
                  ]
                 )

fig.show()

enter image description here

enter image description hereenter image description here

Python相关问答推荐

更改matplotlib彩色条的字体并勾选标签?

Django管理面板显示字段最大长度而不是字段名称

Django mysql图标不适用于小 case

输出中带有南的亚麻神经网络

无法通过python-jira访问jira工作日志(log)中的 comments

如何获得每个组的时间戳差异?

Asyncio:如何从子进程中读取stdout?

为什么Django管理页面和我的页面的其他CSS文件和图片都找不到?'

isinstance()在使用dill.dump和dill.load后,对列表中包含的对象失败

基于Scipy插值法的三次样条系数

Cython无法识别Numpy类型

在用于Python的Bokeh包中设置按钮的样式

如何求相邻对序列中元素 Select 的最小代价

jsonschema日期格式

Python—在嵌套列表中添加相同索引的元素,然后计算平均值

类型对象';敌人';没有属性';损害';

多索引数据帧到标准索引DF

如何在Polars中创建条件增量列?

了解如何让库认识到我具有所需的依赖项

在Django REST框架中定义的URL获得404分