0

I plotted the following choropleth map using plotly. I want to show the Canadian provinces centered in the initial display. RIght know it's showing part of the USA and Canada.

Thank you in advance for any help provided!


import plotly.express as px

with open("canada_provinces.geojson", 'r') as f:
    geojson = json.load(f)


fig = px.choropleth(test_df, geojson=geojson, locations='prov', color="median_wage",  
                    color_continuous_scale="Viridis",
                    featureidkey= 'properties.name',
                    scope='north america')
fig.update_geos(fitbounds='locations', visible=True)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

This is the output:

enter image description here

and I would like something more like this:

enter image description here

Alejandro L
  • 147
  • 1
  • 9
  • Does this answer your question? [Is there a way to start a plot already zoomed on a specific area using plotly?](https://stackoverflow.com/questions/54863027/is-there-a-way-to-start-a-plot-already-zoomed-on-a-specific-area-using-plotly) – Andy Ray Jan 08 '23 at 06:33
  • 1
    You can set the center of the map, so you should specify the latitude and longitude of the center of Canada. Alternatively, you can get the latitude and longitude you wish to center the map on from a map service and set it to the center, and the intended location will be the center. `fig = px.choropleth(...,center={'lon':62.4, lat':-96.466667},...)` – r-beginners Jan 08 '23 at 09:47
  • Thanks but it doesn't center the map the way I want to – Alejandro L Jan 10 '23 at 18:56

1 Answers1

1

You could try this. To center the map on Canada and display only the Canadian provinces, you can use the center and projection parameters in the update_geos() method. The center parameter takes a list of latitude and longitude coordinates that correspond to the center of the map, and the projection parameter controls the projection of the map.

Do this:

fig.update_geos(center=[60, -95], projection="natural earth", visible=True)

You can also use the scope method to your advantage.

fig = px.choropleth(test_df, geojson=geojson, locations='prov', color="median_wage",  
                    color_continuous_scale="Viridis",
                    featureidkey= 'properties.name',
                    scope='canada')