jhaywoo8 jhaywoo8 - 5 months ago 77
Python Question

Adding labels to a Bokeh plot

I have a data frame with columns with player names and their stats. I've plotting two different stats would like the player names to appear under each dot on the scatterplot.

This is what I have so far but it's not working. For text, I assume this is the list of names I want under each point on the plot and source is where the names are coming from.

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)


You're on the right path. However, the source for LabelSet has to be a DataSource. Here is an example code.

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']))

p = 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')