@Amrith
For US the base map exhibits the states boundaries, while for any other country there is a workaround to get displayed these boundaries. But unfortunately you cannot remove the boundaries of neighboring countries from the base map.
Here is what I succeeded to get for India states:
import plotly.express as px
import plotly.graph_objects as go
import urllib.request
import json
import pandas as pd
import numpy as np
# the body of this function I posted 2 years ago here:
# http://localhost:8888/notebooks/Documents/AAAATopojson/India-geo-Choropleth.ipynb
def state_boundaries(geojdata):
pts = []#list of points defining boundaries of polygons, pts has as coordinates the lon and lat
for feature in geojdata['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")
lons, lats = zip(*pts)
return lons, lats
url ="https://raw.githubusercontent.com/Subhash9325/GeoJson-Data-of-Indian-States/master/Indian_States"
with urllib.request.urlopen(url) as url:
geojson = json.loads(url.read().decode())
#print(geojson.keys())
df = pd.DataFrame({"ids": np.arange(1, 36), "vals": np.random.randint(25,35, size=35)})
df1=df[df["vals"]>30] #Choropleth displays only states with vals >30
lons, lats = state_boundaries(geojson)
fig= go.Figure(go.Scattergeo(lon=lons, lat=lats, mode="lines", line_width=1.5, line_color="white"))
fig.update_layout(width=700, height=750,
geo = dict(
scope = 'asia',
resolution = 110, #for info help(go.layout.Geo.resolution)
lonaxis_range= [67, 98 ],
lataxis_range= [2, 38],
landcolor = 'rgb(225, 225, 225)',
))
#fig.show()
#define a reduced geojson file to avoid passing non-necessary data to choropleth
reduced_geojson={"type": "FeatureCollection", "features":[]}
for feat in geojson["features"]:
if feat["properties"]["ID_1"] in df1["ids"]:
reduced_geojson["features"].append(feat)
figc = px.choropleth(df1, geojson=reduced_geojson, locations='ids',
color="vals", color_continuous_scale="viridis",
featureidkey="properties.ID_1")
#fig1.update_traces(marker_line_color="black", marker_line_width=3) #color and width of boundary color for states displayed by coropleth
fig.add_trace(figc.data[0])
fig.show()
Notice that the resolution of the base map is not the same as the resolution from fig.layout.geo
Thatβs why parts of the Pakistan boundary is still visible, as well as the boundaries of eastern India neighbouring countries.