Creating a choropleth chart Using plotly.graph_objects in Python to map data in Canada

541 Views Asked by At

I am using the plotly.graph_objects to map my data in Canda map. And, I am creating the fig as follows:

  fig = go.Figure(data=go.Choropleth(
 locations=df['state Abv'],
 z=df['total applications'].astype(float),
 locationmode='**UNKNOWN**',
 colorscale='Reds',
 autocolorscale=False,
 text=df['text'], 
 marker_line_color='white', 
 colorbar_title="Number of applications"))

Is there a way to set locattionmode as "Canada" to map the data on Canada map?

Thank you!

Ben

1

There are 1 best solutions below

1
jai On

You can use folium library to map countries easily.

For example to map a country, just write coordinates of that place in location parameter:(I have plotted for India)

import folium as fm
world_map = fm.Map(
    location = [20.770685, 73.7200303],
    zoom_start = 6)

And to add a popup and other features to it you can do:(I have added this popup for a place called onatrio.)

ontario = fm.map.FeatureGroup()

ontario.add_child(fm.CircleMarker([26.9127837,75.7411626],
        color = "red", radius = 5, fill_color = "Red")
        )

world_map.add_child(ontario)

fm.Marker([26.9127837,75.7411626], popup = 'ontario').add_to(world_map)
world_map.save('plot_data.html')

from IPython.display import HTML
HTML('<iframe src=plot_data.html width=700 height=450></iframe>')

This code is not for jupyter. If you are using jupyter then cut the last part of IPython libray from this code. I hope you got my instructions.

Happy coding!