Python Bokeh – Plotting glyphs over a Google Map - GeeksforGeeks

03 Jul, 2020
Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity.
Bokeh can be used to plot glyphs over a Google map. A glyph is a hieroglyphic character or a symbol. To use Google maps in Bokeh, we will use the gmap() function of the plotting class.
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
Now let us see how to configure the Google map using the GMapOptions() function :
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 :hybridroadmapsatelliteterrain
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
Import the required libraries and modules :gmap from bokeh.plottingColumnDataSource and GMapOptions from bokeh.modelsoutput_file and show from bokeh.ioCreate a file to store our model using output_file().Configure the Google map using GMapOptions().Generate a GoogleMap object using gmap().Determine the coordinates of the glyphs using ColumnDataSource().Generate the glyphs on the created Google map object.Display the Google map using show().
Import the required libraries and modules :gmap from bokeh.plottingColumnDataSource and GMapOptions from bokeh.modelsoutput_file and show from bokeh.io
gmap from bokeh.plotting
ColumnDataSource and GMapOptions from bokeh.models
output_file and show from bokeh.io
Create a file to store our model using output_file().
Configure the Google map using GMapOptions().
Generate a GoogleMap object using gmap().
Determine the coordinates of the glyphs using ColumnDataSource().
Generate the glyphs on the created Google map object.
Display the Google map using show().
# importing the required modulesfrom bokeh.plotting import gmapfrom bokeh.models import ColumnDataSource, GMapOptionsfrom bokeh.io import output_file, show  # file to save the modeloutput_file("gfg.html")  # configuring the Google maplat = 28.7041lng = 77.1025map_type = "hybrid"zoom = 11google_map_options = GMapOptions(lat = lat,                                 lng = lng,                                 map_type = map_type,                                 zoom = zoom)  # generating the Google mapgoogle_api_key = ""title = "Delhi"google_map = gmap(google_api_key,                  google_map_options,                  title = title)  # the coordinates of the glyphssource = 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 mapx = "lon"y = "lat"size = 20fill_color = "red"fill_alpha = 1google_map.square(x = x,                  y = y,                  size = size,                  fill_color = fill_color,                  fill_alpha = fill_alpha,                  source = source)  # displaying the modelshow(google_map)
Output :
Data Visualization
Python-Bokeh
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Python Dictionary
Taking input in Python
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python