简体   繁体   中英

Plotting City Wise Data on World Map

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.

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])

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