python如何画出漂亮的地图?

这篇博客介绍了如何利用Python的folium库绘制出精美的地图。通过简单几步,如导入folium、设定地图中心点、加载数据、在地图上标记数据、统计并展示各区域犯罪数量,以及创建Choropleth Map和热力图,可以实现地理数据的高效可视化。folium还支持创建动态图,如动态热力图和动态路径图。
摘要由CSDN通过智能技术生成

推荐一个超好用的python包folium, 专门用于地理数据可视化
在这里插入图片描述
使用方法很简单,操作如下:

1.导入包,创建一副世界地图

import folium
import pandas as pd

# define the world map
world_map = folium.Map()

# display world map
world_map

在这里插入图片描述
2. 输入经纬度,尺度,在这里我们以旧金山(37.7749° N, 122.4194° W)为例。

# San Francisco latitude and longitude values
latitude = 37.77
longitude = -122.42

# Create map and display it
san_map = folium.Map(location=[latitude, longitude], zoom_start=12)

# Display the map of San Francisco
san_map

在这里插入图片描述
更改地图显示,默认为’OpenStreetMap’风格,我们还可以选择’Stamen Terrain’, 'Stamen Toner’等。

# Create map and display it
san_map = folium.Map(location=[latitude, longitude], zoom_start=12,tiles='Stamen Toner')

在这里插入图片描述
3. 读取数据集(旧金山犯罪数据集)

# Read Dataset 
cdata = pd.read_csv('https://cocl.us/sanfran_crime_dataset')
cdata.head()

在这里插入图片描述
4. 在地图上显示前200条犯罪数据

作者:NaNNN
链接:https://www.zhihu.com/question/33783546/answer/775946401
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

# get the first 200 crimes in the cdata
limit = 200
data = cdata.iloc[0:limit, :]

# Instantiate a feature group for the incidents in the dataframe
incidents = folium.map.FeatureGroup()

# Loop through the 200 crimes and add each to the incidents feature group
for lat, lng, in zip(cdata.Y, data.X):
    incidents.add_child(
        folium.CircleMarker(
            [lat, lng],
            radius=7, # define how big you want the circle markers to be
            color='yellow',
            fill=True,
            fill_color='red',
            fill_opacity=0.4
        )
    )

# Add incidents to map
san_map = folium.Map(location=[latitude, longitude], zoom_start=12)
san_map.add_child(incidents)

在这里插入图片描述
5. 添加地理标签

# add pop-up text to each marker on the map
latitudes = list(data.Y)
longitudes = list(data.X)
labels = list(data.Category)

for lat, lng, label in zip(latitudes, longitudes, labels):
    folium.Marker([lat, lng], popup=label).add_to(san_map)    
    
# add incidents to map
san_map.add_child(incidents)

在这里插入图片描述
6. 统计区域犯罪总数

作者:NaNNN
链接:https://www.zhihu.com/question/33783546/answer/775946401
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

from folium import plugins

# let's start again with a clean copy of the map of San Francisco
san_map = folium.Map(location = [latitude, longitude], zoom_start = 12)

# instantiate a mark cluster object for the incidents in the dataframe
incidents = plugins.MarkerCluster().add_to(san_map)

# loop through the dataframe and add each data point to the mark cluster
for lat, lng, label, in zip(data.Y, data.X, cdata.Category):
    folium.Marker(
        location=[lat, lng],
        icon=None,
        popup=label,
    ).add_to(incidents)

# add incidents to map
san_map.add_child(incidents)

在这里插入图片描述

  1. 读取geojson文件,可视化旧金山市10个不同Neighborhood的边界
import json
import requests

url = 'https://cocl.us/sanfran_geojson'
san_geo = f'{url}'
san_map = folium.Map(location=[37.77, -122.4], zoom_start=12)
folium.GeoJson(
    san_geo,
    style_function=lambda feature: {
        'fillColor': '#ffff00',
        'color': 'black',
        'weight': 2,
        'dashArray': '5, 5'
    }
).add_to(san_map)

#display map
san_map

在这里插入图片描述

  1. 统计每个区域的犯罪事件数目
# Count crime numbers in each neighborhood
disdata = pd.DataFrame(cdata['PdDistrict'].value_counts())
disdata.reset_index(inplace=True)
disdata.rename(columns={'index':'Neighborhood','PdDistrict':'Count'},inplace=True)
disdata

在这里插入图片描述
9. 创建Choropleth Map (颜色深浅代表各区犯罪事件数目)

m = folium.Map(location=[37.77, -122.4], zoom_start=12)
folium.Choropleth(
    geo_data=san_geo,
    data=disdata,
    columns=['Neighborhood','Count'],
    key_on='feature.properties.DISTRICT',
    #fill_color='red',
    fill_color='YlOrRd',
    fill_opacity=0.7,
    line_opacity=0.2,
    highlight=True,
    legend_name='Crime Counts in San Francisco'
).add_to(m)
m<img src="https://pic4.zhimg.com/50/v2-4ec6f4738108ddafa4a2e949b64017e7_hd.jpg" data-rawwidth="1224" data-rawheight="736" data-size="normal" data-caption="" data-default-watermark-src="https://pic4.zhimg.com/50/v2-16e46b65a65bd879864465eaadac309c_hd.jpg" class="origin_image zh-lightbox-thumb" width="1224" data-original="https://pic3.zhimg.com/v2-4ec6f4738108ddafa4a2e949b64017e7_r.jpg"/>

在这里插入图片描述
10. 创建热力图

from folium.plugins import HeatMap

# let's start again with a clean copy of the map of San Francisco
san_map = folium.Map(location = [latitude, longitude], zoom_start = 12)

# Convert data format
heatdata = data[['Y','X']].values.tolist()

# add incidents to map
HeatMap(heatdata).add_to(san_map)

san_map

在这里插入图片描述
最后,folium还可以用来创建动态热力图,动态路径图等
实现效果如下图所示
在这里插入图片描述

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值