Python 如何画出漂亮的地图?

[复制链接]
7 ^9 K T# k! Q9 U7 i2 H
7 n3 k3 S+ R3 S

推荐一个超好用的python包folium, 专门用于地理数据可视化,官方英文教程教程点击这里

6 n( W6 X t" }# v0 D
5 f( S% v8 E$ [) q$ N2 w

使用方法很简单,操作如下:

导入包,创建一副世界地图6 I& `+ w) s5 u p
import folium * M! @, e& X9 u {/ q! @ import pandas as pd 7 y% m1 H( D" P: ? ( u6 X# y5 ?" Z6 \/ @# H# s # define the world map ! P+ C4 h8 L0 {" W- f5 G& t* a* ^ world_map = folium.Map(); P, U. d1 z, o# N- `& u( ]4 c5 h+ [ 7 ?, f) y, f! ] # display world map9 L( w( l9 Z% |1 [" t world_map
2 s! ]7 {& L; K) A/ ]& C- U
7 ~- s: `$ \& _. _' x8 T

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

7 Y- {- r3 O- i P" C* L
# San Francisco latitude and longitude values , _) e+ ~. ^5 N2 h latitude = 37.77 5 [( I; x0 s/ }' a( |" J4 v longitude = -122.42+ u' A5 P- G p! O2 v4 X1 S& \' k 8 @2 z* w# p, R # Create map and display it & W. D! r- B$ G san_map = folium.Map(location=[latitude, longitude], zoom_start=12) + O% E$ o% i+ V4 Q/ ?9 J' g3 D: z7 e 8 J) Y7 j" P6 N( S" H$ { # Display the map of San Francisco 4 p/ }; N" P. ?: k" w san_map
) b/ @ D* B; ]! G
( U$ B- D! B3 _3 r8 u

更改地图显示,默认为OpenStreetMap风格,我们还可以选择Stamen Terrain, Stamen Toner等。

" J) ?4 D. v2 F+ J3 E4 E
# Create map and display it# \* n3 g' b* _2 ] san_map = folium.Map(location=[latitude, longitude], zoom_start=12,tiles=Stamen Toner)
* E, }2 A1 @" i2 R
2 P5 v h6 _: |1 e* _& }

3. 读取数据集(旧金山犯罪数据集)

; |3 {! n3 n0 e! J/ k1 }
# Read Dataset' y* N, o9 D$ V4 t cdata = pd.read_csv(https://cocl.us/sanfran_crime_dataset)' l5 L# ^: m, }, } cdata.head()
& g. H5 S% Q4 H- f
, M; Y: p4 i/ D

4. 在地图上显示前200条犯罪数据

8 @+ P( A ]8 M3 X9 u
# get the first 200 crimes in the cdata 4 q. z1 H* ^" S( ` limit = 200% ^/ _# |3 n" |7 \5 h V( O data = cdata.iloc[0:limit, :] * `5 Z; a; O2 N# O1 ~: g `1 m # C' y' ^% X2 f0 u8 J8 B # Instantiate a feature group for the incidents in the dataframe 6 I6 ]! b5 X5 Z1 p3 E5 w7 U incidents = folium.map.FeatureGroup() - q1 \' @" t+ i. T Z6 e3 h( Y8 u. \: R # Loop through the 200 crimes and add each to the incidents feature group8 _( \9 C7 H y8 s2 U! [/ j for lat, lng, in zip(cdata.Y, data.X):/ Z& @! q& W' M- r: e: H incidents.add_child(, ]: P7 ~4 f' g" D0 A2 i folium.CircleMarker(: P; u1 |# I/ m( p2 l7 n9 T+ B' s# g [lat, lng], + [1 k. E" E5 z* s) } radius=7, # define how big you want the circle markers to be) k0 ?; j. u! [0 x* l" x2 k color=yellow,8 V: `8 L: @+ r6 v fill=True, 8 @6 J: z( i( E7 L3 @1 E" Z2 D. R% D fill_color=red, + V- k: j* T8 z' J/ L; c5 P fill_opacity=0.4% ]9 p6 g+ }3 ?6 M ) 0 R" ~# e4 Q1 R* R w. B: a )( q' S/ [/ U% P' }- T5 R+ h + {# \6 Y1 B+ L" Y; B: f # Add incidents to map , r7 X1 e) v$ @1 k san_map = folium.Map(location=[latitude, longitude], zoom_start=12) - t6 N s) D% N! j0 ]* A& u3 ] n san_map.add_child(incidents)
& k7 y- V! m2 I5 f0 f1 W) q8 h
, E- j1 r2 b4 Y- [1 w7 z* j

5. 添加地理标签

: W& ?: T8 I! `
# add pop-up text to each marker on the map ) r& @3 H' U9 ^( R latitudes = list(data.Y) ) u f; q/ {2 k; i) q longitudes = list(data.X)$ O' {# ] ?1 V' g9 x' S2 o3 F labels = list(data.Category); o7 \3 I$ ?" t0 K : \. `( A! g3 @& X& [3 O for lat, lng, label in zip(latitudes, longitudes, labels):+ O! M, p% {. {& a" O: ~ folium.Marker([lat, lng], popup=label).add_to(san_map) ; k* a7 g, Y1 Y! S l3 f+ ~ ( p8 B5 s# n% I! {6 W" J # add incidents to map7 _9 b% T4 [$ t* ~ san_map.add_child(incidents)
! s, j1 i1 ?/ B9 W1 E
- \! h( B2 o2 ]8 m3 B& f

6. 统计区域犯罪总数

7 a) ?' L6 r. c8 P7 B
from folium import plugins 8 w5 S5 r Q: u0 ^4 t7 I; p4 }4 Q% }$ E# K3 j- x6 b7 J0 @* [$ O # lets start again with a clean copy of the map of San Francisco + ]( a/ `& B$ y* |. [ san_map = folium.Map(location = [latitude, longitude], zoom_start = 12)* Q. M# A, W( `, }4 `/ P+ {* L5 p : n! y- W! W1 X1 p0 y& ^ # instantiate a mark cluster object for the incidents in the dataframe: t. t8 F2 h3 V) A) j. ^9 X0 W incidents = plugins.MarkerCluster().add_to(san_map) 5 }4 o* B# `% ?& Y, W% f - y3 U( e# Q4 d% b( _3 x # loop through the dataframe and add each data point to the mark cluster / }) `/ a* o. H- x, P) A for lat, lng, label, in zip(data.Y, data.X, cdata.Category): 2 l! A/ ]8 z/ n. P$ Y folium.Marker( 3 N/ ^. h5 Z$ \ location=[lat, lng],* X* @( e4 L; z5 l8 E! c8 k icon=None,( d0 ?0 E8 {! o7 e popup=label, + ^# y3 v. }; f: r+ V ).add_to(incidents)* e3 l9 b" B, Y5 h( ]# ~ 7 x: p7 d( R; B3 ` n # add incidents to map8 E1 L3 A: D8 `, j$ i' k8 j/ A san_map.add_child(incidents)
9 _" I$ z5 ~! W: Y
* ~8 t/ \3 S% |5 ]: E8 ^; Y9 q' R9 j

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

! R' e6 z/ H Z9 p
import json& R5 X2 E; o& y7 X$ \ import requests 6 S; @" Y) T9 \$ z# r6 D# z & D/ \ T( r- h/ x3 ` url = https://cocl.us/sanfran_geojson+ S; w; e z, o. B0 v san_geo = f{url}8 V P) ~2 C& ?( ?5 r* \& F san_map = folium.Map(location=[37.77, -122.4], zoom_start=12); E; c5 @: @5 _3 @# n# V folium.GeoJson(! Q4 q% S9 M5 k7 l san_geo,$ R' J7 {% G- X( _, k2 |5 h style_function=lambda feature: { $ W; {4 r! }% k+ A fillColor: #ffff00, 0 E6 `* z: W6 ~( }- \8 p4 W F5 A color: black,7 m% i% [' N, @ weight: 2,) s% i9 I. x9 ^& A0 w dashArray: 5, 50 x; I R& o6 T6 F, { }4 ^+ ~* X/ P5 q; m" e& o ).add_to(san_map)' r! q/ U6 p s! q. F4 L" C ( Y5 z. n- W/ w8 e+ M #display map9 Z8 r( V7 x) y+ V: s* j san_map
2 b4 N4 A _. Q% V! b" X* S
6 h. i, t1 M/ |& X% }5 l' ~9 t! V

8. 统计每个区域的犯罪事件数目

1 i1 P8 y! x4 |; G
# Count crime numbers in each neighborhood" V5 d( L" ?0 S$ k/ @: n# F disdata = pd.DataFrame(cdata[PdDistrict].value_counts()) # J* U) G3 q, x. e+ |" L- f disdata.reset_index(inplace=True) 0 o+ N2 k2 @0 W disdata.rename(columns={index:Neighborhood,PdDistrict:Count},inplace=True) . y$ X% u" K- F8 E disdata
9 S3 s% \8 p. M' x" }) E
% _4 |( _+ s; t& _4 g- A. w

9. 创建Choropleth Map (颜色深浅代表各区犯罪事件数目)

( ], m+ h1 ~, t% R$ A
m = folium.Map(location=[37.77, -122.4], zoom_start=12)" y: W k" F+ b* D folium.Choropleth(2 p/ Z& C5 O; a- I, K2 t" V geo_data=san_geo, & [" `5 c! K: D" m- [ X data=disdata,0 ~0 A% q+ w% y- x4 E columns=[Neighborhood,Count], - s: T9 f& o- p3 R; Q- d) m+ Z5 @( R key_on=feature.properties.DISTRICT,9 k1 {+ T+ L8 M4 U0 R #fill_color=red, " g+ i2 g& u6 s fill_color=YlOrRd, 0 m2 P4 {" N/ G! }+ _ |5 B4 Y fill_opacity=0.7, L% ~2 ?& L+ z6 ^# Y( J line_opacity=0.2, 0 Z7 @* r7 a* U( c8 c" f highlight=True,/ b% \6 h- H- b legend_name=Crime Counts in San Francisco2 V$ r+ m6 I" o2 {# _; Z1 D ).add_to(m) ( ?. h0 \5 Q" S y) i$ f m% ?) u/ k: ^3 M7 |( Q, q
( R/ C6 d. X8 f4 d- {
1 v. s0 \* U- f p) w H

10. 创建热力图

% T" Z7 U; p( X/ r
from folium.plugins import HeatMap 5 O+ |6 `9 [/ ~7 ?0 }$ @2 B 5 }0 j( T3 P5 h) W, `" {8 l # lets start again with a clean copy of the map of San Francisco 8 K( _6 B: f9 ]' A) d" y san_map = folium.Map(location = [latitude, longitude], zoom_start = 12) 6 ^' a3 X5 o$ {5 r0 ?! y/ \# M! u# o8 T! g) Q # Convert data format% n, i% m7 i& a" Y7 k heatdata = data[[Y,X]].values.tolist() ) S5 ]9 J6 s" Z- J* x1 p 1 G- Y5 y- t! d. h9 |5 F8 F # add incidents to map. }0 @" W/ g. P$ I- Q% `* Z HeatMap(heatdata).add_to(san_map) ) Y9 y: h" O5 y0 o 2 [2 Z5 P/ |0 q% E( [/ g/ T( j( V san_map
: d" L# v5 J8 |
]8 T0 k" G1 O w! i% @* E9 h

最后,folium还可以用来创建动态热力图,动态路径图等,具体可参考Medium上的这篇文章。

" d1 V3 [' H( P& S; X6 d

实现效果如下图所示 (直接从Medium上抱过来的图,详细代码请点击上述链接)。

- c) g: z' k& p C
9 `# }& ~5 P8 f. v7 l" i' J

我的其他回答:

( l0 v' F, Y* M; b, P5 t + l, `/ n! @1 V& n9 f6 L 9 ~% c/ ~( A1 z/ v9 i 3 H; \+ F* }+ T X/ K

最近有小伙伴私信我推荐一些学习编程的经验,我给大家几点建议:

( W' \$ ~ a- {

1. 尽量不去看那些冗长的视频教程,不是说学不到知识,而是,在你学到知识之前,你可能已经睡着了 =_=

; i* r% Z7 k5 F$ i

2. 不要贪多,选一个知名度高的python教程,教学为辅,练习为主。要随时记住,我们学习python的目的在于会用,而不是背过了多少知识点。

4 v8 z/ e$ D4 |2 Y6 _

给大家推荐一款我超喜欢的python课程——夜曲编程。我闲没事在上面刷了一些编程题目,竟回想起当年备考雅思时被百词斩支配的恐惧。这款课程对新手小白很适合。虽然有手机app,但我建议你们用网页端学习哈。

* y0 R x" M& T; t

最后,我的终极建议是:无论选什么教程,贪多嚼不烂,好好专注一门课程,勤加练习才是提高编程水平的王道。等基础知识学的差不多了,去Kaggle上参加几场比赛,琢磨琢磨别人的编程思路和方法就完美啦!

5 R6 ~) [3 w" n6 T
4 i5 ?, u# G- T! L0 A 6 ?3 L5 U1 m- @2 y& i * j0 M- P7 c$ ~4 g 2 [. D p% m6 @1 t) g5 m9 `; X- z$ f% B" L+ c* {4 w8 r8 q
回复

举报 使用道具

相关帖子

全部回帖
暂无回帖,快来参与回复吧
懒得打字?点击右侧快捷回复 【吾爱海洋论坛发文有奖】
您需要登录后才可以回帖 登录 | 立即注册
风亥
活跃在7 天前
快速回复 返回顶部 返回列表