简体   繁体   中英

Plotly Bubble Map from list of cities

I have a list of city names all within a country, and I want to use plotly to build a bubble map of the listed cities, where the size of each bubble is relative to how many times a given city is listed.

So far I am able to generate the empty map with plotly express, But in terms of displaying the actual data I am unsure how to proceed.

I understand that a lot of the examples on the plotly website use dataframes, which makes me think that I will need to generate one from this list. In particular, the third example on the linked page above uses lat/longitude to locate the bubbles. Is there a way to graph the bubbles from the city names directly? Otherwise, how can I approach this?

Thanks for any help, as I am not terribly familiar with plotly.

  • to plot a scatter (which you referred to as bubble) you need the GPS co-ordinates of cities. Have sourced these from here: https://simplemaps.com/data/world-cities
  • you have not provided sample data of a dataframe which contains list of cities. Have generated a random list delist
  • now it's a simple case of
    1. aggregate cities to number of times in list dflist.groupby("city", as_index=False).size()
    2. merge geometry merge(dfuk, on="city")
    3. generate plotly figure
import sys, requests, urllib
import pandas as pd
from pathlib import Path
from zipfile import ZipFile
import plotly.express as px

# fmt: off
# download data set, world cities including GPS co-ordinates
url = "https://simplemaps.com/static/data/world-cities/basic/simplemaps_worldcities_basicv1.74.zip"

f = Path.cwd().joinpath(f'{urllib.parse.urlparse(url).path.split("/")[-1]}.zip')
if not f.exists():
    r = requests.get(url, stream=True, headers={"User-Agent": "XY"})
    with open(f, "wb") as fd:
        for chunk in r.iter_content(chunk_size=128):
            fd.write(chunk)
    
zfile = ZipFile(f)
dfs = {f.filename: pd.read_csv(zfile.open(f)) for f in zfile.infolist() if f.filename.split(".")[1]=="csv"}
# fmt: on

# just UK cities.. with GPS
dfuk = dfs["worldcities.csv"].loc[lambda d: d["iso3"].eq("GBR")]

# dataframe referred to in question, cities listed multiple times
n_cities = 20
p = np.geomspace(0.01, 1, n_cities)
p = p / p.sum()
dflist = pd.DataFrame({"city": np.random.choice(dfuk.sample(n_cities)["city"], 500, p=p)})

px.scatter_mapbox(
    dflist.groupby("city", as_index=False).size().merge(dfuk, on="city"),
    lat="lat",
    lon="lng",
    hover_name="city",
    size="size",
).update_layout(mapbox={"style": "carto-positron", "zoom": 4}, margin={"t":0,"b":0,"l":0,"r":0})

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM