在散景中插入标签

时间:2018-07-25 16:40:48

标签: bokeh

我正在尝试在Bokeh中插入标签,但是它不起作用。

我的代码是:

from bokeh.io import show, output_file
from bokeh.plotting import figure
from bokeh.io import output_notebook
from bokeh.models import NumeralTickFormatter

df_carteira_grafico = df_resumo_1
df_carteira_grafico['mes_status'] = (df_carteira_grafico['mes_juncao'].astype(dtype=str))+' - '+df_carteira_grafico['Atraso']

output_notebook()
p=figure()
carteira = df_carteira_grafico['mes_status']
tamanho = df_resumo_1['Valor a Entregar']

p = figure(x_range=carteira, plot_height=300, title="Status_Carteira")
p.vbar(x=carteira, top=tamanho, width=0.9)

p.xgrid.grid_line_color = None
p.y_range.start = 0
p.yaxis[0].formatter = NumeralTickFormatter(format="0.0")

show(p)

我得到这个:

graph no labels

我想要这个:

graph labels

谢谢。

2 个答案:

答案 0 :(得分:2)

如果您将数据自己放在ColumnDataSource中,则该源可用于驱动demonstrated in the documentationvbarLabelSet。像这样:

# CDS can also be created directly from data frames, but not clear in your case 
source = ColumnDataSource(data=
    dict(carteira=carteira, tamanho=tamanho, labels=[str(x) for x in tamanho])
)

p.vbar(x='carteira', top='tamanho', width=0.9, source=source)

labels = LabelSet(x='carteira', y='tamanho', text='labels', y_offset=5, source=source)

p.add_layout(labels)

但是请注意,由于您问题中的示例代码不是独立且完整的,因此我无法直接对其进行实际测试。希望它指明了道路。

有关Bokeh数据源的更多信息,请参见Providing Data for Plots and Tables

答案 1 :(得分:1)

知道了。对于将来可能需要的人,这里是代码:

bokeh.io import show, output_file
from bokeh.plotting import figure
from bokeh.io import output_notebook
from bokeh.models import NumeralTickFormatter
from numpy import pi
from bokeh.models import ColumnDataSource
from bokeh.models import LabelSet

df_carteira_grafico = df_resumo_1
df_carteira_grafico['mes_status'] = (df_carteira_grafico['mes_juncao'].astype(dtype=str))+' - '+df_carteira_grafico['Atraso']

output_notebook()
p=figure()
carteira = df_carteira_grafico['mes_status']
tamanho = df_resumo_1['Valor a Entregar']


source = ColumnDataSource(data=dict(carteira=carteira, tamanho=tamanho, labels=[str(x) for x in tamanho]))

p = figure(x_range=carteira, plot_height=400, title="Status_Carteira")
p.vbar(x='carteira', top='tamanho', width=0.9, source=source)


labels = LabelSet(x='carteira', y='tamanho', text='labels', y_offset=5, source=source)

p.add_layout(labels)

p.xgrid.grid_line_color = None
p.y_range.start = 0
p.yaxis[0].formatter = NumeralTickFormatter(format="0.0")


show(p)