Reputation: 20362
I'm using the code from this link.
https://devskrol.com/2021/12/27/choropleth-maps-using-python/
Here's my actual code.
import plotly.express as px
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 libraries
import pandas as pd
import plotly.express as px
fig = px.choropleth(df_mover, geojson=counties,
locations='my_zip',
locationmode="USA-states",
color='switcher_flag',
range_color=(10000, 100000),
scope="usa"
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
I'm simply trying to pass in data from my dataframe named df_movers, which has two fields: my_zip and switcher_flag. When I run this in a Jupyter notebook, it just runs and runs; it never stops. I'm only trying to plot 25 records, so it's not like there's too much data here. Finally, my_zip is data type object. Any idea what could be wrong here?
Upvotes: 4
Views: 1203
Reputation: 35275
Since you did not provide any user data, I tried your code with data including US zip codes from here. I think the issue is that you don't need to specify the location mode. I specified county_fips for the location and population for the color fill.
import plotly.express as px
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 libraries
import pandas as pd
us_zip = pd.read_csv('data/uszips.csv', dtype={'county_fips': str})
fig = px.choropleth(us_zip,
geojson=counties,
locations='county_fips',
#locationmode="USA-states",
color='population',
range_color=(1000, 10000),
scope="usa"
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
Upvotes: 3