; @0 W7 v; I3 u3 c( X Choropleth 等值线图
0 n3 A- w8 v' V: P8 g% ?# c$ j/ k import pandas as pd #读取数据
/ ]' y" \/ _# N0 L0 O2 L- W from folium import Map,Choropleth,CircleMarker #用到的包/ }; V& b3 t" i: `: @. y5 \
V9 k: X; O" R( u1 F& E* P
#包含省的中国地图json
6 H( G2 ]: e6 {/ _; Q china_geo = fhttps://geo.datav.aliyun.com/areas_v2/bound/100000_full.json& J/ j% n, T T B( I! ?
#读取用到的面积数据 v7 E/ b: ^- Q5 o8 Q5 I K- X
datad = pd.read_csv(Desktop/square.csv,index_col=index)# `" v' p9 }4 {3 X; R
5 E% G) S# d! ?- R# |6 Q# A
5 {3 F/ o' E5 Y; h m=Map(tiles=Stamen Toner) #地图风格; T2 C3 x1 X( L; n8 f+ g2 B \+ \
, t, T9 }, G; Z2 f: m Choropleth(china_geo, #选择json
# P s- P! n. h* ? data = datad, #数据
$ [: }& g: a+ Q* {! A9 a: q7 w5 L5 L columns = [province,square], #列,第一个为key,第二个为value
4 V: X+ u% t- K) t* s1 k key_on = feature.properties.name,#匹配到json) K8 O8 m3 i( h8 h8 p
fill_color = RdPu, #颜色( {! }; T( f( E# {& ^' H6 |
fill_opacity = 0.8, #填充透明度, v/ j/ ] _, I
line_opactity = 1, #线透明度
* t8 _8 n3 l1 L4 n, x, b line_weight = 1, #线宽
$ e& y! ^4 G/ V: c( A: k legend_name = 面积 #图例
7 c, Z- a1 J' u& x( M- ?7 D/ p9 L! L Z' n
).add_to(m)
* k1 h) J3 E" r y7 d CircleMarker(location = [39.907518, 116.397514], #坐标点! Z! s( ^2 D1 U; b
radius = 10, #半径7 l" z, J9 q2 g* M# x, G
fill = True, #填充* ~) W! c8 A* E2 C) e# ~
popup = This is beijing, #弹窗5 n: y i* C i: C
weight = 1 #circlemarker线宽
( e5 c/ _, F9 T: \% Z+ M( @: W ).add_to(m)/ a6 R1 l, N; @& t
m.fit_bounds(m.get_bounds())
! W& @* j( s& t2 z' s! _! i m
: V; h5 s; e0 h
+ S& k: O& O: N/ W& J' t+ d
1 S6 H; C5 J6 N/ E2 A+ X8 x# X3 a 数据地址: square.rar - 蓝奏云
4 P: F5 u5 v7 }& @2 w/ b7 x W 两个重要的网站
8 y8 I) C7 ~( z2 M. x1 o& ? 手动绘制geojson $ I! @7 ^- s l3 U4 s; A0 a$ V
, g- a, ]* a, R) @ 目前更新的geojson * v, R5 R$ K' m K( f6 J4 o1 L& ^) R$ n
! E. t' `8 y. C0 |6 U- _4 B- [ geojson格式
) Y, J6 l- n0 m% t2 f+ [ {
+ v4 w# ?6 c* W4 F! y "type": "FeatureCollection",
6 [' y& Y5 J( g0 B p9 U "features": [
* i& q+ z6 ?: z# o; \ {1 H3 K0 n9 _9 O: ~2 e2 o+ ]
"properties": {"name": "Alabama"}," W* T( M) T, ~* X4 f D T# K
"id": "AL",
' Z3 o" Y0 V4 N0 B: k* n "type": "Feature",; D* d; f( e, H; H# Y
"geometry": {* d$ [9 W& R2 W5 n0 Z( }1 y5 ]1 n
"type": "Polygon",5 J$ C- `3 N1 t0 {8 ?
"coordinates": [[[-87.359296, 35.00118], ...]]
7 g4 F; `4 W4 M' c s }
6 h9 O, k4 l) b5 _6 b: S0 X9 Z },
# ^! |' W! D- ^0 U {4 n1 J% r6 N& _( P( \6 L$ t1 b5 X5 s
"properties": {"name": "Alaska"},/ h2 ~* O1 K# t7 i% c8 i9 u& [
"id": "AK",: y' C1 h6 O* P/ }3 \
"type": "Feature",
0 T! y( V3 q$ {6 }, | e7 a "geometry": {
7 E, k' I* g% C+ y( K+ L5 D "type": "MultiPolygon",! Q3 d7 g& d( ^! d
"coordinates": [[[[-131.602021, 55.117982], ... ]]]/ f; w9 |# n2 A5 c
}
0 ~% ^7 L$ n4 B8 x0 n },5 a, w8 w' |( v4 B: l7 d
...
9 T8 K8 j. P O6 ~0 h ]
8 }2 o3 k# O+ J$ s( b! T }% U) O0 P$ V+ i9 ] ^
& U* u; d) ]7 v' Q
读取本地的json文件
9 J+ Q8 Y0 r8 }" E8 [" @$ s f = open(zhengzhou.json)
1 w+ v5 D* b4 ~- |& h t = json.load(f)
7 P4 }2 f% ]; W7 A0 q, R" Y
! h B; p% v* z# I1 @ 读取网络json
) Z% j" Y( v, g* o( |6 s url = (
7 p3 v _5 U$ h. n' b! B( B. j9 S "https://raw.githubusercontent.com/python-visualization/folium/master/examples/data"* g! e' u7 W+ s* z0 J* I( Z K
). S& _0 _* }) Z
us_states = f"{url}/us-states.json"8 l$ T5 _* i+ E" \( K; s: x
4 {& j6 S p. ^: C+ H
geo_json_data = json.loads(requests.get(us_states).text)8 ?6 A) U; w1 ]5 D5 x9 `; f& S
+ [ B f% [% a- D* J5 C/ k. `& }1 A8 C1 u1 F6 \( n4 T
* P+ @$ l5 t5 m- i& S8 u) S$ t7 n; \" m+ J c( `5 M
3 h3 {8 d. t& b. L* }6 R) u% P |