基于自定义 .geojson 创建一个 Plotly 的分级色块图。

huangapple go评论65阅读模式
英文:

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()

基于自定义 .geojson 创建一个 Plotly 的分级色块图。

huangapple
  • 本文由 发表于 2023年5月7日 02:40:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定