在世界地图上绘制城市明智的数据
世界上的城市字典和其中的事件
我有一个
{'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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了此问题,
使用了上面的相同代码来绘制从城市名称中查找国家名称,只是使用了下面的代码
Solved this
Used the Same Code above to Plot to Find Country Name from City name Just Used the Code Below