我将Bokeh与Geopandas结合使用,使可视化的GIS数据集也具有位置高程。高程值是Double类型,源自于我正在加载的Shapefile,例如'1382.770000'。
在向用户显示这些值时(例如,使用HoverTool),它们以指数形式显示,例如对于1.382e + 3以上的示例。
由于代码是面向用户的/交互式的,我想以简化的浮点格式显示这些数字-可以实现吗?
我尝试过:
hover = HoverTool (tooltips = [('Elevation', float('@elev')])
但是这带来了 ValueError:无法将字符串转换为float:'@elev'
代码示例:
import pandas as pd
import geopandas as gpd
import json
import bokeh.io
from bokeh.io import output_notebook, show, output_file
from bokeh.plotting import figure, show
from bokeh.models import GeoJSONDataSource, HoverTool
# prevent Bokeh from savig sketch into file / opening in a new tab
bokeh.io.reset_output()
bokeh.io.output_notebook()
shapefile = 'data/USA Counties 20m/cb_2017_us_county_20m_with_cZone_USAF_coordinates_elevations.shp'
#Read shapefile using Geopandas
gdf = gpd.read_file(shapefile)[['CZONE', 'NAME', 'geometry','USAF','xcoord','ycoord','ELEV_IN_M']]
#Rename columns.
gdf.columns = ['cZone', 'Name', 'geometry','USAF','Long','Lat','elev']
#Reset index
gdf = gdf.reset_index(drop=True)
#Read data to json.
json_raw = json.loads(gdf.to_json())
#Convert to String like object.
json_data = json.dumps(json_raw)
#Input GeoJSON source that contains features for plotting.
geosource = GeoJSONDataSource(geojson = json_data)
#Add hover tools
hover = HoverTool(tooltips = [ ('Climate Zone','@cZone'),
('County','@Name'),
('USAF','@USAF'),
('elev','@elev')])
#Create figure object.
p = figure(title = 'USA Climate Zone Map by County', plot_height = 450 , plot_width = 800, toolbar_location = "below")
p.add_tools(hover)
#Add patch renderer to figure.
p.patches('xs','ys', source = geosource,
line_color = 'black', line_width = 0.25, fill_alpha = 1)
# Display figure & widget
show(p)
可以访问shapefile here