在世界地图上绘制城市明智的数据

发布于 2025-02-01 17:58:39 字数 1166 浏览 3 评论 0原文

世界上的城市字典和其中的事件

我有一个

{'Iasi': 4, 'Reston': 38, 'Krakow': 2, 'Amsterdam': 103, 'Washington': 99, 'Madrid': 9, 'Tehran': 3, 'New Delhi': 2, 'Hanoi': 2, 'Johor': 3, 'Chicago': 3, 'W Sussex': 13, 'Shiraz': 2, 'Morelos': 2, 'New York': 49, 'Abingdon': 19, 'Basel': 17, 'Beijing': 51, 'Malden': 4, 'London': 47, 'Heidelberg': 14}

频率

因此, 绘制国家明智的数据 (代码中使用的数据不是我的数据)

import plotly.express as px
import numpy as np
import pandas as pd

np.random.seed(12)
gapminder = px.data.gapminder().query("year==2007")
#gapminder['counts'] = np.nan

d = {'United States': [139421],
    'Canada': [21601], 
    'United Kingdom': [18314],
    'Germany': [17024],
    'Spain': [13096]}

yourdata = pd.DataFrame(d).T.reset_index()
yourdata.columns=['country', 'count']

df=pd.merge(gapminder, yourdata, how='left', on='country')

fig = px.choropleth(df, locations="iso_alpha",
                    color="count", 
                    hover_name="country", # column to add to hover information
                    color_continuous_scale=px.colors.sequential.Plasma)

fig.show()

,但这是根据国家名称而不是城市名称的绘图的问题上面给出的城市。

So I have a Dictionary of Cities in World and Frequency of Events in them

For Example a small subset of Data

{'Iasi': 4, 'Reston': 38, 'Krakow': 2, 'Amsterdam': 103, 'Washington': 99, 'Madrid': 9, 'Tehran': 3, 'New Delhi': 2, 'Hanoi': 2, 'Johor': 3, 'Chicago': 3, 'W Sussex': 13, 'Shiraz': 2, 'Morelos': 2, 'New York': 49, 'Abingdon': 19, 'Basel': 17, 'Beijing': 51, 'Malden': 4, 'London': 47, 'Heidelberg': 14}

Now I want to plot this Data on World map to show how many events happen from which location

I found a code online to do similar kind of plotting for country wise data
(Data used in Code is not my Data)

import plotly.express as px
import numpy as np
import pandas as pd

np.random.seed(12)
gapminder = px.data.gapminder().query("year==2007")
#gapminder['counts'] = np.nan

d = {'United States': [139421],
    'Canada': [21601], 
    'United Kingdom': [18314],
    'Germany': [17024],
    'Spain': [13096]}

yourdata = pd.DataFrame(d).T.reset_index()
yourdata.columns=['country', 'count']

df=pd.merge(gapminder, yourdata, how='left', on='country')

fig = px.choropleth(df, locations="iso_alpha",
                    color="count", 
                    hover_name="country", # column to add to hover information
                    color_continuous_scale=px.colors.sequential.Plasma)

fig.show()

But the Issue with this is it plots according to Country Names not city names does anyone know a way so that I can plot by city or any way to create a Country wise dictionary from the Dictionary of Cities given above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过潦 2025-02-08 17:58:40

解决了此问题,

使用了上面的相同代码来绘制从城市名称中查找国家名称,只是使用了下面的代码

from geopy.geocoders import Nominatim

j={'Iasi': 4, 'Reston': 38, 'Krakow': 2, 'Amsterdam': 103, 'Washington': 99, 'Madrid': 9, 'Tehran': 3, 'New Delhi': 2, 'Hanoi': 2, 'Johor': 3, 'Chicago': 3, 'W Sussex': 13, 'Shiraz': 2, 'Morelos': 2, 'New York': 49, 'Abingdon': 19, 'Basel': 17, 'Beijing': 51, 'Malden': 4, 'London': 47, 'Heidelberg': 14}

geolocator = Nominatim(user_agent = "geoapiExercises")
for i in j :
   location = geolocator.geocode(i)
   #print("Country Name: ", location)
   loc_dict=location.raw
   k=(loc_dict['display_name'].rsplit(',' , 1)[1])

Solved this

Used the Same Code above to Plot to Find Country Name from City name Just Used the Code Below

from geopy.geocoders import Nominatim

j={'Iasi': 4, 'Reston': 38, 'Krakow': 2, 'Amsterdam': 103, 'Washington': 99, 'Madrid': 9, 'Tehran': 3, 'New Delhi': 2, 'Hanoi': 2, 'Johor': 3, 'Chicago': 3, 'W Sussex': 13, 'Shiraz': 2, 'Morelos': 2, 'New York': 49, 'Abingdon': 19, 'Basel': 17, 'Beijing': 51, 'Malden': 4, 'London': 47, 'Heidelberg': 14}

geolocator = Nominatim(user_agent = "geoapiExercises")
for i in j :
   location = geolocator.geocode(i)
   #print("Country Name: ", location)
   loc_dict=location.raw
   k=(loc_dict['display_name'].rsplit(',' , 1)[1])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文