簡體   English   中英

如何從 plotly express 在地圖框上顯示特定數據

[英]How can I show specific data on a mapbox from plotly express

我已經鏈接了數據並選擇了正確的參數,但地圖是空的。 我還查看了https://plotly.com/python/mapbox-county-choropleth/的文檔,但這對我沒有幫助。

這是我制作的代碼:

# IMPORTEREN VAN LIBARIES
import plotly.express as px
import pandas as pd
import plotly 
import plotly.offline as po
from urllib.request import urlopen
import json

# LEZEN VAN DATASET
fietsdata = pd.read_excel('Fietsdata.xlsx')

with urlopen('http://larscuny.info/projecten/dip/data/countries.json') as response:
    countries = json.load(response)

fig = px.choropleth_mapbox(
    fietsdata, 
    geojson=countries, 
    locations='country', 
    color='total_profit',
    color_continuous_scale="Viridis",
    mapbox_style="open-street-map", # carto-positron
)

fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

# LAYOUT VAN DE VISUALISATIE
fig.update_layout(
    font_family="Poppins",
    font_color="#002072",
    title_font_family="Poppins",
    title_font_color="#002072",
    legend_title="Gender",
    legend_title_font_color="#002072",
)

# OPEN VISUALISATIE
po.plot(fig, filename="total-revenue.html")

我使用的數據集:

國家 狀態 總成本 總收入 總利潤
加拿大 不列顛哥倫比亞省 360.00 950.00 590.00
澳大利亞 新南威爾士 1,035.00 2,401.00 1366.00
美國 俄勒岡州 405.00 929.00 524.00

我希望有一個人可以幫助我

  • 您的數據准備不認為該國家/地區不在geojson 中作為屬性。 已使用geopandas進行准備和加入。 NB - 美國丟失,因為加入密鑰不一致
  • 你應該考慮你想成為中心的位置縮放級別
# IMPORTEREN VAN LIBARIES
import plotly.express as px
import pandas as pd
import geopandas as gpd
import plotly 
import plotly.offline as po
from urllib.request import urlopen
import json

# LEZEN VAN DATASET
# fietsdata = pd.read_excel('Fietsdata.xlsx')
fietsdata =pd.DataFrame({'country': ['Canada', 'Australia', 'United States'],
 'state': ['Brittish Columbia', 'New South Wales', 'Oregon'],
 'total_costs': ['360.00', '1,035.00', '405.00'],
 'total_revenue': ['950.00', '2,401.00', '929.00'],
 'total_profit': [590.0, 1366.0, 524.0]})


with urlopen('http://larscuny.info/projecten/dip/data/countries.json') as response:
    countries = json.load(response)

# use geopandas and join data together
gdf = gpd.GeoDataFrame.from_features(countries)
gdf = gdf.merge(fietsdata, left_on="ADMIN", right_on="country")
    
    
fig = px.choropleth_mapbox(
    gdf, 
    geojson=gdf.geometry, 
    locations=gdf.index, 
    color='total_profit',
    color_continuous_scale="Viridis",
    center={"lat": gdf.iloc[0].geometry.centroid.y, "lon": gdf.iloc[0].geometry.centroid.x},
    zoom=1,
    mapbox_style="open-street-map", # carto-positron
)

fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

# LAYOUT VAN DE VISUALISATIE
fig.update_layout(
    font_family="Poppins",
    font_color="#002072",
    title_font_family="Poppins",
    title_font_color="#002072",
    legend_title="Gender",
    legend_title_font_color="#002072",
)

# OPEN VISUALISATIE
fig

暫無
暫無

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

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