python用folium繪制地圖并設(shè)置彈窗效果
python–Folium
官方說明:folium.link.
- map用于生成地圖,主要用到黃色的參數(shù)
Map(location=None, width=‘100%', height=‘100%', left=‘0%', top=‘0%', position=‘relative', tiles=‘OpenStreetMap', attr=None, min_zoom=0, max_zoom=18, zoom_start=10, min_lat=- 90, max_lat=90, min_lon=- 180, max_lon=180, max_bounds=False, crs=‘EPSG3857', control_scale=False, prefer_canvas=False, no_touch=False, disable_3d=False, jpg_enabled=False, zoom_control=True, **kwargs)
Parameters 參數(shù)說明
- location (tuple or list, default None) – Latitude and Longitude of Map (Northing, Easting).
- width (pixel int or percentage string (default: ‘100%')) – Width of the map.
- height (pixel int or percentage string (default: ‘100%')) – Height of the map.
- tiles (str, default ‘OpenStreetMap') – Map tileset to use. Can choose from a list of built-in tiles, pass a custom URL or pass None to create a map without tiles. For more advanced tile layer options, use the TileLayer class.
- min_zoom (int, default 0) – Minimum allowed zoom level for the tile layer that is created.
- max_zoom (int, default 18) – Maximum allowed zoom level for the tile layer that is created.
- zoom_start (int, default 10) – Initial zoom level for the map.
- attr (string, default None) – Map tile attribution; only required if passing custom tile URL.
- crs (str, default ‘EPSG3857') – Defines coordinate reference systems for projecting geographical points into pixel (screen) coordinates and back.定義坐標(biāo)參考系統(tǒng)
- control_scale (bool, default False) – Whether to add a control scale on the map.
- prefer_canvas (bool, default False) – Forces Leaflet to use the Canvas back-end (if available) for vector layers instead of SVG.
- no_touch (bool, default False) – Forces Leaflet to not use touch events even if it detects them.
- disable_3d (bool, default False) – Forces Leaflet to not use hardware-accelerated CSS 3D transforms for positioning (which may cause glitches in some rare environments) even if they're supported.
- zoom_control (bool, default True) – Display zoom controls on the map.
basic example
m = folium.Map(location=[45.523, -122.675], width=750, height=500) m = folium.Map(location=[45.523, -122.675], tiles='cartodb positron') m = folium.Map( location=[45.523, -122.675], zoom_start=2, tiles='https://api.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}.jpg?access_token=mytoken', attr='Mapbox attribution' ...)
Markers
m = folium.Map(location=[45.372, -121.6972], zoom_start=12, tiles="Stamen Terrain") tooltip = "Click me!" folium.Marker( [45.3288, -121.6625], popup="<i>Mt. Hood Meadows</i>", tooltip=tooltip ).add_to(m)
tooltip:點(diǎn)擊標(biāo)記后彈出的信息
popup:鼠標(biāo)放標(biāo)記上顯示的信息
標(biāo)記樣式修改
m = folium.Map(location=[45.372, -121.6972], zoom_start=12, tiles="Stamen Terrain") folium.Marker( location=[45.3288, -121.6625], popup="Mt. Hood Meadows", icon=folium.Icon(icon="cloud"), ).add_to(m) folium.Marker( location=[45.3311, -121.7113], popup="Timberline Lodge", icon=folium.Icon(color="green"), ).add_to(m) folium.Marker( location=[45.3300, -121.6823], popup="Some Other Location", icon=folium.Icon(color="red", icon="info-sign"), ).add_to(m)
標(biāo)記圓形區(qū)域
點(diǎn)擊任意位置出現(xiàn)經(jīng)緯度folium.LatLngPopup()
m = folium.Map(location=[46.1991, -122.1889], tiles="Stamen Terrain", zoom_start=13) m.add_child(folium.LatLngPopup())
打點(diǎn)功能:點(diǎn)擊任意位置出現(xiàn)標(biāo)記,再點(diǎn)擊彈出信息
m = folium.Map(location=[46.8527, -121.7649], tiles="Stamen Terrain", zoom_start=13) folium.Marker([46.8354, -121.7325], popup="Camp Muir").add_to(m) m.add_child(folium.ClickForMarker(popup="Waypoint"))
允許顯示任何HTML對(duì)象
m = folium.Map(location=[46.3014, -123.7390], zoom_start=7, tiles="Stamen Terrain") folium.Marker( location=[47.3489, -124.708], popup=folium.Popup(max_width=450).add_child( folium.Vega(vis1, width=450, height=250) ), ).add_to(m)
可以參考的代碼
html參考鏈接
實(shí)戰(zhàn)
用folium繪制中國(guó)的政策地圖,城市的政策匯總在一個(gè)excel表中。要實(shí)現(xiàn)的效果是政策可視化,點(diǎn)擊某一個(gè)城市,可以彈出這個(gè)城市所有的政策。
import folium import webbrowser as wb import numpy as np import xlrd import pandas as pd from folium import CustomIcon def get_data(): ''' return: df_data:返回城市對(duì)應(yīng)的編號(hào),經(jīng)緯度 df_policy:返回城市的政策名稱、鏈接、編號(hào) ''' file_name = 'policy.xls' all_data = xlrd.open_workbook(file_name) table = all_data.sheet_by_name('num_lat_lon') df_data = pd.DataFrame(columns=['city','number','latitude','longitude']) for i in range(1,table.nrows): df_data.loc[i] = table.row_values(i) table_policy = all_data.sheet_by_name('policy') df_policy = pd.DataFrame(columns=['number','name','link']) for i in range(1,table_policy.nrows): df_policy.loc[i,'number'] = table_policy.cell(i,3).value df_policy.loc[i,'name'] = table_policy.cell(i,1).value df_policy.loc[i,'link'] = table_policy.cell(i,2).value return df_data, df_policy # 中文轉(zhuǎn)換 def parse_zhch(s): return str(str(s).encode('ascii' , 'xmlcharrefreplace'))[2:-1] def show_map(df_data,df_policy): ''' 可以使用高德地圖或默認(rèn)地圖 m = folium.Map(zoom_start=4,zoom_control=True,tiles='http://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}',attr='default') ''' # 默認(rèn)地圖 m = folium.Map(location=[39.904,116.408],zoom_start=4,zoom_control=True,tiles='OpenStreetMap') for i in range(1,len(df_data)): # 得到該城市的編號(hào)、經(jīng)緯度 number = df_data.loc[i,'number'] latitude = df_data.loc[i,'latitude'] longitude = df_data.loc[i,'longitude'] tip = df_data.loc[i,'city'] # 獲取城市編號(hào)對(duì)應(yīng)的所有政策和鏈接的行索引 city_index = df_policy[df_policy.number == number].index.tolist() # 將城市名稱和鏈接放同一個(gè)list中 s=[] for i in city_index: s.append(df_policy.loc[i,'name']) s.append(df_policy.loc[i,'link']) # 計(jì)算最大長(zhǎng)度 length = [] for i in range(len(s)): length.append(len(s[i])) WIDTH = max(length) # 點(diǎn)擊彈出內(nèi)容和彈出框大小設(shè)置 ss='' for i in range(len(s)): ss = ss + s[i] + '</br>' pop = folium.Popup(html=ss,max_width=WIDTH*10) icon = CustomIcon(icon_image ='loc.jpg',icon_size=(20, 20)) # tooltip:懸浮彈出信息;popup:點(diǎn)擊出現(xiàn)信息 folium.Marker([latitude,longitude],icon=icon, popup=pop,tooltip=parse_zhch(tip)).add_to(m) m.save('map.html') wb.open('map.html') if __name__ == '__main__': df_data,df_policy = get_data() show_map(df_data,df_policy)
實(shí)現(xiàn)效果:
到此這篇關(guān)于python用folium繪制地圖并設(shè)置彈窗效果的文章就介紹到這了,更多相關(guān)python folium繪制地圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python語言描述機(jī)器學(xué)習(xí)之Logistic回歸算法
這篇文章主要介紹了Python語言描述機(jī)器學(xué)習(xí)之Logistic回歸算法,涉及Sigmoid函數(shù),梯度上升法等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12Python中使用__new__實(shí)現(xiàn)單例模式并解析
單例模式是一個(gè)經(jīng)典設(shè)計(jì)模式,簡(jiǎn)要的說,一個(gè)類的單例模式就是它只能被實(shí)例化一次,實(shí)例變量在第一次實(shí)例化時(shí)就已經(jīng)固定。 這篇文章主要介紹了Python中使用__new__實(shí)現(xiàn)單例模式并解析 ,需要的朋友可以參考下2019-06-06Python之根據(jù)輸入?yún)?shù)計(jì)算結(jié)果案例講解
這篇文章主要介紹了Python之根據(jù)輸入?yún)?shù)計(jì)算結(jié)果案例講解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07Python unittest discover批量執(zhí)行代碼實(shí)例
這篇文章主要介紹了Python unittest discover批量執(zhí)行代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Python中if?__name__==‘__main__‘用法詳情
這篇文章主要介紹了Python中if?__name__==‘__main__‘用法詳情,文章首先通過我們先定義一個(gè)test01.py的文件展開詳情,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2022-06-06Django項(xiàng)目創(chuàng)建的圖文教程
本文主要介紹了Django項(xiàng)目創(chuàng)建的圖文教程,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Python實(shí)現(xiàn)多子圖繪制系統(tǒng)的示例詳解
這篇文章主要介紹了如何利用python實(shí)現(xiàn)多子圖繪制系統(tǒng),文中的示例代碼講解詳細(xì),具有一定的的參考價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09python控制結(jié)構(gòu)的條件判斷與循環(huán)示例詳解
這篇文章主要為大家介紹了python控制結(jié)構(gòu)的條件判斷與循環(huán)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06