人口全部乱序 - -Python - Matplotlib - 映射

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

语言:Python

我将此数据作为 Datraframe 存储在名为:

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()

它绘制了地图,其中包含人口较多的县区。当我选择下拉选项时,它会扰乱所有信息。例如:在所有选项中,县“King”给了我 200 万人口,当我选择少于 50K 人口的选项时,它会向我显示突出显示的县“King”,但它不应该这样做,因为它高于 50K,而不是更少然后是 50k,显示人口为 4688。

我的问题是我能做些什么来解决这个问题?我尝试了一点破折号,但没有运气。

python matplotlib geojson
1个回答
0
投票

地图显示不正确的原因是县城几何形状与县城人口不是一一对应的关系,所以显示不正确。因此,我认为需要根据该组调整几何形状,以匹配下拉列表中给出的人口数据。最简单的方法是将符合条件的数据提供给当前数据框,否则提供该值 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()

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