简体   繁体   English

Plotly 气泡 Map 来自城市列表

[英]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.我有一个国家/地区的城市名称列表,我想使用 plotly 构建列出城市的气泡 map ,其中每个气泡的大小与列出给定城市的次数有关。

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.到目前为止,我能够使用 plotly express 生成空的 map,但在显示实际数据方面,我不确定如何进行。

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.我知道 plotly 网站上的许多示例都使用数据框,这让我觉得我需要从这个列表中生成一个。 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.感谢您的帮助,因为我对 plotly 不是很熟悉。

  • to plot a scatter (which you referred to as bubble) you need the GPS co-ordinates of cities.到 plot 一个点(您称为气泡),您需要城市的 GPS 坐标。 Have sourced these from here: https://simplemaps.com/data/world-cities从这里采购了这些: https://simplemaps.com/data/world-cities
  • you have not provided sample data of a dataframe which contains list of cities.您没有提供包含城市列表的 dataframe 的样本数据。 Have generated a random list delist已生成随机列表delist
  • now it's a simple case of现在这是一个简单的案例
    1. aggregate cities to number of times in list dflist.groupby("city", as_index=False).size()将城市聚合到列表中的次数dflist.groupby("city", as_index=False).size()
    2. merge geometry merge(dfuk, on="city")合并几何merge(dfuk, on="city")
    3. generate plotly figure生成plotly
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})

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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