将解析后的字符串值转换为float

问题描述 投票:0回答:1

使用Geopandas和Bokeh,我可视化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

bokeh geopandas
1个回答
1
投票

您可以使用这样的工具提示格式(Bokeh v1.1.0):

hover = HoverTool(tooltips = [ ('Climate Zone', '@cZone'),
                               ('County', '@Name'),
                               ('USAF', '@USAF'),
                               ('elev', '@{elev}{%0.3f}') ],
                  formatters = {'elev' : 'printf'} )

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.