散景不显示标签?

时间:2019-07-17 21:14:49

标签: python pandas bokeh

我正在尝试将标签放入某些圈子。我已经使用以下代码在类似示例中正常工作,但在这里无法正常工作。

from bokeh.plotting import figure, show, save
from bokeh.tile_providers import CARTODBPOSITRON_RETINA
from bokeh.io import curdoc, output_notebook, output_file, export_png
from pyproj import Proj, transform
from bokeh.transform import linear_cmap, log_cmap
import pandas as pd
from bokeh.models import (
  GeoJSONDataSource, ColumnDataSource, Circle,CircleX,Hex, Square, Patches, LabelSet, HoverTool,Legend, LegendItem, Plot, LinearAxis, Grid, LogColorMapper, LinearColorMapper, ColorBar, BasicTicker,
  TapTool,Range1d, PanTool, WheelZoomTool, BoxSelectTool,OpenURL, ZoomInTool, ZoomOutTool, Arrow, OpenHead, NormalHead, VeeHead, LabelSet, Label
)
from bokeh.palettes import *
from haversine import haversine   

def latlonrange(lat1,lon1,lat2,lon2):
    p1 = Proj(init='epsg:4326')
    p2 = Proj(init='epsg:3857')    
    x1, y1 = transform(p1,p2,lon1,lat1)
    x2, y2 = transform(p1,p2,lon2,lat2)
    return {"x_range":(x1, x2), "y_range":(y1,y2)}   

df = pd.DataFrame({'daily_cartons': [478,378,763,859], 
                   'daily_cartonsPercent': [19,15,31,35],
                   'clong': [42.259387,42.235110,42.136309,41.874587],
                   'clat': [-71.689145,-71.182045,-71.045608,-71.405451],          
                   'cx_merc': [-7.980399e+06,-7.923949e+06,-7.908761e+06,-7.948818e+06],
                   'cy_merc': [5.199914e+06,5.196263e+06,5.181420e+06,5.142212e+06]
                  })

curdoc().clear()
output_notebook()

latlonbox = latlonrange(43, -74, 41, -69)

p = figure(title="XXXX",
           x_range=latlonbox["x_range"], y_range=latlonbox["y_range"],
           x_axis_type="mercator", y_axis_type="mercator",
           plot_width=1200, plot_height=750,
           tools="pan,wheel_zoom,box_zoom,reset,zoom_in,zoom_out,save")
p.add_tile(CARTODBPOSITRON_RETINA)


ksource = ColumnDataSource(dict(x=df['cx_merc'], y=df['cy_merc'], daily_cartons = df['daily_cartons'],
                                daily_cartons_perc = df['daily_cartonsPercent'].astype(str) + '%', lcolor=viridis(4)))

kcircle = Circle(x="x", y="y", size=30, line_color="#de2d26", fill_color="white", line_width=2, fill_alpha=1)
kc1 = p.add_glyph(ksource, kcircle)

klabels = LabelSet(x='x', y='y', text='daily_cartons_perc', level='glyph', source=ksource, render_mode='canvas',
                   x_offset=-7, text_font_size = '8pt',text_font_style='bold', text_color = 'black', text_baseline ='middle' )
p.add_layout(klabels)

kc1_hover = HoverTool(renderers=[kc1], tooltips=[('Daily_cartons: ', '@daily_cartons{00,000}')])
p.add_tools(kc1_hover)
# #----------------------------------------------------------------------------------------------------------------------------------

show(p)

上面的代码显示的是圆圈,但没有显示标签。不知道为什么吗?

enter image description here

Python:3.7.3,散景:1.2.0

1 个答案:

答案 0 :(得分:1)

删除参数level='glyph'