英文:
Creating a plotly choropleth based on a custom .geojson
问题
I can't figure out what I am doing wrong here. I already tried to alter the code to my .geojson but it seems like I am doing something wrong.
geojson file is:
https://raw.githubusercontent.com/JohnSmith1790/bigdata/main/landkreise_simplify0.geo.json
import json
import requests
import pandas as pd
import numpy as np
import plotly.express as px
polygons = requests.get("https://raw.githubusercontent.com/JohnSmith1790/bigdata/main/landkreise_simplify0.geo.json").json()
df = pd.DataFrame(
# fips stands here for the IDs of the districts in the plot table
{"fips": range(1, 403, 1), "unemp": np.random.uniform(0.0, 501, 402)}
)
fig = px.choropleth(
df,
geojson=polygons,
locations="fips",
featureidkey="properties.AGS",
color="unemp",
color_continuous_scale="viridis",
range_color=(0, 501),
# scope="europe",
labels={"unemp": "Gesamtscore"}
)
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.update_geos(fitbounds="locations", visible=True)
fig.show()
(Note: I have removed the HTML-encoded characters for clarity.)
英文:
I can't figure out what I am doing wrong here. I already tried to alter the code to my .geojson but it seems like i am doing something wrong.
geojson file is:
https://raw.githubusercontent.com/JohnSmith1790/bigdata/main/landkreise_simplify0.geo.json
import json
import requests
import pandas as pd
import numpy as np
import plotly.express as px
polygons = requests.get("https://raw.githubusercontent.com/JohnSmith1790/bigdata/main/landkreise_simplify0.geo.json").json()
df = pd.DataFrame(
#fips steht hier stand jetzt für die ID's der Landkreise in der Plot-Tabelle
{"fips": range(1, 403, 1), "unemp": np.random.uniform(0.0, 501, 402)}
)
fig = px.choropleth(
df,
geojson=polygons,
locations="fips",
featureidkey="properties.AGS",
color="unemp",
color_continuous_scale="viridis",
range_color=(0, 501),
# scope="europe",
labels={"unemp": "Gesamtscore"}
)
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.update_geos(fitbounds="locations", visible=True)
fig.show()
答案1
得分: 0
以下是您要翻译的代码部分:
The AGS in the updated geojson is a string, but the user data is numeric. This is the reason why it is not displayed. By replacing the fips in the user data with AGS obtained from geojson, the map will be displayed. The user data needs to be consistent with the attribute data in geojson.
import json
import requests
import pandas as pd
import numpy as np
import plotly.express as px
polygons = requests.get("https://raw.githubusercontent.com/JohnSmith1790/bigdata/main/landkreise_simplify0.geo.json").json()
ags = []
for i in range(len(polygons['features'])):
ags_name = polygons['features'][i]['properties']['AGS']
ags.append(ags_name)
df = pd.DataFrame(
{"fips": ags, "unemp": np.random.uniform(0.0, 501, len(ags))}
)
fig = px.choropleth(
df,
geojson=polygons,
locations="fips",
featureidkey="properties.AGS",
color="unemp",
color_continuous_scale="viridis",
range_color=(0, 501),
labels={"unemp": "Gesamtscore"}
)
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.update_geos(fitbounds="locations", visible=True)
fig.show()
希望这有助于您的项目!
英文:
The AGS in the updated geojson is a string, but the user data is numeric. This is the reason why it is not displayed. By replacing the fips in the user data with AGS obtained from geojson, the map will be displayed. The user data needs to be consistent with the attribute data in geojson.
import json
import requests
import pandas as pd
import numpy as np
import plotly.express as px
polygons = requests.get("https://raw.githubusercontent.com/JohnSmith1790/bigdata/main/landkreise_simplify0.geo.json").json()
ags = []
for i in range(len(polygons['features'])):
ags_name = polygons['features'][i]['properties']['AGS']
ags.append(ags_name)
df = pd.DataFrame(
#fips steht hier stand jetzt für die ID's der Landkreise in der Plot-Tabelle
{"fips": ags, "unemp": np.random.uniform(0.0, 501, len(ags))}
)
fig = px.choropleth(
df,
geojson=polygons,
locations="fips",
featureidkey="properties.AGS",
color="unemp",
color_continuous_scale="viridis",
range_color=(0, 501),
# scope="europe",
labels={"unemp": "Gesamtscore"}
)
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.update_geos(fitbounds="locations", visible=True)
fig.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论