简体   繁体   English

如何从 plotly express 在地图框上显示特定数据

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

I have linked the data and chosen the right parameters, but the map is empty.我已经链接了数据并选择了正确的参数,但地图是空的。 I have also looked at the documentation of https://plotly.com/python/mapbox-county-choropleth/ , but that doesn't help me.我还查看了https://plotly.com/python/mapbox-county-choropleth/的文档,但这对我没有帮助。

Here is the code I made:这是我制作的代码:

# 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")

And the dataset I used:我使用的数据集:

country国家 state状态 total_costs总成本 total_revenue总收入 total_profit总利润
Canada加拿大 Brittish Columbia不列颠哥伦比亚省 360.00 360.00 950.00 950.00 590.00 590.00
Australia澳大利亚 New South Wales新南威尔士 1,035.00 1,035.00 2,401.00 2,401.00 1366.00 1366.00
United States美国 Oregon俄勒冈州 405.00 405.00 929.00 929.00 524.00 524.00

I hope someone can help me我希望有一个人可以帮助我

  • your data preparation does not consider that country is not in the geojson as a property.您的数据准备不认为该国家/地区不在geojson 中作为属性。 Have used geopandas for prep and joining.已使用geopandas进行准备和加入。 NB - USA is lost as join keys are inconsistent NB - 美国丢失,因为加入密钥不一致
  • you should consider where you want to be the center and the zoom level你应该考虑你想成为中心的位置缩放级别
# 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