Search code examples
pythonmatplotlibgeojson

Population All Scrambled - -Python - Matplotlib - Mapping


Language: Python

I have this data stored as a Datraframe in a variable called: 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

I ran this below:

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

And it plots the map with the county areas filled with population. When I select the dropdown options it scrambles all info. For example: On all option the county 'King' gives me 2M in population, when i select the option less then 50K Population, it shows me the county 'King' highlighted, which it shouldn't because its higher then 50K and not lesser then 50k, and it shows with a population of 4688.

My question is what can i do to resolve this? I tried a little of dash but without luck.


Solution

  • The reason the map does not display correctly is that the county geometry is not in a one-to-one relationship with the county population, so the display is incorrect. So I think the geometry needs to be adjusted to that group to match the population data given in the dropdown. The easiest way to do this is to give the data that matches the criteria to the current data frame, give that value None otherwise, and hide the map.

    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