Search code examples
pythonplotlychoropleth

Missing State in Choropleth Map?


I'm plotting this choropleth map of all the U.S. states using the country's abbreviation codes instead of FIPS codes and it is missing the state Missouri, but I have it included in my dataset here: https://raw.githubusercontent.com/naymalabonna/CSC474-Final-Project/main/Data/Jail%20Data/covid_prison_rates.csv

I tried this with 2 choropleth maps and same issue with Missouri missing.enter image description here

This is the code:

import pandas as pd
import plotly as py
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
import plotly.graph_objs as go
df = pd.read_csv('https://raw.githubusercontent.com/naymalabonna/CSC474-Final-Project/main/Data/Jail%20Data/covid_prison_rates.csv')
df.head()

data = dict(type ='choropleth',
            colorscale = 'electric',
            z = df['prisoner_cases_pct'],
            locations = df['name'],
            locationmode = 'USA-states',
            text = df['name'],
            marker = dict(line = dict(color = 'rgb(255, 255, 255)', width =1)),
            colorbar = {'title': 'Prisoner Cases Rates'})

layout = dict(title = 'Prisoner COVID-19 Cases Rates',
              geo = dict(scope ='usa'))

choromap = go.Figure(data = [data], layout = layout)
choromap.show()

Solution

  • The reason Missouri is not showing up is because the state abbreviations in the data contain unnecessary white space; I ran it through your data in express and confirmed it.

    import pandas as pd
    import plotly.express as px
    #from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
    import plotly.graph_objects as go
    
    df = pd.read_csv('https://raw.githubusercontent.com/naymalabonna/CSC474-Final-Project/main/Data/Jail%20Data/covid_prison_rates.csv')
    
    df.name.unique() #'MO ' is error
    array(['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'Federal', 'FL',
           'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD',
           'MA', 'MI', 'MN', 'MS', 'MO ', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM',
           'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'PR', 'SC', 'SD', 'TN',
           'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY', 'National'],
          dtype=object)
    
    df.loc[25,'name'] = 'MO'
    fig = px.choropleth(locations=df.name.unique().tolist(),
                        locationmode='USA-states',
                        color=df.name.unique().tolist(),
                        scope='usa')
    fig.show()
    

    enter image description here