$ O& z( B5 G$ \6 u; B: `7 ] N0 K Choropleth 等值线图. C3 Q; m# e- r9 u5 y
import pandas as pd #读取数据
1 @) |. P( j4 {+ _4 g; K9 L0 j from folium import Map,Choropleth,CircleMarker #用到的包
/ ~3 t0 P+ W0 n
8 i8 X- y$ k; A! M7 | #包含省的中国地图json
: Y% v; j( Q7 T2 [! F china_geo = fhttps://geo.datav.aliyun.com/areas_v2/bound/100000_full.json
) }3 o/ A4 Z# N; V0 J4 o #读取用到的面积数据
! O2 |! r+ T) V8 r datad = pd.read_csv(Desktop/square.csv,index_col=index)
% Q# e0 d0 h: o, }$ j
: R: K: z! V! I0 ^& J ( _5 U* Y) g( b0 I% N0 L7 V# J
m=Map(tiles=Stamen Toner) #地图风格
, ~5 b" |; h, g/ y
/ K- a1 @0 E- V# H$ G Choropleth(china_geo, #选择json( t$ t9 b3 J* ?! m
data = datad, #数据7 E( S% d4 w" t, Y& t
columns = [province,square], #列,第一个为key,第二个为value
7 H* j& N4 ^5 k5 @2 X& G* {! h key_on = feature.properties.name,#匹配到json- M) q4 J, E, ~5 b; P
fill_color = RdPu, #颜色
' s2 ]" I% J, h9 n7 z fill_opacity = 0.8, #填充透明度 P# t! j) } V- s
line_opactity = 1, #线透明度
9 z4 t: f8 U6 [. M line_weight = 1, #线宽
: Z2 X" x6 m% W% H O( F& S legend_name = 面积 #图例
2 [ Q7 t: \$ k. ^1 i5 q& x, [* V; t( a8 t0 Z5 V
).add_to(m)5 {( e+ h$ I$ c! k
CircleMarker(location = [39.907518, 116.397514], #坐标点2 Y7 Z; z. X4 ?- D- \4 B
radius = 10, #半径- D {( C: J2 t* O# ~- [$ T
fill = True, #填充
! w, | r7 s& N" D" ]% s( U3 } popup = This is beijing, #弹窗# X* O+ C2 d0 G! Z; c! b$ @" f; y4 Y
weight = 1 #circlemarker线宽 3 E! n7 P* O. K H M
).add_to(m)- _# G" ^ I1 e, r' q, u
m.fit_bounds(m.get_bounds())9 s6 g4 P& W% x5 F7 N
m
2 b) ]! c) [. p% C ) O' G& u& R) e% A7 B
1 O1 J+ d& F' |! ~1 f/ g* l 数据地址: square.rar - 蓝奏云
! b3 ^7 F* n. z8 E5 Y 两个重要的网站 9 ?) }- e1 R0 b( b: o, V* D0 i% u
手动绘制geojson
' A9 V0 k' T0 X. a5 y- \" @- _ ( k( E" p0 m* Y
目前更新的geojson
* g# B3 A4 L8 B* e4 z: P! M ( H T3 [. A+ l& F
geojson格式 4 c0 w R( _. l+ V
{4 X+ f ~1 A) X+ M: B& W. X
"type": "FeatureCollection",% W3 b2 C" l# V$ G, j' T, T
"features": [% F4 [3 j }" G" [
{
& D) a* ]. h. T4 c5 J! V5 Z$ f' S7 n "properties": {"name": "Alabama"},
# O& o, f, q$ _% p- W( T "id": "AL",+ L: ]4 x* B1 Z- H
"type": "Feature",
; g# g2 B" _" B. u6 a/ @8 a "geometry": {, U- u6 Y. s3 A0 _2 ]; j0 O
"type": "Polygon",2 q* U1 l3 [; C! m6 ~
"coordinates": [[[-87.359296, 35.00118], ...]]
9 D- W1 a( h" P; g# G; ?6 O, | }
6 [0 G: J. ~, }6 [/ b: ]1 n; ?# k },
2 I1 [; ]1 i* t' \. J( @ {! ~( d2 m2 }$ l0 F- Z; k3 K3 @6 `
"properties": {"name": "Alaska"},
8 u9 x" ~3 N$ M' G" y! W7 \) Z* B "id": "AK",# }# r7 e& w. F7 I/ T) y8 Q+ f/ Y
"type": "Feature",
/ S. m3 C$ G/ @+ M- m6 R" ` "geometry": {
# t. M) y9 m5 K9 h "type": "MultiPolygon",0 f2 j ]0 @* K( m. l
"coordinates": [[[[-131.602021, 55.117982], ... ]]]% x# `+ k/ P4 Y. a, I0 R
}
& o+ Z+ l2 [" O; H },5 v1 f5 w7 c+ K6 }# J2 G4 Z8 q
...
+ H G8 `0 E( d7 \6 _) R ] T3 h0 M& M$ y
}
* Z: O1 g/ P: f. [: S! \8 t0 M' ?
1 b9 t; W9 j; S, Y! v! _2 X 读取本地的json文件
4 j" i; N# c5 w) w6 U+ y6 t f = open(zhengzhou.json)1 b: F4 ^7 E0 l- \
t = json.load(f)
7 `3 c: H0 s+ L% {+ W
0 W" {0 M: R8 y) c 读取网络json N" }6 O7 f& I1 O
url = (& f& {4 J& q R2 D# c2 y7 l
"https://raw.githubusercontent.com/python-visualization/folium/master/examples/data"6 Z& `1 B, E4 _6 w" O% `! O
): Z& n. e8 e8 z* r
us_states = f"{url}/us-states.json"& b/ h8 t9 Z( f2 C# u
' i6 M0 G" E; ]# S geo_json_data = json.loads(requests.get(us_states).text)* [* u5 i d- k+ q+ N. l [& F
9 H$ H6 y( u- v
P3 q0 z# x m
4 D! A: [ M$ ?% z) b! e/ e- j
$ ~6 {; e$ Y) D
, h" Z* I3 C4 l S |