世界疫情及地级市疫情地图可视化python代码
时间: 2023-11-12 08:48:35 浏览: 226
python 疫情数据可视化
5星 · 资源好评率100%
以下是一个简单的世界疫情地图可视化的 Python 代码,使用了 Plotly 库和数据集来自于 John Hopkins University。
代码:
```python
import pandas as pd
import plotly.express as px
# 加载数据集
data = pd.read_csv('https://raw.githubusercontent.com/datasets/covid-19/main/data/worldwide-aggregated.csv')
# 可视化地图
fig = px.choropleth(data, locations='Country', locationmode='country names', color='Confirmed', hover_name='Country', animation_frame='Date')
fig.update_layout(title_text='Worldwide COVID-19 Cases')
fig.show()
```
如果你想要查看中国的地级市疫情地图可视化,可以使用百度地图API和数据集来自于丁香园。
代码:
```python
import requests
import json
import pandas as pd
import plotly.express as px
# 加载数据集
url = 'https://ncov.dxy.cn/ncovh5/view/pneumonia'
response = requests.get(url)
response.encoding = 'utf-8'
html = response.text
json_str = html.split('try { window.getAreaStat = ')[1].split('}catch(e){};')[0]
data = json.loads(json_str)
# 处理数据
province_data = []
for province in data:
for city in province['cities']:
province_data.append({
'Province': province['provinceShortName'],
'City': city['cityName'],
'Confirmed': city['confirmed'],
'Lat': city['location']['lat'],
'Lon': city['location']['lng']
})
city_data = pd.DataFrame(province_data)
# 可视化地图
fig = px.scatter_mapbox(city_data, lat='Lat', lon='Lon', hover_name='City', size='Confirmed', color='Province', zoom=3, mapbox_style='open-street-map')
fig.update_layout(title_text='China COVID-19 Cases')
fig.show()
```
需要注意的是,以上代码可能无法直接运行,需要根据具体情况进行修改和调整。
阅读全文