pandas 向散景图添加标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40450943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:21:53  来源:igfitidea点击:

Adding labels to a Bokeh plot

pythonpandasbokeh

提问by jhaywoo8

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)
p.add_layout(labels)

回答by Oluwafemi Sule

You're on the right path. However, the sourcefor LabelSethas to be a DataSource. Here is an example code.

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

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)