Black Lives Matter. Please consider donating to Black Girls Code today.

Scatter Geo, change map

I’d like to do something like this example number 3 (Bubble Map with go.Scattergeo, United States Bubble Map). The problem is, I’m supposed to do it with a map of Italy. How can I do that?

Hi @Francesco.mttl,

To get the Italy map, set lataxis_range, lonaxis_range for Italy. This is a minimal code for a Scattergeo
that defines only the position of Milano:

fig = go.Figure()
fig.add_trace(go.Scattergeo(
                    lon = [9.189510],             
                    lat = [45.4642715],
                    text = 'Milano',
                    marker_color='red',
                    marker_size=8 ))
fig.update_layout(width = 675, height=900,
        title_text = 'Your title',title_x=0.5,
        showlegend = True,
        geo = dict(
            scope = 'europe',
            resolution = 50,
            lonaxis_range= [ 6.6, 18.4 ],
            lataxis_range= [35.47, 47.25],
            landcolor = 'rgb(217, 217, 217)',
        )
    )
fig.show()

You can tune the width and height of the plot to get the desired size of the map.

1 Like

thank you, if I want to upload a geojson map how can I do that?

@Francesco.mttl

Here is the code for reading the geojson file for Italy regions and plotting the corresponding map.
For other Italy’s geojson files see https://github.com/openpolis/geojson-italy/tree/master/geojson

import json
import urllib.request
import numpy as np
import plotly.graph_objects as go


#Load and read the geojson file for Italys regions. 

italy_url = "https://raw.githubusercontent.com/openpolis/geojson-italy/master/geojson/limits_IT_regions.geojson"
with urllib.request.urlopen(italy_url) as url:
        jdata = json.loads(url.read().decode())
              
pts = []#list of points defining boundaries of polygons
for  feature in jdata['features']:
    if feature['geometry']['type'] == 'Polygon':
        pts.extend(feature['geometry']['coordinates'][0])    
        pts.append([None, None])#mark the end of a polygon   
        
    elif feature['geometry']['type'] == 'MultiPolygon':
        for polyg in feature['geometry']['coordinates']:
            pts.extend(polyg[0])
            pts.append([None, None])#end of polygon
    elif feature['geometry']['type'] == 'LineString': 
        points.extend(feature['geometry']['coordinates'])
        points.append([None, None])
    else: pass           
    #else: raise ValueError("geometry type irrelevant for map")
x, y = zip(*pts)    

fig = go.Figure()
fig.add_scatter(x=x, y=y, mode='lines', line_color='#999999', line_width=1.5)
fig.update_layout(width=600,  height=800)
fig.show()

1 Like

Thank you. You’ve been very helpful.
Let me add one thing, is it possible at this point to color the regions with different colors according to a set of variables?

@Francesco.mttl

What you want to plot is called choropleth map, i.e. you can map each characteristic value for a region to a colorscale and color that region with the corresponding color.

Plotly provides the class go.Choroplethmapbox, for wich the basemap is a mapbox map. Here is how it works https://plot.ly/~empet/15238

But if you want to fill with a color each region defined by the code above, then this example https://plot.ly/~empet/15354 could be useful. You can skip the part that converts a shapefile to a geojson, because your map data are already in geojson format.

1 Like