Python Bokeh – 在谷歌地图上绘制字形
Bokeh 是一个Python交互式数据可视化。它使用 HTML 和 JavaScript 渲染其绘图。它针对现代 Web 浏览器进行演示,提供具有高性能交互性的新颖图形的优雅、简洁构造。
Bokeh 可用于在 Google 地图上绘制字形。字形是象形文字字符或符号。要在 Bokeh 中使用 Google 地图,我们将使用plotting
类的gmap()
函数。
地图()
Syntax : gmap(parameters)
Parameters :
- google_api_key : key of the Google API
- map_options : GMapOptions i.e. the configuration specific to the Google map
- active_drag : drag tool that is to be initially active
- active_inspect : inspect tool that is to be initially active
- active_scroll : scroll tool that is to be initially active
- active_tap : tap tool that is to be initially active
- title : title of the Google map
- tools : tools the plot should start with
- x_axis_label : label for the x-axis
- x_axis_location : location of the x-axis
- x_minor_ticks : number of minor ticks between adjacent x-axis major ticks
- y_axis_label : label for the y-axis
- y_axis_location : location of the y-axis
- y_minor_ticks : number of minor ticks between adjacent y-axis major ticks
Returns : an object of the class GMap
现在让我们看看如何使用GMapOptions()
函数配置 Google 地图:
GMapOptions()
Syntax : GMapOptions(parameters)
Parameters :
- lat : latitude where the Google map is to be centered
- lng : longitude where the Google map is to be centered
- map_type : type of map to be used for the plot, here are the map types available :
- hybrid
- roadmap
- satellite
- terrain
- name : name for the Google map
- scale_control : whether the Google map should display its distance scale control
- styles : style of map to be used for the plot
- subscribed_events : list of events that are subscribed to by Python callbacks
- tags : tags associated with the Google map model
- tilt : tilting the angle of the map
- zoom : initial zoom level of the Google map
- apply_theme : theme of the model
Returns : an object of the class GMapOptions
让我们看看如何在 Google Map 上绘制字形:
- 导入所需的库和模块:
- 来自 bokeh.plotting 的 gmap
- bokeh.models 中的 ColumnDataSource 和 GMapOptions
- output_file 并从 bokeh.io 显示
- 使用
output_file()
创建一个文件来存储我们的模型。 - 使用
GMapOptions()
配置 Google 地图。 - 使用
gmap()
生成一个 GoogleMap 对象。 - 使用
ColumnDataSource()
确定字形的坐标。 - 在创建的 Google 地图对象上生成字形。
- 使用
show()
显示谷歌地图。
# importing the required modules
from bokeh.plotting import gmap
from bokeh.models import ColumnDataSource, GMapOptions
from bokeh.io import output_file, show
# file to save the model
output_file("gfg.html")
# configuring the Google map
lat = 28.7041
lng = 77.1025
map_type = "hybrid"
zoom = 11
google_map_options = GMapOptions(lat = lat,
lng = lng,
map_type = map_type,
zoom = zoom)
# generating the Google map
google_api_key = ""
title = "Delhi"
google_map = gmap(google_api_key,
google_map_options,
title = title)
# the coordinates of the glyphs
source = ColumnDataSource(
data = dict(lat = [28.6, 28.65, 28.7, 28.75, 28.8, 28.85],
lon = [76.95, 77, 77.05, 77.1, 77.15, 77.25]))
# generating the glyphs on the Google map
x = "lon"
y = "lat"
size = 20
fill_color = "red"
fill_alpha = 1
google_map.square(x = x,
y = y,
size = size,
fill_color = fill_color,
fill_alpha = fill_alpha,
source = source)
# displaying the model
show(google_map)
输出 :