繁体   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