簡體   English   中英

為什么我的 choropleth map 顏色編碼不是所有 zip 代碼?

[英]Why isn't my choropleth map color coding all zip codes?

我嘗試將丹麥的 zip 代碼區域顏色編碼為三個代碼,具體取決於城市與最大城市之間的距離以確定價格范圍。 我通過將 GEOJSON 文件中 zip 代碼中的坐標從單獨的多邊形“合並”為多多邊形來修復了很多非彩色區域,但我無法使其適用於這兩個區域。 有小費嗎? :-)

丹麥 Choropleth 地圖

# Import 
from plotly import graph_objects as go
import plotly.express as px
import pandas as pd
import json

# Load data
with open("map.geojson") as f:
    geo_data = json.load(f)

df = pd.read_csv("postnr.csv",
                   dtype={"fips": str})

# Create figure object
fig = go.Figure(
    go.Choroplethmapbox(
        geojson = geo_data,
        featureidkey = "properties.POSTNR_TXT", #Assign feature key from geojson
        locations =df["postnr"], #Assign location data
        z = df["zone"], #Assign information data
        zauto = True,
        colorscale = 'viridis',
        showscale = True,
   )
)

# Update layout
fig.update_layout(
    mapbox_style = "carto-positron", #Decide a style for the map
    mapbox_zoom = 5, #Zoom in scale
    mapbox_center = {"lat": 56.26392, "lon": 9.501785}, #Center location of the map
    width=500,
    height=500,
)
import pandas as pd
import geopandas as gpd
import shapely.geometry
import requests, io
import plotly.graph_objects as go

# get regions
gdf = gpd.read_file(
    "https://raw.githubusercontent.com/ok-dk/dagi/aa7d2c48233ba5ec5edb7ab69c6847733ec1da42/geojson/kommuner.geojson"
)
# compress geometries
gdf.groupby("KOMKODE", as_index=False).agg(
    {
        "KOMNAVN": "first",
        "geometry": lambda g: g
        if len(g) == 1
        else shapely.geometry.MultiPolygon(g.values),
    }
)

# get 3 biggest cities
x = requests.get(
    "https://www.tageo.com/index-e-da-cities-DK.htm", headers={"User-Agent": "XY"}
).text
df = [df for df in pd.read_html(io.StringIO(x)) if len(df) > 5][0]
df.columns = df.loc[0]
df = df.drop(0).head(3)

# find closest city to a region
gdf2 = gdf.sjoin_nearest(
    gpd.GeoDataFrame(
        geometry=gpd.points_from_xy(
            df["Longitude (DD)"], df["Latitude (DD)"], crs="epsg:4326"
        ),
        data=df,
    )
)


# Create figure object
fig = go.Figure(
    go.Choroplethmapbox(
        geojson=gdf2["geometry"].__geo_interface__,
        # featureidkey = "properties.POSTNR_TXT", #Assign feature key from geojson
        # locations =df["postnr"], #Assign location data
        locations=gdf2.index,
        z=gdf2["Rank"],  # Assign information data
        zauto=True,
        colorscale="viridis",
        showscale=True,
    )
)

# Update layout
fig.update_layout(
    mapbox_style="carto-positron",  # Decide a style for the map
    mapbox_zoom=5,  # Zoom in scale
    mapbox_center={"lat": 56.26392, "lon": 9.501785},  # Center location of the map
    width=500,
    height=500,
)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM