How to add additional countries to a map?

Hi everyone,

I’m working on a Plotly map that currently focuses on Africa and the Middle East. The map shows data for different countries, with bubble sizes proportional to the total count. I’d like to expand the map to include other countries from the Middle East that happen to be in Asia (UAE, Pakistan, etc), but I’m not sure how to adjust the scope or other settings to do this. Any help? I’m using a scatter_geo plot.

This is what i currently have:

import openpyxl
import pandas as pd
import plotly.express as px

# Load the Excel file
file_path = "your_file.xlsx"  # Replace with the path to your Excel file
wb = openpyxl.load_workbook(file_path)
sheet = wb.active

# Extract data from the Excel sheet
data = {"Country": [], "TPM": [], "RE": [], "NPI": [], "SA": [], "LM": []}

for row in sheet.iter_rows(min_row=2, values_only=True):  # Assuming first row is header
    data["Country"].append(row[0])
    data["TPM"].append(row[1] if row[1] is not None else 0)
    data["RE"].append(row[2] if row[2] is not None else 0)
    data["NPI"].append(row[3] if row[3] is not None else 0)
    data["SA"].append(row[4] if row[4] is not None else 0)
    data["LM"].append(row[5] if row[5] is not None else 0)

# Convert to DataFrame
df = pd.DataFrame(data)

# Calculate the Total Headcount
df["Total"] = df[["TPM", "RE", "NPI", "SA", "LM"]].sum(axis=1)

# Create the Geomap with customized hover data
fig = px.scatter_geo(
    df,
    locations="Country",
    locationmode="country names",
    size="Total",
    title="Headcount per Country",
    projection="natural earth",
    size_max=50,
    hover_name="Country",
    hover_data={
        "TPM": True,
        "RE": True,
        "NPI": True,
        "SA": True,
        "LM": True,
        "Total": True,
    },
)

fig.update_layout(
    title_text="Headcounts<br>(Bubble size proportional to total headcount)",
    showlegend=False,
    geo=dict(
        showframe=False,
        showcoastlines=False,
        projection_type="mercator",
        resolution=50,
        scope="africa",  # Current scope is set to Africa; how to include other regions?
        landcolor="rgb(217, 217, 217)",
        bgcolor="white",
        countrycolor="white",
        coastlinecolor="white",
    ),
)

fig.show()

edit: What i’m trying to accomplish is getting the map to look like this (with the bubbles of course):

Specify a range of latitude and longitude as the graph range setting, without specifying a scope. I have optimized your code using the data in the reference since your data has not been submitted. The latitude and longitude ranges are yours to adjust.

import plotly.express as px
df = px.data.gapminder().query("year==2007").query("continent=='Africa'|continent=='Asia'")

import plotly.graph_objects as go

fig = px.scatter_geo(df,
                     locations="iso_alpha",
                     color="continent",
                     hover_name="country",
                     size="pop",
                     size_max=100,
                     projection="natural earth"
                    )

fig.update_layout(
    geo = dict(
        showframe=False,
        showcoastlines=False,
        showcountries=True,
        projection_type="mercator",
        resolution=50,
        landcolor="rgb(217, 217, 217)",
        bgcolor="white",
        countrycolor="white",
        coastlinecolor="white",
        lonaxis_range=[-17.53-3, 51.415+3],
        lataxis_range=[-34.833-3, 37.347+3],
    )
)
fig.update_layout(height=600,width=800)
fig.show()

Thank you for suggesting a solution that might navigate my issue. I did consider removing the scope, but was informed that the map should only illustrate specific countries. I.e. the end result should look like this - a map of the MEA region (with the bubbles of course):

My appologies for not being specific in terms of what I’m trying to accomplish. Is there any way to hide the countries in your map?

Unfortunately, the scope of the map can be specified as [‘africa’, ‘asia’, ‘europe’, ‘north america’, ‘south america’, ‘usa’, ‘world’], and only countries in that scope will be displayed. I don’t think it is possible to specify an arbitrary country.

Hi,

I would have done it this way:

  1. download the geometric boundaries for all countries (e.g. from Natural earth Data: Natural Earth » 1:110m Cultural Vectors - Free vector and raster map data at 1:10m, 1:50m, and 1:110m scales)
  2. read the geometries with GeoPandas. You can load the zip file directly: Reading and writing files — GeoPandas 1.0.1+0.g747d66e.dirty documentation
  3. filter out the countries you don’t need (the natural earth data contains the names and ISO codes for example)
  4. plot your choropleth map, following the doc instructions here: Choropleth maps in Python

I hope it helps.