如何用散景绘制形状多边形?
How to plot Shapely polygon with Bokeh?
我想用散景绘制存储在地图上的 GeoDataFrame 中的形状优美的多边形。
1. 选择什么类型的字形来绘制多边形?
2.如何将数据传给Glyph?
我正在尝试通过以下方式进行:
from bokeh.models import GMapOptions, PanTool, WheelZoomTool,
WheelPanTool, BoxSelectTool, BoxEditTool
from bokeh.plotting import gmap
p = gmap(api_key, map_options, title= f'offer {str(sales_id)} ')
map_options = GMapOptions(lat = lats_s, lng = lons_s,
map_type="roadmap", zoom=12)
api_key = 'my_api_key'
x, y = some_shapely_polygon.exterior.coords.xy
x = x.tolist()
y = y.tolist()
source = ColumnDataSource(data=dict(
x=x, y=y,))
p.patches('x', 'y', source=source,
fill_alpha=0.8, line_color="black", line_width=0.3)
show(p)
我收到一个错误:
"Javascript Error: Invalid array length"
当我用 Circles 向儿子传递其他数据时一切正常,我无法绘制 Ploygons。
谢谢!
这些示例对我有用。将 map.shp 替换为您的文件名。适用于 Bokeh v1.0.4。 运行 与:python map.py
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
import geopandas as gp
import shapely
sf = gp.read_file('map.shp')
x, y = [], []
[(x.append(list(polygon.exterior.coords.xy[0])), y.append(list(polygon.exterior.coords.xy[1]))) for polygon in sf['geometry'] if type(polygon.boundary) == shapely.geometry.linestring.LineString ]
p = figure(title = "A map from Shapefile", plot_width = 800)
p.patches('x', 'y', source = ColumnDataSource(dict(x = x, y = y)), line_color = "white", line_width = 0.5)
show(p)
或
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
import geopandas as gp
def getPolyCoords(row, geom, coord_type):
if coord_type == 'x':
return list(row[geom].exterior.coords.xy[0])
elif coord_type == 'y':
return list(row[geom].exterior.coords.xy[1])
gdf = gp.GeoDataFrame.from_file('map.shp')
gdf['x'] = gdf.apply(getPolyCoords, geom = 'geometry', coord_type = 'x', axis = 1)
gdf['y'] = gdf.apply(getPolyCoords, geom = 'geometry', coord_type = 'y', axis = 1)
p_df = gdf.drop('geometry', axis = 1).copy()
p = figure(title = "A map from Shapefile", plot_width = 800)
p.patches('x', 'y', source = ColumnDataSource(p_df), line_color = "white", line_width = 0.5)
show(p)
我想用散景绘制存储在地图上的 GeoDataFrame 中的形状优美的多边形。 1. 选择什么类型的字形来绘制多边形? 2.如何将数据传给Glyph?
我正在尝试通过以下方式进行:
from bokeh.models import GMapOptions, PanTool, WheelZoomTool,
WheelPanTool, BoxSelectTool, BoxEditTool
from bokeh.plotting import gmap
p = gmap(api_key, map_options, title= f'offer {str(sales_id)} ')
map_options = GMapOptions(lat = lats_s, lng = lons_s,
map_type="roadmap", zoom=12)
api_key = 'my_api_key'
x, y = some_shapely_polygon.exterior.coords.xy
x = x.tolist()
y = y.tolist()
source = ColumnDataSource(data=dict(
x=x, y=y,))
p.patches('x', 'y', source=source,
fill_alpha=0.8, line_color="black", line_width=0.3)
show(p)
我收到一个错误: "Javascript Error: Invalid array length"
当我用 Circles 向儿子传递其他数据时一切正常,我无法绘制 Ploygons。
谢谢!
这些示例对我有用。将 map.shp 替换为您的文件名。适用于 Bokeh v1.0.4。 运行 与:python map.py
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
import geopandas as gp
import shapely
sf = gp.read_file('map.shp')
x, y = [], []
[(x.append(list(polygon.exterior.coords.xy[0])), y.append(list(polygon.exterior.coords.xy[1]))) for polygon in sf['geometry'] if type(polygon.boundary) == shapely.geometry.linestring.LineString ]
p = figure(title = "A map from Shapefile", plot_width = 800)
p.patches('x', 'y', source = ColumnDataSource(dict(x = x, y = y)), line_color = "white", line_width = 0.5)
show(p)
或
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
import geopandas as gp
def getPolyCoords(row, geom, coord_type):
if coord_type == 'x':
return list(row[geom].exterior.coords.xy[0])
elif coord_type == 'y':
return list(row[geom].exterior.coords.xy[1])
gdf = gp.GeoDataFrame.from_file('map.shp')
gdf['x'] = gdf.apply(getPolyCoords, geom = 'geometry', coord_type = 'x', axis = 1)
gdf['y'] = gdf.apply(getPolyCoords, geom = 'geometry', coord_type = 'y', axis = 1)
p_df = gdf.drop('geometry', axis = 1).copy()
p = figure(title = "A map from Shapefile", plot_width = 800)
p.patches('x', 'y', source = ColumnDataSource(p_df), line_color = "white", line_width = 0.5)
show(p)