1

I'm lookin at the use map from the link below.

https://plotly.com/python/mapbox-county-choropleth/

Here is their code.

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_mapbox(df, geojson=counties, locations='fips', color='unemp',
                           color_continuous_scale="Viridis",
                           range_color=(0, 12),
                           mapbox_style="carto-positron",
                           zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
                           opacity=0.5,
                           labels={'unemp':'unemployment rate'}
                          )
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

Here is their results.

enter image description here

Here is my own code.

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

import plotly.express as px

fig = px.choropleth_mapbox(df_lat_lon, geojson=counties, locations='local_market', color='days_between_start_and_complete',
                           color_continuous_scale="Viridis",
                           range_color=(0, 12),
                           mapbox_style="carto-positron",
                           zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
                           opacity=0.5,
                           labels={'markets'}
                          )
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

Here is my DF:

Here is the data from my DF:

        index        local_market   latitude    longitude       days
        4753         Las Vegas      35.936682   -115.086450     134
        1628         El Paso        31.755722   -106.273967     2
        1625         Denver         39.925373   -105.115118     11
        6896         Phoenix        33.609486   -112.181292     11
        6897         Tucson         32.228861   -110.947664     9
        6898         Tucson         32.228861   -110.947664     9
        6899         Tucson         32.228861   -110.947664     9
        6900         Tucson         32.228861   -110.947664     9

My map is completely blank.

enter image description here

ASH
  • 20,759
  • 19
  • 87
  • 200
  • Please consider sharing your dataframe like [this](https://stackoverflow.com/questions/63163251/pandas-how-to-easily-share-a-sample-dataframe-using-df-to-dict/63163254#63163254) instead of like an image. – vestland Feb 02 '21 at 19:48
  • Their code has a first column `fips` why doesn’t yours? Isn’t that the county code that’s being coloured in their code? How are you hoping to colour counties in your code? – DisappointedByUnaccountableMod Feb 02 '21 at 19:54

2 Answers2

0

The colors, in your case calculated based on days_between_start_and_complete, are linked to the polygons via their feature id. In your geojson the feature id is a fips code, but the column you pass as locations holds city names.

Hence you should either pass a column with fips codes as locations, or alternatively use a geojson file where the features have city name as their id.

emher
  • 5,634
  • 1
  • 21
  • 32
0

Create a column of 'fips' codes associated with your county names in 'local_market' in your data table based on the values here: https://www.nrcs.usda.gov/wps/portal/nrcs/detail/national/home/?cid=nrcs143_013697 for example, Las Vegas is in Clark County with fips code of '05019':

index        local_market   fips     latitude     longitude      days
4753         Las Vegas     '05019'   35.936682   -115.086450     134
1628         El Paso       '48141'   31.755722   -106.273967     2

Then set the location as fips in your code. Also change the range 'range_color' to the range of the column you are plotting ('days' in this example is from 2 to 134):

fig = px.choropleth_mapbox(df_lat_lon, geojson=counties, locations='fips', color='days',
                       color_continuous_scale="Viridis",
                       range_color=(2, 134),
                       mapbox_style="carto-positron",
                       zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
                       opacity=0.5,
                       labels={'markets'}
                      )
Faraz
  • 67
  • 7