6 a9 }3 y* ~& h" l 推荐一个超好用的python包folium, 专门用于地理数据可视化,官方英文教程教程点击这里。
# x2 t) X& N( G( L
7 _& b6 S( f/ O6 Z! N3 a7 N0 z* O 使用方法很简单,操作如下:
导入包,创建一副世界地图. ]4 s- w2 L; g8 R" G5 b% `: E1 l; \
import folium
4 ~* X% X. d9 P/ h2 I! M, L import pandas as pd
& @: g- s7 c0 L+ G! @8 t% i" z1 W8 }4 }1 ~2 m. N; c" z! \
# define the world map, I ~9 A, z) j/ k) q4 Y
world_map = folium.Map()% K d& L7 D. W8 E* T6 e
" w1 B3 q+ Y& Y8 r
# display world map% b% H* b. b1 H& m6 J
world_map
+ b3 a U, u _7 D6 m3 Z
% D9 z: f( c7 B* h" C# q8 m 2. 输入经纬度,尺度,在这里我们以旧金山(37.7749° N, 122.4194° W)为例。
+ ~! C' P" e1 D$ m # San Francisco latitude and longitude values4 |3 e1 }4 i9 L m w" n
latitude = 37.77
& R2 k1 g2 z* x) `/ G longitude = -122.42
$ _2 f4 M6 x* V9 a" R& W S5 r4 v6 i5 n3 |$ e
# Create map and display it
+ Z$ c( `7 N* H! N san_map = folium.Map(location=[latitude, longitude], zoom_start=12)9 ]: Z' U* x/ w5 D: B
% Q6 K7 t; g3 b
# Display the map of San Francisco
: t" q4 Z* R" p" c( u7 u san_map
7 U7 [4 l5 x9 k7 p( c, V3 j' K+ A . \) H8 F. A# C8 M K7 J
更改地图显示,默认为OpenStreetMap风格,我们还可以选择Stamen Terrain, Stamen Toner等。
1 t5 N7 v9 M/ `7 J6 q # Create map and display it
, P: ?& h r6 Y3 C san_map = folium.Map(location=[latitude, longitude], zoom_start=12,tiles=Stamen Toner)
4 A5 j7 F2 Q+ W# X& Q# i5 a
( W$ @# [6 Q+ D4 e' o n 3. 读取数据集(旧金山犯罪数据集)
. V: M; z; z$ P
# Read Dataset) c0 ?+ M4 E4 z: `2 e$ l R
cdata = pd.read_csv(https://cocl.us/sanfran_crime_dataset)9 I$ v# |8 |2 U" E# K/ N
cdata.head()
0 x+ ?# J9 `3 W! ~) D
7 ?' F o x5 d( m$ g+ \: o* X
4. 在地图上显示前200条犯罪数据
) P6 }' S2 C7 a, I: w # get the first 200 crimes in the cdata
. L7 ^3 j' d' A limit = 200
1 }4 }; k- P4 T data = cdata.
iloc[0:limit, :]
6 t) V- `' u. J
7 g f7 z3 L, s( t6 F' M- y' S
# Instantiate a feature group for the incidents in the dataframe
, c4 [+ t: u% I2 P- p incidents = folium.map.FeatureGroup()
2 v, p7 @ \5 O* `; o& P( {8 X. `' A
: Q, A$ |+ N9 Z; k# Y7 f+ a
# Loop through the 200 crimes and add each to the incidents feature group
& L w8 \& T2 {4 ]& E: Y for lat, lng, in zip(cdata.Y, data.X):
/ Z6 }9 y0 B+ w0 X
incidents.add_child(
) K6 X9 @2 N9 E folium.CircleMarker(
1 f }/ Z" S" ]* e
[lat, lng],
5 ~+ @7 _5 q7 t radius=7, # define how big you want the circle markers to be
+ l0 d0 Q' B6 {% m& ^9 k" l color=yellow,
5 o9 @+ F0 l9 K# P8 ^8 k
fill=True,
9 T. I+ `; H- ]7 n
fill_color=red,
4 d' y7 @* O# m- x. {
fill_opacity=0.4
8 A) h/ Z+ n& @. k' ~4 E# ~
)
" D1 d/ L( |6 k) h8 Y* v )
# [' G% J& \$ a# q# U! @2 N7 l* _3 m% e ^
# Add incidents to map
: K0 z2 ~; e5 r- _6 k# A/ U san_map = folium.Map(location=[latitude, longitude], zoom_start=12)
% M: N! K$ E, I5 K0 C$ G! @ san_
map.add_child(incidents)
* z+ A! ^' p$ R# U
9 }7 n" g- Y% e4 z9 H& t& Q: y
5. 添加地理标签
) x/ K6 C8 Z& H) \4 ~+ s* P* b # add pop-up text to each marker on the map
# b" F2 ]3 b1 ~$ T
latitudes = list(data.Y)
; \! q1 j4 f: H0 V7 s' j. X9 }, n
longitudes = list(data.X)
% x8 Y4 e6 [0 t" B0 @! `; I" G labels = list(data.Category)
! g0 @/ U& U) |- r; F3 V* m5 j8 N3 M& v1 _8 J
for lat, lng, label in zip(latitudes, longitudes, labels):
. P8 n* ]* }# c$ ` J; _ folium.Marker([lat, lng], popup=label).add_to(
san_map)
) k, w8 N! I5 U `* y4 n
, G6 ?2 p& N6 X7 t# K- i( Y& B
# add incidents to map
: ^7 B2 f# n( z7 I3 d2 B! ]% R0 u( U. u san_map.add_child(incidents)
0 M) B; ~% E8 b$ i( [4 m
2 U+ A! @6 c) Z. ]! \ 6. 统计区域犯罪总数
" y6 w0 j& W5 b: ~; @- s8 b
from folium import plugins
& \; Y! P$ l3 Z" [7 I9 d$ L( A2 o; }% f9 q
# lets start again with a clean copy of the map of San Francisco
0 g p1 r: _1 a+ O! |9 {9 v( g( q
san_map = folium.Map(location = [latitude, longitude], zoom_start = 12)
0 \5 M+ ?" g' b# z2 y
2 L$ J* I' v* V# n. L) [$ g # instantiate a mark cluster object for the incidents in the dataframe
) @7 h/ W/ o% x incidents = plugins.MarkerCluster().add_to(san_map)
( t; ?, J; c& C/ e' U# A( {5 k+ n0 ?( U' Y
# loop through the
dataframe and add each data point to the mark cluster
- W2 h( g8 l z
for lat, lng, label, in zip(data.Y, data.X, cdata.Category):
, q; i5 v( s# p+ w9 [" a' t$ P folium.Marker(
, k9 E6 j5 F* L$ K4 I
location=[lat, lng],
$ E4 V/ }& r" t8 m5 g" ]) X icon=None,
3 n! G/ w, ?7 v M" }$ t7 g- r! o1 x
popup=label,
6 u+ E; ?+ k e ).add_to(incidents)
& k9 s$ M% d, V e
1 f7 s4 z8 [$ z! ?* V
# add incidents to map
- h/ I# @# s8 @ P8 D
san_map.add_child(incidents)
+ E2 S4 r% @0 E! a1 }3 h/ k F5 V
& X, I$ H$ {6 U* l% P$ @5 [ 7. 读取geojson文件,可视化旧金山市10个不同Neighborhood的边界
& { c5 b& a' P- S& l; u import json& L5 P; h$ W7 d+ c0 c' q3 b
import requests3 m! a; S2 _" I- ~7 g. D+ U
. q/ @) U4 y/ S7 \. z' c url = https://cocl.us/sanfran_geojson# E- h, e. `- s [( \5 G
san_geo = f{url}
, ^5 s6 ?8 J- _" S/ r: v5 O san_map = folium.Map(location=[37.77, -122.4], zoom_start=12)% b" d! d- E, _- d" @
folium.GeoJson(9 ~7 G. K5 C+ F/ x4 o
san_geo,
( X3 o0 {( ?$ U7 \. @" f, T. i0 T4 C style_function=lambda feature: {1 w8 U. {4 Q6 V% J( Q
fillColor: #ffff00,
; q* e Q1 Y, `6 O* e color: black,
/ l# b& K" G: d5 G8 ~ weight: 2,6 X& c" h" Z8 J# C2 H+ W) }3 ]
dashArray: 5, 5
3 E) f; Y0 L- T- [( ? }' a. X$ P$ a! i
).add_to(san_map) t8 M2 L1 ]' f" M: ?* N( ?5 ]
; [5 W% p/ [( ~6 {" U5 T$ S) K5 V# O #display map) W3 S% c, S/ L" L# R$ V
san_map
; m/ F. k% o, C" q
2 p2 s$ K& p; s: a' d" H 8. 统计每个区域的犯罪事件数目
" l) p9 ^" |. h- r! u T
# Count crime numbers in each
neighborhood
$ G" d" d) U# v5 \ disdata = pd.DataFrame(cdata[PdDistrict].value_counts())
9 V3 v: a0 d, T6 v& M7 e0 l disdata.reset_index(inplace=True)
& t4 U N$ f. c( w3 ], I8 ^2 _6 u
disdata.rename(columns={index:Neighborhood,PdDistrict:Count},inplace=True)
; F: N. \: Z1 I7 G disdata
6 C& u! M3 ]) K8 C/ _$ M
- F; U, ?( ], X! u& P) p 9. 创建Choropleth Map (颜色深浅代表各区犯罪事件数目)
" f5 |; b7 G6 I. h7 ]) L m = folium.Map(location=[37.77, -122.4], zoom_start=12)
' Y7 b: |. h% @$ w7 X; _( L folium.Choropleth(
4 E1 m% f: m6 C! c. w( F* E& n
geo_data=san_geo,
8 E$ P/ H+ }1 m/ r P% t$ {) n9 l
data=disdata,
) I. v9 T3 `5 ~- x8 X
columns=[Neighborhood,Count],
0 ?0 W% L0 l- _; B3 B; O
key_on=feature.properties.DISTRICT,
4 q, p; h9 ` [
#fill_color=red,
1 B. F N8 O3 o' \) g: G" \ fill_color=YlOrRd,
* [' h2 s/ c5 V0 d: h$ A" b& Y
fill_opacity=0.7,
1 {/ n* S: m' Q$ l
line_opacity=0.2,
" e" H5 M' B6 b/ M; J highlight=True,
% r- |2 p' H6 n7 `# H" h3 i7 s legend_name=Crime Counts in San Francisco
7 u9 d5 w3 _+ v/ [
).add_to(m)
7 m9 Y% n1 q! T. g
m
- R" o& V! f- ?7 i
4 U4 s6 l' ~+ D3 n9 r# R - y- k4 N& @# j3 U+ V- B2 \! i) K
10. 创建热力图
5 K' A5 G! S2 a from folium.plugins import HeatMap
8 H7 G& s/ t$ N- ?9 F& o+ y( T( M$ V0 H; K- b! Q( _
# lets start again with a clean copy of the map of San Francisco
8 `. w* d2 I7 ~% S9 @ san_map = folium.Map(location = [latitude, longitude], zoom_start = 12)
: T6 B8 l" k& ~4 ]- [3 _
- I+ L' h9 N2 A. t; W # Convert data format
& n: Z' n' g; W) x/ c, ]7 s" V/ ` heatdata = data[[Y,X]].values.tolist()6 [+ j) m2 @# @
0 t8 h, e. e* V' C6 L # add incidents to map; N# O! J/ f: t7 [# ?2 }! M9 Q
HeatMap(heatdata).add_to(san_map); `: f$ B. d7 E0 h7 F! K% w
0 Q; g3 i" p# B4 x san_map
6 o- b _# d' ?" }4 j8 R
1 h; W3 `( Z3 d. d7 z3 ?9 Z5 T/ Y
最后,folium还可以用来创建动态热力图,动态路径图等,具体可参考Medium上的这篇文章。
5 u- \! ]+ o R+ Q3 {( [8 S! ?5 J
实现效果如下图所示 (直接从Medium上抱过来的图,详细代码请点击上述链接)。
. h/ P; |( Q" N7 E" C1 b! L& o1 M
$ W( K8 X) p6 j3 J W
我的其他回答:
4 F0 Y. P" s& P+ k0 s* | ( b* l1 V5 O$ o: _6 I
+ T: s- I1 i) z
* q- B0 M* X/ h" q8 K% X& n 最近有小伙伴私信我推荐一些学习编程的经验,我给大家几点建议:
, ?9 G3 D: s( M( h4 R+ Q 1. 尽量不去看那些冗长的视频教程,不是说学不到知识,而是,在你学到知识之前,你可能已经睡着了 =_=
& F. J4 T$ C) _+ y
2. 不要贪多,选一个知名度高的python教程,教学为辅,练习为主。要随时记住,我们学习python的目的在于会用,而不是背过了多少知识点。
' M9 D; a8 b1 H 给大家推荐一款我超喜欢的python课程——夜曲编程。我闲没事在上面刷了一些编程题目,竟回想起当年备考雅思时被百词斩支配的恐惧。这款课程对新手小白很适合。虽然有手机app,但我建议你们用网页端学习哈。
% g9 A- P4 n/ }0 b. O6 U
最后,我的终极建议是:无论选什么教程,贪多嚼不烂,好好专注一门课程,勤加练习才是提高编程水平的王道。等基础知识学的差不多了,去Kaggle上参加几场比赛,琢磨琢磨别人的编程思路和方法就完美啦!
) z. M. t9 d+ m