简体   繁体   中英

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.

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 950.00 590.00
Australia New South Wales 1,035.00 2,401.00 1366.00
United States Oregon 405.00 929.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. Have used geopandas for prep and joining. NB - USA is lost as join keys are inconsistent
  • 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM