# c6 f, s. k# F6 i 推荐一个超好用的python包folium, 专门用于地理数据可视化,官方英文教程教程点击这里。
% W& y( V8 k/ U* T m4 ]6 ? " q4 _) y2 ?, z0 {, B6 C
使用方法很简单,操作如下:
导入包,创建一副世界地图3 ?, [4 X O, @- }' h, ?7 s
import folium. t f8 C" K2 t& c( K: } Y+ o
import pandas as pd
& N7 F1 a, R* t! H3 U, b/ G" A$ d/ A: u& t" V; s8 J
# define the world map% w% X% E+ R! B7 Q2 s8 h' h7 h- O
world_map = folium.Map()
6 _* `7 h4 e5 b- [0 @+ W9 j
' I @0 G7 i5 n+ S0 L # display world map& c6 w2 T1 V6 W8 A& b+ Y4 @
world_map
9 C4 [! T4 p: Q( d0 y+ L+ z
: K R' R) J/ s0 G8 a0 \: R 2. 输入经纬度,尺度,在这里我们以旧金山(37.7749° N, 122.4194° W)为例。
- Z# O3 r6 G0 A9 S # San Francisco latitude and longitude values$ h" r; ^0 ~& k. S! {
latitude = 37.77' a* U9 I* ] l; a
longitude = -122.42
: K1 d0 z4 B" {) I: T# s/ V* t" p
$ \2 v$ P. @2 Y; u # Create map and display it; P4 Y! `! N1 D5 M3 k' _
san_map = folium.Map(location=[latitude, longitude], zoom_start=12)! K4 z* n8 k- t1 q5 N" u4 v/ f
# D; T* p" N! p5 u6 ` # Display the map of San Francisco
' K# }$ \1 a' S2 S) ]: } san_map
* r' `! d# v/ d
- a! w7 p& c; Q" {/ u( }7 x 更改地图显示,默认为OpenStreetMap风格,我们还可以选择Stamen Terrain, Stamen Toner等。
" S( B' Y6 @9 x- Y # Create map and display it
7 f& ?5 G2 m9 x& M2 C san_map = folium.Map(location=[latitude, longitude], zoom_start=12,tiles=Stamen Toner)
( J9 W/ k4 v% K- S: ~9 b
4 ?- w4 y/ o- u3 O; L2 O2 J- w8 c 3. 读取数据集(旧金山犯罪数据集)
6 i7 C' k3 ~: Z+ C3 I# ] # Read Dataset! Q' u( o2 L$ S8 q6 S
cdata = pd.read_csv(https://cocl.us/sanfran_crime_dataset)
/ o' [; E' M% h6 W cdata.head()
1 O# k% s" U5 w' W . K' a0 `: K& Q4 c+ F. _
4. 在地图上显示前200条犯罪数据
: i. s9 G) U9 P' x2 \& x l: S
# get the first 200 crimes in the cdata
+ Z! T- `, H) Q- p2 I& ]; i limit = 200
$ V- x9 Z" C$ ]; ?" h data = cdata.
iloc[0:limit, :]
) e" z8 n4 O" c( D
' A5 Z8 n0 [$ u. V # Instantiate a feature group for the incidents in the dataframe
9 n6 S9 C" u! g4 m* t8 L0 C incidents = folium.map.FeatureGroup()
+ S+ [: a2 R7 S- t0 Z
! R, x% S" G6 U9 u% I5 Q
# Loop through the 200 crimes and add each to the incidents feature group
' o9 p1 O. @3 r% U! H2 J
for lat, lng, in zip(cdata.Y, data.X):
5 E2 q% |6 G" S% J" O4 j
incidents.add_child(
6 ~& J4 i- S5 F1 b) D$ m n
folium.CircleMarker(
" T) l5 V. |" {# v
[lat, lng],
* a$ |# O' D5 ]2 Z- H
radius=7, # define how big you want the circle markers to be
; d" m5 R6 q" g+ g# e* | color=yellow,
" Y# r% W4 g7 i9 X( k
fill=True,
. e' H5 \ B$ U! Z fill_color=red,
% n6 o) m: F; l& h% M: I
fill_opacity=0.4
9 h2 C$ P3 ^. j( Y$ h0 W p
)
- f( z( x' i( H5 a4 w )
9 ^$ ^2 \0 c5 E8 {
0 Q3 H9 x$ n7 C% |1 ?+ G' N # Add incidents to map
$ T' D7 G0 q# d
san_map = folium.Map(location=[latitude, longitude], zoom_start=12)
1 k4 x# B) Y r+ k8 M san_
map.add_child(incidents)
2 \# x. t/ W: z4 Y+ M * ~7 J5 \) _' [. x! Z8 k v
5. 添加地理标签
" q8 j: z4 C2 m) Y
# add pop-up text to each marker on the map
: Z# U# Z9 H8 J6 c1 {1 S# ?* M# W
latitudes = list(data.Y)
( r8 i& }, [( F8 N longitudes = list(data.X)
# P9 {: R) I% g# V$ P& j
labels = list(data.Category)
5 \2 V( `! }, n& [7 t
! i( l& Q1 K# \- H4 T+ l for lat, lng, label in zip(latitudes, longitudes, labels):
+ r- ?+ [+ ]% \9 i( g$ P, E1 I
folium.Marker([lat, lng], popup=label).add_to(
san_map)
; B0 o+ l3 y/ n& o4 A# c, h# g; A& D. G
# add incidents to map
/ F. d# k) F* F% z: e2 `
san_map.add_child(incidents)
Y2 d* w% @! v* ~6 G! ^3 q I
% l( \3 |7 |3 Y
6. 统计区域犯罪总数
6 {5 {) }+ L1 `7 I+ L+ B. S" Z
from folium import plugins
& r. _" e) [- A2 k
; X2 M2 X9 B! m; D # lets start again with a clean copy of the map of San Francisco
+ K3 f) i! I7 ? san_map = folium.Map(location = [latitude, longitude], zoom_start = 12)
L% g1 W& \! m X4 d5 |
4 N% @( G* l* J. ^" { # instantiate a mark cluster object for the incidents in the dataframe
8 @3 r3 G" U( q incidents = plugins.MarkerCluster().add_to(san_map)
" c! ^' X2 z% a3 M9 W! S9 y3 w! A9 S3 h/ n
# loop through the
dataframe and add each data point to the mark cluster
: J+ N3 i; z+ c0 F3 u- K Q- M for lat, lng, label, in zip(data.Y, data.X, cdata.Category):
1 ?2 ~! z, H9 R* H1 h, i folium.Marker(
S& j- a) M. ` T) ` location=[lat, lng],
8 F8 d+ K8 ` J) F- c) F icon=None,
: H- Y- z. ?# U8 F4 a popup=label,
3 y# E& H- q. H* n$ o7 q ).add_to(incidents)
3 l; \4 S1 E2 `7 o: J/ L7 o1 `4 |
" G, o6 B7 n) B' {2 ] # add incidents to map
. E" A+ u7 r7 j5 h% K
san_map.add_child(incidents)
6 e5 D+ D/ W# J 5 {; R, V' i8 n n; p, e5 |$ }
7. 读取geojson文件,可视化旧金山市10个不同Neighborhood的边界
' n- i& R, |6 o( K
import json$ Q9 Q" T4 s( v4 O+ b
import requests
4 p$ A7 I/ {" u$ y+ G; z6 A+ c
/ ?( i6 i- X5 C' O+ e3 ~4 u url = https://cocl.us/sanfran_geojson- u$ s6 {. D3 F8 D/ R
san_geo = f{url}, W9 G; d( q4 D1 D5 k
san_map = folium.Map(location=[37.77, -122.4], zoom_start=12)9 _6 Q8 E4 ]+ K; v5 y) m- z
folium.GeoJson(
: s' b4 ~- c% s, \ J7 D8 T san_geo,& G* u. @( b$ @2 |) K
style_function=lambda feature: {: \8 i' D8 b* ]/ O3 ]
fillColor: #ffff00,7 V! M' D6 S4 O3 X8 S
color: black,/ |! ~6 k* ~5 ?' w2 G
weight: 2,( G, M4 W i; d; t) J
dashArray: 5, 5
/ c6 E- H! V2 G+ h. l/ r1 y2 B( u }
^: X7 p7 ~8 i( Z1 [ ).add_to(san_map)' E3 Y7 C. k0 u% B6 g0 J- b4 R
, T4 Y6 l; y: y* ~( `/ s #display map
+ ?3 ^. b9 X9 ~) W6 z {0 }7 E' I2 C san_map
3 O5 Y, J* w& T$ J- _
, U/ k4 Y H% G& f
8. 统计每个区域的犯罪事件数目
5 \) q4 F$ j( q # Count crime numbers in each
neighborhood- {; Z. s2 M& e7 ~# W
disdata = pd.DataFrame(cdata[PdDistrict].value_counts())
4 H1 g. W! P4 K) h
disdata.reset_index(inplace=True)
$ c9 g6 [* F9 f$ i# k# V
disdata.rename(columns={index:Neighborhood,PdDistrict:Count},inplace=True)
6 B1 c$ G0 G5 E disdata
; q5 l) U& S/ l! i/ k% } @4 w; J
5 L: p; e* l. ^' d% x; C L
9. 创建Choropleth Map (颜色深浅代表各区犯罪事件数目)
0 Y4 z% j$ Y& B* d4 N; r
m = folium.Map(location=[37.77, -122.4], zoom_start=12)
7 X$ K6 I, N6 k& T3 S, o folium.Choropleth(
; f6 l2 _4 Q) s) m: S
geo_data=san_geo,
2 m: e- f/ ?3 a. n data=disdata,
2 l3 z6 T% W' g) m5 V/ Q columns=[Neighborhood,Count],
! j" Z2 I9 U3 U, t, m% i
key_on=feature.properties.DISTRICT,
6 I' h: O* l" [# b0 H #fill_color=red,
9 _6 @6 _3 q- ?0 g! L fill_color=YlOrRd,
6 q: {2 a. U: O3 }$ J
fill_opacity=0.7,
- a- a" l9 w7 U+ v4 I9 x% D+ a
line_opacity=0.2,
1 n4 u( V5 W7 [& _8 \2 F highlight=True,
) c R# I1 j f6 j9 I
legend_name=Crime Counts in San Francisco
( x3 h; M k2 z6 a5 i
).add_to(m)
9 O) v4 `( `0 F' w* Z m
. e6 m( d2 f* ]+ ~ A2 f& C3 C ' M0 p: b, e( V
/ k) d3 V: q* a) l* C6 K( b
10. 创建热力图
! x0 I8 Q/ b3 b5 x3 V
from folium.plugins import HeatMap
5 e9 ~% T8 p* L8 S, H4 R l4 m7 z8 h |8 T8 o/ H
# lets start again with a clean copy of the map of San Francisco+ _( W; e8 _* N
san_map = folium.Map(location = [latitude, longitude], zoom_start = 12) L8 }, M: o* P7 O# b1 C
( Z: t5 I5 k1 B. t n0 B
# Convert data format
- O1 n+ y) b' P7 Z heatdata = data[[Y,X]].values.tolist()
( X, i* e3 F- T6 `' [1 q; I8 D
# add incidents to map- Z# |) X2 k6 a! i3 u" H3 M
HeatMap(heatdata).add_to(san_map)6 b' F/ @) y( Y& R! j5 Q
" w* T3 Y" j& o* Y, F6 o2 O7 d
san_map
- l! K, K. K/ M) D
; k1 e2 F; M8 Z6 p( ^3 @ 最后,folium还可以用来创建动态热力图,动态路径图等,具体可参考Medium上的这篇文章。
. Z; U: e) V+ }- M- h
实现效果如下图所示 (直接从Medium上抱过来的图,详细代码请点击上述链接)。
9 U! x+ p& y4 b" _$ H0 q
7 e( U+ N: x9 Y0 q5 y" o+ ^7 c% l) F 我的其他回答:
2 z7 I( G' f& R& Z+ x
7 P$ q+ { N# q
- E- }# f5 l7 J4 w* v4 J
, _/ K( c1 a, g4 i4 P1 A$ y 最近有小伙伴私信我推荐一些学习编程的经验,我给大家几点建议:
$ x3 n f4 d4 V6 X1 Q' n5 b
1. 尽量不去看那些冗长的视频教程,不是说学不到知识,而是,在你学到知识之前,你可能已经睡着了 =_=
4 h! ?! m1 S1 K1 D& R1 _ 2. 不要贪多,选一个知名度高的python教程,教学为辅,练习为主。要随时记住,我们学习python的目的在于会用,而不是背过了多少知识点。
6 B& S! m, J; Y* ?5 C6 h 给大家推荐一款我超喜欢的python课程——夜曲编程。我闲没事在上面刷了一些编程题目,竟回想起当年备考雅思时被百词斩支配的恐惧。这款课程对新手小白很适合。虽然有手机app,但我建议你们用网页端学习哈。
4 z9 Y. |; W5 L* I% r( E6 f9 C 最后,我的终极建议是:无论选什么教程,贪多嚼不烂,好好专注一门课程,勤加练习才是提高编程水平的王道。等基础知识学的差不多了,去Kaggle上参加几场比赛,琢磨琢磨别人的编程思路和方法就完美啦!
k4 B. V+ M7 N* M3 c