簡體   English   中英

Choropleth map 顯示白頁而不是 map

[英]Choropleth map showing white page instead of the map

我正在處理等值線 map,它顯示的是白頁而不是 map,如此處所示https://i.stack.imgur.com/boYKY.png

我在同一個文件夾中下載了 geojson 和 excel 文件。

geojson https://drive.google.com/file/d/1N-rp9yHqE1Rzn2VxoAAweJ8-5XIjk61j/view?usp=sharing excel https://docs.google.com/spreadsheets/d/1NKeUg20XxJe0jccMgjj9pMxrTIIWeuQk/edit?usp=sharing&ouid=100050178655652050254&rtpof =真&sd=真

這是我的代碼

import json
import numpy as np
import pandas as pd
import plotly.express as px

df = pd.read_excel('kraje.xlsx', sheet_name='List1')

regions_json = json.load(open("KRAJE.geojson", "r"))

fig = px.choropleth(df,
             locations="K_KRAJ",
             geojson=regions_json,
             color='OB1506')
fig.show()

我正在查看 map 的瀏覽器控制台顯示了這個

我在 brave 瀏覽器中使用 jupyter notebook。

誰能幫我解決這個問題? 謝謝

編輯:

我找到了正確的 geojson 文件,但現在我遇到了不同的問題。 只有一個區域是彩色的,甚至沒有正確的顏色,map 的 rest 甚至在我的區域之外也是相同的顏色。 當我 hover 在我的區域上時,我可以看到它們在正確的位置但顏色錯誤。 而且我也不知道為什么代碼為整個 map 而不僅僅是 geojson 文件中的區域着色。 這是 output 的圖片

新的(應該是正確的)geojson https://drive.google.com/file/d/1S03NX5Q0pqgAsbJnjqt8O5w8gUHH1rt_/view?usp=sharing

import json
import numpy as np
import pandas as pd
import plotly.express as px

df = pd.read_excel('kraje.xlsx', sheet_name='List1')

regions_json = json.load(open("KRAJE.geojson", "r"))
for feature in regions_json['features']:
    feature["id"] = feature["properties"]["K_KRAJ"]

fig = px.choropleth(df,
             locations="K_KRAJ",
             geojson=regions_json,
             color='OB1506')
fig.update_geos(fitbounds="locations", visible=False)
fig.show()

解決方案

多虧了 Rob Raymond,它終於成功了。 geojson 文件存在問題。 我在安裝 geopandas 時也遇到了很多問題,唯一有效的教程是分別安裝每個 package ( https://stackoverflow.com/a/69210111/17646343 )

  • 您的 geojson 存在多個問題
    1. 需要定義 CRS,它顯然不是 epsg:4326。 似乎是捷克共和國的 UTM CRS
    2. 即使這樣也有無效的多邊形
  • 使用有效的 geojson,您錯過了幾點
    • 位置需要在您的數據框和 geojson 中通用
    • 需要使用featureidkey來定義您要加入的名稱
import json
import numpy as np
import pandas as pd
import plotly.express as px
import geopandas as gpd

files = {
    f.suffix: f
    for p in ["KRAJE*.*", "KRAJE*.*".lower()]
    for f in Path.home().joinpath("Downloads").glob(p)
}
# df = pd.read_excel('kraje.xlsx', sheet_name='List1')
df = pd.read_excel(files[".xlsx"], sheet_name="List1")

# regions_json = json.load(open("KRAJE.geojson", "r"))
regions_json = json.load(open(files[".geojson"], "r"))
regions_json = (
    gpd.read_file(files[".geojson"])
    .dropna()
    .set_crs("EPSG:32633", allow_override=True)
    .to_crs("epsg:4326")
    .__geo_interface__
)

fig = px.choropleth(
    df,
    locations="N_KRAJ",
    featureidkey="properties.name",
    geojson=regions_json,
    color="OB1506",
)
fig.update_geos(fitbounds="locations", visible=True)
fig

更新

  • 您的geojson仍然存在問題。 已使用geopandasbuffer(0)修復它(請參閱Fix invalid polygon in Shapely
  • 有了這個並更改為plotly參數我現在可以生成一個數字
import json
import numpy as np
import pandas as pd
import plotly.express as px
import geopandas as gpd
from pathlib import Path

files = {
    f.suffix: f
    for p in ["KRAJ_*.*", "KRAJE*.*".lower()]
    for f in Path.home().joinpath("Downloads").glob(p)
}
# df = pd.read_excel('kraje.xlsx', sheet_name='List1')
df = pd.read_excel(files[".xlsx"], sheet_name="List1")

# regions_json = json.load(open("KRAJE.geojson", "r"))
regions_json = json.load(open(files[".json"], "r"))
# geometry is still invalid!!! force it to valid by buffer(0)
regions_json = gpd.read_file(files[".json"]).assign(geometry=lambda d: d["geometry"].buffer(0)).__geo_interface__

fig = px.choropleth(
    df,
    locations="K_KRAJ",
    featureidkey="properties.K_KRAJ",
    geojson=regions_json,
    color="OB1506",
)
fig.update_geos(fitbounds="locations", visible=True)
fig

在此處輸入圖像描述

暫無
暫無

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

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