python分分鐘繪制精美地圖海報(bào)
1 簡(jiǎn)介
今天我要給大家介紹的這個(gè)Python庫(kù)prettymaps非常的有趣,基于它,我們只需要簡(jiǎn)單的代碼就可以對(duì)地球上給定坐標(biāo)和范圍的任意地區(qū)進(jìn)行地圖可視化??。

2 利用prettymaps快速制作海報(bào)級(jí)地圖
遺憾的是prettymaps暫時(shí)還不能通過(guò)pip或conda直接進(jìn)行安裝,但可以利用pip配合git從源碼倉(cāng)庫(kù)進(jìn)行安裝,對(duì)于國(guó)內(nèi)的用戶來(lái)說(shuō),可以使用下面的語(yǔ)句從github的鏡像地址快速安裝:
pip install git+https://hub.fastgit.org/marceloprates/prettymaps.git
安裝完成后,如果下面的語(yǔ)句執(zhí)行無(wú)誤,那么恭喜你已經(jīng)安裝完成:
from prettymaps import *
2.1 prettymaps的幾種使用方式
prettymaps無(wú)需用戶自行準(zhǔn)備數(shù)據(jù),會(huì)根據(jù)用戶設(shè)定的坐標(biāo)和范圍大小來(lái)自動(dòng)從OpenStreetMap上獲取相應(yīng)范圍內(nèi)的矢量數(shù)據(jù)作為繪圖素材,主要有以下幾種使用方式:
2.1.1 圓形模式
prettymaps中最簡(jiǎn)單的繪圖模式為「圓形模式」,我們只需要傳入中心點(diǎn)經(jīng)緯度坐標(biāo),以及半徑范圍(單位:米)即可,下面的例子來(lái)自官方示例程序,我將其地點(diǎn)換成以上海外灘為中心向外2500米范圍:
from prettymaps import *
from matplotlib import pyplot as plt
# 創(chuàng)建圖床
fig, ax = plt.subplots(figsize = (12, 12), constrained_layout = True)
layers = plot(
(31.23346, 121.492154), # 圓心坐標(biāo),格式:(緯度, 經(jīng)度)
radius = 2500, # 半徑
ax = ax, # 綁定圖床
layers = {
'perimeter': {}, # 控制繪圖模式,{}即相當(dāng)于圓形繪圖模式
# 下面的參數(shù)用于定義從OsmStreetMap選擇獲取的矢量圖層要素,不了解的無(wú)需改動(dòng)照搬即可
'streets': {
'custom_filter': '["highway"~"motorway|trunk|primary|secondary|tertiary|residential|service|unclassified|pedestrian|footway"]',
'width': {
'motorway': 5,
'trunk': 5,
'primary': 4.5,
'secondary': 4,
'tertiary': 3.5,
'residential': 3,
'service': 2,
'unclassified': 2,
'pedestrian': 2,
'footway': 1,
}
},
'building': {'tags': {'building': True, 'landuse': 'construction'}, 'union': False},
'water': {'tags': {'natural': ['water', 'bay']}},
'green': {'tags': {'landuse': 'grass', 'natural': ['island', 'wood'], 'leisure': 'park'}},
'forest': {'tags': {'landuse': 'forest'}},
'parking': {'tags': {'amenity': 'parking', 'highway': 'pedestrian', 'man_made': 'pier'}}
},
# 下面的參數(shù)用于定義OpenStreetMap中不同矢量圖層的樣式,嫌麻煩的直接照抄下面的官方示例即可
drawing_kwargs = {
'background': {'fc': '#F2F4CB', 'ec': '#dadbc1', 'hatch': 'ooo...', 'zorder': -1},
'perimeter': {'fc': '#F2F4CB', 'ec': '#dadbc1', 'lw': 0, 'hatch': 'ooo...', 'zorder': 0},
'green': {'fc': '#D0F1BF', 'ec': '#2F3737', 'lw': 1, 'zorder': 1},
'forest': {'fc': '#64B96A', 'ec': '#2F3737', 'lw': 1, 'zorder': 1},
'water': {'fc': '#a1e3ff', 'ec': '#2F3737', 'hatch': 'ooo...', 'hatch_c': '#85c9e6', 'lw': 1, 'zorder': 2},
'parking': {'fc': '#F2F4CB', 'ec': '#2F3737', 'lw': 1, 'zorder': 3},
'streets': {'fc': '#2F3737', 'ec': '#475657', 'alpha': 1, 'lw': 0, 'zorder': 3},
'building': {'palette': ['#FFC857', '#E9724C', '#C5283D'], 'ec': '#2F3737', 'lw': .5, 'zorder': 4},
},
osm_credit = {'color': '#2F3737'}
)
# 導(dǎo)出圖片文件
plt.savefig('上海外灘-圓形模式.png', dpi=500)

2.1.2 圓角矩形模式
除了上述的「圓形模式」之外,prettymaps中還可以使用「圓角矩形模式」,同樣需要定義中心點(diǎn)坐標(biāo)和半徑,接著為參數(shù)layers下的每個(gè)鍵值對(duì)添加鍵值對(duì){'circle': False, 'dilate': 圓角半徑}即可,其中圓角半徑為數(shù)值型,這次我們換一個(gè)地方,以故宮為例,半徑選擇600米:
# 創(chuàng)建圖床
fig, ax = plt.subplots(figsize = (12, 12), constrained_layout = True)
dilate = 100
layers = plot(
(39.91645697864148, 116.39077532493388), # 圓心坐標(biāo),格式:(緯度, 經(jīng)度)
radius = 600, # 半徑
ax = ax, # 綁定圖床
layers = {
'perimeter': {'circle': False, 'dilate': dilate}, # 控制繪圖模式,{}即相當(dāng)于圓形繪圖模式
# 下面的參數(shù)用于定義從OsmStreetMap選擇獲取的矢量圖層要素,不了解的無(wú)需改動(dòng)照搬即可
'streets': {
'custom_filter': '["highway"~"motorway|trunk|primary|secondary|tertiary|residential|service|unclassified|pedestrian|footway"]',
'width': {
'motorway': 5,
'trunk': 5,
'primary': 4.5,
'secondary': 4,
'tertiary': 3.5,
'residential': 3,
'service': 2,
'unclassified': 2,
'pedestrian': 2,
'footway': 1,
},
'circle': False, 'dilate': dilate
},
'building': {'tags': {'building': True, 'landuse': 'construction'}, 'union': False, 'circle': False, 'dilate': dilate},
'water': {'tags': {'natural': ['water', 'bay']}, 'circle': False, 'dilate': dilate},
'green': {'tags': {'landuse': 'grass', 'natural': ['island', 'wood'], 'leisure': 'park'}, 'circle': False, 'dilate': dilate},
'forest': {'tags': {'landuse': 'forest'}, 'circle': False, 'dilate': dilate},
'parking': {'tags': {'amenity': 'parking', 'highway': 'pedestrian', 'man_made': 'pier'}, 'circle': False, 'dilate': dilate}
},
# 下面的參數(shù)用于定義OpenStreetMap中不同矢量圖層的樣式,嫌麻煩的直接照抄下面的官方示例即可
drawing_kwargs = {
'background': {'fc': '#F2F4CB', 'ec': '#dadbc1', 'hatch': 'ooo...', 'zorder': -1},
'perimeter': {'fc': '#F2F4CB', 'ec': '#dadbc1', 'lw': 0, 'hatch': 'ooo...', 'zorder': 0},
'green': {'fc': '#D0F1BF', 'ec': '#2F3737', 'lw': 1, 'zorder': 1},
'forest': {'fc': '#64B96A', 'ec': '#2F3737', 'lw': 1, 'zorder': 1},
'water': {'fc': '#a1e3ff', 'ec': '#2F3737', 'hatch': 'ooo...', 'hatch_c': '#85c9e6', 'lw': 1, 'zorder': 2},
'parking': {'fc': '#F2F4CB', 'ec': '#2F3737', 'lw': 1, 'zorder': 3},
'streets': {'fc': '#2F3737', 'ec': '#475657', 'alpha': 1, 'lw': 0, 'zorder': 3},
'building': {'palette': ['#FFC857', '#E9724C', '#C5283D'], 'ec': '#2F3737', 'lw': .5, 'zorder': 4},
},
osm_credit = {'color': '#2F3737'}
)
# 導(dǎo)出圖片文件
plt.savefig('北京故宮-圓角矩形模式.png', dpi=500)

2.1.3 添加文字內(nèi)容
有了這樣美觀大方的藝術(shù)地圖,我們還可以基于matplotlib中自定義字體的方法,在地圖上添加標(biāo)注信息,仍然以上海外灘為例,我們利用外部的書法字體,在正中心繪制文字標(biāo)注信息:
import matplotlib.font_manager as fm
# 創(chuàng)建圖床
fig, ax = plt.subplots(figsize = (12, 12), constrained_layout = True)
layers = plot(
(31.23346, 121.492154), # 圓心坐標(biāo),格式:(緯度, 經(jīng)度)
radius = 2500, # 半徑
ax = ax, # 綁定圖床
layers = {
'perimeter': {}, # 控制繪圖模式,{}即相當(dāng)于圓形繪圖模式
# 下面的參數(shù)用于定義從OsmStreetMap選擇獲取的矢量圖層要素,不了解的無(wú)需改動(dòng)照搬即可
'streets': {
'custom_filter': '["highway"~"motorway|trunk|primary|secondary|tertiary|residential|service|unclassified|pedestrian|footway"]',
'width': {
'motorway': 5,
'trunk': 5,
'primary': 4.5,
'secondary': 4,
'tertiary': 3.5,
'residential': 3,
'service': 2,
'unclassified': 2,
'pedestrian': 2,
'footway': 1,
}
},
'building': {'tags': {'building': True, 'landuse': 'construction'}, 'union': False},
'water': {'tags': {'natural': ['water', 'bay']}},
'green': {'tags': {'landuse': 'grass', 'natural': ['island', 'wood'], 'leisure': 'park'}},
'forest': {'tags': {'landuse': 'forest'}},
'parking': {'tags': {'amenity': 'parking', 'highway': 'pedestrian', 'man_made': 'pier'}}
},
# 下面的參數(shù)用于定義OpenStreetMap中不同矢量圖層的樣式,嫌麻煩的直接照抄下面的官方示例即可
drawing_kwargs = {
'background': {'fc': '#F2F4CB', 'ec': '#dadbc1', 'hatch': 'ooo...', 'zorder': -1},
'perimeter': {'fc': '#F2F4CB', 'ec': '#dadbc1', 'lw': 0, 'hatch': 'ooo...', 'zorder': 0},
'green': {'fc': '#D0F1BF', 'ec': '#2F3737', 'lw': 1, 'zorder': 1},
'forest': {'fc': '#64B96A', 'ec': '#2F3737', 'lw': 1, 'zorder': 1},
'water': {'fc': '#a1e3ff', 'ec': '#2F3737', 'hatch': 'ooo...', 'hatch_c': '#85c9e6', 'lw': 1, 'zorder': 2},
'parking': {'fc': '#F2F4CB', 'ec': '#2F3737', 'lw': 1, 'zorder': 3},
'streets': {'fc': '#2F3737', 'ec': '#475657', 'alpha': 1, 'lw': 0, 'zorder': 3},
'building': {'palette': ['#FFC857', '#E9724C', '#C5283D'], 'ec': '#2F3737', 'lw': .5, 'zorder': 4},
},
osm_credit = {'color': '#2F373700'}
)
# 添加文字標(biāo)注
ax.text(
0.5, 0.5,
'外灘, 上海',
zorder = 6,
ha='center',
va='center',
fontsize=120,
fontproperties = fm.FontProperties(fname='FZZJ-HLYHXSJW.TTF'),
transform=ax.transAxes
)
# 導(dǎo)出圖片文件
plt.savefig('上海外灘-添加文字標(biāo)注.png', dpi=500)

?你可以找到你關(guān)注地點(diǎn)的經(jīng)緯度坐標(biāo),盡情地繪制出各種藝術(shù)地圖作品,譬如下面這些地標(biāo):



結(jié)尾:?
最后完整代碼已經(jīng)打包整理好了,有需要的小伙伴,可以點(diǎn)擊這行字體,要么私信小編!
以上就是本文的全部?jī)?nèi)容,歡迎在評(píng)論區(qū)與我進(jìn)行討論~
到此這篇關(guān)于python分分鐘繪制精美地圖海報(bào)的文章就介紹到這了,更多相關(guān)Python 地圖海報(bào)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
動(dòng)態(tài)規(guī)劃之矩陣連乘問(wèn)題Python實(shí)現(xiàn)方法
這篇文章主要介紹了動(dòng)態(tài)規(guī)劃之矩陣連乘問(wèn)題Python實(shí)現(xiàn)方法,較為詳細(xì)的分析了矩陣連乘問(wèn)題的概念、原理并結(jié)合實(shí)例形式分析了Python相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-11-11
python定時(shí)任務(wù)schedule庫(kù)用法詳細(xì)講解
python中有一個(gè)輕量級(jí)的定時(shí)任務(wù)調(diào)度的庫(kù)schedule,下面這篇文章主要給大家介紹了關(guān)于python定時(shí)任務(wù)schedule庫(kù)用法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
CoordConv實(shí)現(xiàn)卷積加上坐標(biāo)實(shí)例詳解
這篇文章主要介紹了CoordConv實(shí)現(xiàn)卷積加上坐標(biāo)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Python實(shí)現(xiàn)將數(shù)據(jù)庫(kù)一鍵導(dǎo)出為Excel表格的實(shí)例
下面小編就為大家?guī)?lái)一篇Python實(shí)現(xiàn)將數(shù)據(jù)庫(kù)一鍵導(dǎo)出為Excel表格的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
python使用pandas讀取json文件并進(jìn)行刷選導(dǎo)出xlsx文件的方法示例
這篇文章主要介紹了python使用pandas讀取json文件并進(jìn)行刷選導(dǎo)出xlsx文件的方法,結(jié)合實(shí)例形式分析了python調(diào)用pandas模塊針對(duì)json數(shù)據(jù)操作的相關(guān)使用技巧,需要的朋友可以參考下2023-06-06
python并發(fā)編程多進(jìn)程 模擬搶票實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了python并發(fā)編程多進(jìn)程 模擬搶票實(shí)現(xiàn)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
詳解opencv Python特征檢測(cè)及K-最近鄰匹配
這篇文章主要介紹了詳解opencv Python特征檢測(cè)及K-最近鄰匹配,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Django分頁(yè)功能的實(shí)現(xiàn)代碼詳解
在本篇文章里小編給大家整理了關(guān)于Django分頁(yè)功能的實(shí)現(xiàn)代碼以及相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們可以跟著學(xué)習(xí)參考下。2019-07-07

