'plotly Choropleth Map gets stuck loading when changing colour=

I am trying to create a plotly choropleth map of the uk local authorities, using predicted autism prevalence. The script is getting stuck infinitely loading when I try and assign "colour=predictedprevalence" so that the choropleth shows predicted autism rates from the dataset in each authority area. I am unsure what I need to do. I have attached an image of where the script hangs shows where the script hangs

import pandas as pd
import json
from urllib.request import urlopen
import numpy as np
#With Plotly
import plotly.express as px
from geojson_rewind import rewind

with urlopen('https://raw.githubusercontent.com/plummy95/1312306/main/localauth.json') as response:
    counties = json.load(response)

df=pd.read_csv('https://raw.githubusercontent.com/plummy95/1312306/main/localauth.json')

counties_corrected=rewind(counties,rfc7946=False)

fig = px.choropleth(df, geojson=counties_corrected, locations='nuts318cd', featureidkey="properties.nuts318cd", color='predictedprevalence',
                            color_continuous_scale="PurPor", labels={'label name':'label name'}, title='MAP TITLE',
                            scope="europe")

fig.update_geos(fitbounds="locations", visible=False)

This is what my dataset (lauth2020.csv) looks like. There are 311 entries:

dataset

I have been a wee stuck for a few days with this and would really appreciate some assistance. Thank you



Solution 1:[1]

Are you sure you are not specifying the user data incorrectly, it should be in csv format, but you are reading JSON format. I datamined your CSV data image and ran the code. The graph appears to be displayed correctly.

import pandas as pd
import json
from urllib.request import urlopen
import numpy as np
#With Plotly
import plotly.express as px
from geojson_rewind import rewind

with urlopen('https://raw.githubusercontent.com/plummy95/1312306/main/localauth.json') as response:
    counties = json.load(response)

counties_corrected=rewind(counties,rfc7946=False)

import io

data = '''
nuts318cd "local authority" year predictedprevalence latitude longitude
UKC11 Adur 2020 357 50.84572 -0.32417 
UKC12 Allerdale 2020 551 54.68524 -3.2809
UKC13 "Amber Valley" 2020 746 53.02884 -1.46219
UKC14 Arun 2020 845 50.84321 -0.64999
UKC21 Ashfield 2020 761 53.09747 -1.25422
UKC22 Ashford 2020 742 51.13096 0.823374
UKC23 Babergh 2020 496 52.0645 0.916149 
UKD11 "Barking and Dagenham" 2020 1307 51.54555 0.129479
UKD12 Barnet 2020 2476 51.61107 -0.21819 
UKD33 Barnsley 2020 1470 53.52577 -1.54925 
UKD34 "Barrow-in-Furness" 2020 390 54.15731 -3.1999 
UKD35 Basildon 2020 1091 51.59036 0.475055 
UKD36 "Basingstoke and Deane" 2020 1055 51.25937 -1.22021 
UKD37 Bassetlaw 2020 686 53.35604 -0.9787 
UKD41 "Bath and North East Somerset" 2020 1226 51.35604 -2.48654
UKD42 Bedford 2020 1011 52.19628 -0.45463 
UKD44 Bexley 2020 1475 51.45822 0.146212 
UKD45 Birmingham 2020 7088 52.48404 -1.88141 
UKD46 Blaby 2020 595 52.57706 -1.19887 
UKD47 "Blackburn with Darwen" 2020 899 53.7008 -2.4636
UKD61 Blackpool 2020 821 53.82164 -3.02199 
UKD62 Bolsover 2020 485 53.23875 -1.27228 
UKD63 Bolton 2020 1682 53.58449 -2.47952 
UKD71 Boston 2020 419 52.97794 -0.11218 
UKD72 "Bournemouth, Christchurch and Poole" 2020 2381 50.74609 -1.84807
UKD73 "Bracknell Forest" 2020 755 51.4113 -0.73363 
UKD74 Bradford 2020 3145 53.84382 -1.87389 
UKE11 Braintree 2020 871 51.91634 0.575911 
UKE12 Breckland 2020 790 52.59421 0.818716 
UKE13 Brent 2020 2216 51.56438 -0.27568 
UKE21 Brentwood 2020 432 51.64108 0.290091 
UKE22 "Brighton and Hove" 2020 2070 50.8465 -0.15079 
'''
df = pd.read_csv(io.StringIO(data), delim_whitespace=True)

fig = px.choropleth(df, 
                    geojson=counties_corrected,
                    locations='nuts318cd',
                    featureidkey="properties.nuts318cd",
                    projection='mercator',
                    color='predictedprevalence',
                    color_continuous_scale="PurPor",
                    labels={'label name':'label name'},
                    title='MAP TITLE',
                    scope="europe"
                   )

fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(autosize=False,
                  width=800,
                  height=600,
                  margin={"r":0,"t":20,"l":0,"b":0})
fig.show()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 r-beginners