向散景图添加标签

Adding labels to a Bokeh plot

我有一个数据框,其中包含玩家姓名及其统计数据的列。我绘制了两个不同的统计数据,希望玩家姓名显示在散点图上每个点的下方。

这是我目前所拥有的,但它不起作用。对于文本,我假设这是我想要在图上每个点下的名称列表,源是名称的来源。

p = Scatter(dt,x='off_rating',y='def_rating',title="Offensive vs. Defensive Eff",color="navy")
labels = LabelSet(x='off_rating',y='def_rating',text="player_name",source=dt)
p.add_layout(labels)

你走在正确的道路上。但是,LabelSetsource 必须是数据源。这是一个示例代码。

from bokeh.plotting import show, ColumnDataSource
from bokeh.charts import Scatter
from bokeh.models import LabelSet
from pandas.core.frame import DataFrame

source = DataFrame(
    dict(
        off_rating=[66, 71, 72, 68, 58, 62],
        def_rating=[165, 189, 220, 141, 260, 174],
        names=['Mark', 'Amir', 'Matt', 'Greg', 'Owen', 'Juan']
    )
)


scatter_plot = Scatter(
                source,
                x='off_rating',
                y='def_rating',
                title='Offensive vs. Defensive Eff',
                color='navy')

labels = LabelSet(
            x='off_rating',
            y='def_rating',
            text='names',
            level='glyph',
            x_offset=5, 
            y_offset=5, 
            source=ColumnDataSource(source), 
            render_mode='canvas')

scatter_plot.add_layout(labels)

show(scatter_plot)

@Oluwafem 的解决方案无效,因为 bokeh.charts 已弃用。这是一个更新的解决方案

from bokeh.plotting import figure, output_file, show,ColumnDataSource
from bokeh.models import  ColumnDataSource,Range1d, LabelSet, Label

from pandas.core.frame import DataFrame
source = DataFrame(
    dict(
        off_rating=[66, 71, 72, 68, 58, 62],
        def_rating=[165, 189, 220, 141, 260, 174],
        names=['Mark', 'Amir', 'Matt', 'Greg', 'Owen', 'Juan']
    )
)
p = figure(plot_width=600, plot_height=450, title = "'Offensive vs. Defensive Eff'")
p.circle('off_rating','def_rating',source=source,fill_alpha=0.6,size=10, )
p.xaxis.axis_label = 'off_rating'
p.yaxis.axis_label = 'def_rating'
labels = LabelSet(x='off_rating', y='def_rating', text='names',text_font_size='9pt',
              x_offset=5, y_offset=5, source=ColumnDataSource(source), render_mode='canvas')
p.add_layout(labels)
show(p)