Python画出好看的地图基于folium

[复制链接]
7 I' g% S' b/ ^4 J) {; d

Choropleth 等值线图

! Z2 g% U" |9 l( G9 @. D
import pandas as pd #读取数据: {9 R% ~7 [+ L; Y5 A from folium import Map,Choropleth,CircleMarker #用到的包 {3 g% k# b6 n! p$ A) a) e* u
8 d; k3 c! [4 V: H# D
#包含省的中国地图json; n+ @ L+ F4 }& s( J china_geo = fhttps://geo.datav.aliyun.com/areas_v2/bound/100000_full.json $ Y1 S# e5 J; G #读取用到的面积数据 * V! C `4 v" P datad = pd.read_csv(Desktop/square.csv,index_col=index) ( g$ I4 `! m8 g: h: T+ g7 J
( j$ K# ^& l, V: G4 w0 L6 X
) H7 K9 W: [/ Z5 T! }
m=Map(tiles=Stamen Toner) #地图风格 - Q8 e: d' L) E; f 4 ?5 L! c4 N; u- h/ r3 N: y: z Choropleth(china_geo, #选择json * M* M5 t% e6 m data = datad, #数据$ U3 J9 A% P9 j! E; ?0 m( b4 l columns = [province,square], #列,第一个为key,第二个为value - q. X5 a' X- R, J7 a- i* O4 A; P key_on = feature.properties.name,#匹配到json/ B- b5 K! y! n7 U2 k fill_color = RdPu, #颜色 5 k1 l6 h& e- X( d6 o" Y' ^' U8 u w fill_opacity = 0.8, #填充透明度0 ?2 _* B; v# |1 d) H line_opactity = 1, #线透明度 6 \7 ]. x+ V$ R, v) D3 e9 f5 Z4 s line_weight = 1, #线宽, z& Z' R- l: H legend_name = 面积 #图例4 |" @- ^' I6 b1 q$ L/ z " A6 ?- I: C9 U) F* q/ x ).add_to(m)0 ]1 f( o- f# E/ M0 J H CircleMarker(location = [39.907518, 116.397514], #坐标点 5 `1 i: R) P* a. h radius = 10, #半径6 M% g' g; ?4 X6 n, z+ Z5 D' h6 u fill = True, #填充# R. ^$ E/ b5 h popup = This is beijing, #弹窗 2 Q* W6 H: B# G3 `% H weight = 1 #circlemarker线宽 8 ~- K% C' q0 j) u1 s0 M ^ ).add_to(m)2 i4 P+ }2 ~2 R3 z# A& H: ^9 J m.fit_bounds(m.get_bounds()) * @' B* L/ J$ ~" \2 Y m- z3 v# I0 x1 p! m
$ w, o. d, X1 m1 `9 Q+ `2 C, x. S
0 \# U' {* _! K

数据地址: square.rar - 蓝奏云

/ J3 i0 {) ]& B4 L/ O g* {/ |2 z9 T

两个重要的网站

8 n: d6 S, `5 B) I% t$ b

手动绘制geojson

+ P, z8 }/ R$ v. H, E ! w7 d4 {' F) F% @

目前更新的geojson

* j$ D% }" h/ @5 R " U) |+ z" B5 c$ r& s6 R

geojson格式

! m) O7 B7 X. o% a5 h: r
{4 O' p/ G; f: } "type": "FeatureCollection", " \8 c, K, a; ^" _5 h( _ "features": [5 T5 {% @+ s# ^" z+ { {' X4 f* `$ w q+ e% a L+ | "properties": {"name": "Alabama"},: o3 h! V% a+ a; @ t "id": "AL", % @/ x# |! P+ F1 M+ V "type": "Feature", 0 i- |- z, L( ? r& v* d* d "geometry": { / W. i; Z" F! C "type": "Polygon", 4 Y% M( I6 X8 b. u* j7 k$ Q "coordinates": [[[-87.359296, 35.00118], ...]] 7 Q) Y3 `; Y4 S/ d: K4 G }+ `8 E$ m2 q- b/ A6 S# d' |- N },% ?9 F4 y# a6 i# i' G; O { + t) U2 Z4 B, }3 s "properties": {"name": "Alaska"}, 7 E, i* F8 w! x: H1 f1 [ "id": "AK",# l( t0 i A. ~# Y "type": "Feature", 5 Q( A6 J5 m* n8 a: |! X "geometry": {$ i8 ^/ K9 m0 v/ D+ _ "type": "MultiPolygon",) m2 f$ t& p n "coordinates": [[[[-131.602021, 55.117982], ... ]]] + C" K: x/ X' S7 |; r+ K4 O( @ }# [7 B$ \0 l9 m Z+ a }," A6 T% ]4 [, r2 z ... 1 g% f! i' X, [1 w# K! A! t* p# q7 d ] 5 u2 b" }2 f9 V* m4 b$ e } & z% u0 X5 c( J9 e
0 y% f3 z9 l: L! D2 P. W% \2 ^' E0 d

读取本地的json文件

, \$ O) `, x# q6 g2 l t4 ~! G0 q
f = open(zhengzhou.json) * F5 T3 p/ ]& o* F; A/ y t = json.load(f) 2 I' x2 R+ _# H
" h7 R7 i2 G/ y# }9 A9 o

读取网络json

' ^, K( X! W+ @' L
url = (0 s" O4 y! X$ m: B "https://raw.githubusercontent.com/python-visualization/folium/master/examples/data"" ?/ t/ ?3 k% [! y' U a ) % f0 O7 e- J4 |" P* V us_states = f"{url}/us-states.json"5 u7 L( y" |; r5 S1 w2 ^8 M; v3 q3 W6 q & o- i7 t2 h" t. |* m4 [. H1 ?4 ` geo_json_data = json.loads(requests.get(us_states).text) 0 P9 d3 A6 [: r5 W$ }7 ^) q1 {6 ^1 D3 z
4 H3 D2 P( A$ i$ `* m ' K# I4 h& m: Y3 G- I. y, M+ P8 N' m1 v' S( w4 l k/ v& D3 l* K 7 c0 F6 [" m# U# t: i$ [9 c# A& s" ` \6 O E8 R* v) I* S1 E
回复

举报 使用道具

相关帖子

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