【发布时间】:2019-04-16 17:39:15
【问题描述】:
将 Geopandas 与 Bokeh 结合使用,我正在可视化一个 GIS 数据集,该数据集也具有位置高程。高程值是 Double 类型,源自我正在加载的 Shapefile,例如'1382.770000'。
当向用户显示这些值时(例如,使用 HoverTool),它们以指数形式呈现,例如对于上面的例子 1.382e+3.
由于代码是面向用户/交互式的,我想以简化的浮点格式显示这些数字 - 这可以实现吗?
我试过了:
hover = HoverTool (tooltips = [('Elevation', float('@elev')])
然而这会带来一个ValueError: could not convert string to 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 进行可视化。我已按照建议使用更多信息更新了帖子