散景:仅显示一个工具提示

Arn*_*uza 3 python python-2.7 python-3.x pandas bokeh

我面临与在散景图中正确显示 HoverTool 工具提示相关的问题。我的目的是只显示一个,使用属性:

模式 = vline

如下图所示: 想要的情节

但是,我的绘图为 vline 在与图形交互期间穿过的每个字形(系列)呈现一个工具提示,如下所示:

呈现三重工具提示

即使我将属性 Mode 更改为“鼠标”,该图仍会继续呈现多个工具提示:

双提示

我的代码:[一个汇编函数,一个更大项目的一部分]

from bokeh.models import (HoverTool,
                          ColumnDataSource,
                          Title,
                          DatetimeTickFormatter)
import pandas as pd
from bokeh.io import show, output_file
from bokeh.plotting import figure


def assembly_chart(df, complements):
    """function to assembly the chart"""

    output_file("movigrama_chart.html")

    source = ColumnDataSource(df)

    p = figure(x_axis_type='datetime',
               x_axis_label='days of moviment',
               y_axis_label='unities movimented',
               plot_width=1230,
               plot_height=500,
               active_scroll='wheel_zoom')

    p.vbar(x='DT',
           bottom=0,
           top='STOCK',
           width=pd.Timedelta(days=1),
           fill_alpha=0.4,
           color='#99d8c9',
           source=source)

    p.vbar(x='DT',
           bottom=0,
           top='SOMA_SAI',
           width=pd.Timedelta(days=1),
           fill_alpha=0.8,
           color='crimson',
           source=source)

    p.vbar(x='DT',
           bottom=0,
           top='SOMA_ENTRA',
           width=pd.Timedelta(days=1),
           fill_alpha=0.8,
           color='seagreen',
           source=source)

    p.add_layout(Title(text=complements['warehouse'],
                       text_font='helvetica',
                       text_font_size='10pt',
                       text_color='orangered',
                       text_alpha=0.5,
                       align='center',
                       text_font_style="italic"), 'above')
    p.add_layout(Title(text=complements['product'],
                       text_font='helvetica',
                       text_font_size='10pt',
                       text_color='orangered',
                       text_alpha=0.5,
                       align='center',
                       text_font_style="italic"), 'above')
    p.add_layout(Title(text='Movigrama Endicon',
                       text_font='helvetica',
                       text_font_size='16pt',
                       text_color='orangered',
                       text_alpha=0.9,
                       align='center',
                       text_font_style="bold"), 'above')

    p.x_range.range_padding = 0.1
    p.y_range.range_padding = 0.1

    p.outline_line_width = 4
    p.outline_line_alpha = 0.1
    p.outline_line_color = 'orangered'

    p.axis.major_label_text_color = 'gray'
    p.axis.major_label_text_font_style = 'bold'

    p.axis.axis_label_text_color = 'gray'
    p.axis.axis_label_text_font_style = 'bold'

    p.axis.major_tick_out = 10
    p.axis.minor_tick_in = -3
    p.axis.minor_tick_out = 6
    p.axis.minor_tick_line_color = 'gray'

    p.xaxis.formatter = DatetimeTickFormatter(
                days=['%d/%m'],
                months=['%m/%Y'],
                years=['%Y'])

    # iniciate hover object
    hover = HoverTool()
    hover.mode = 'vline'  # activate hover by vertical line
    hover.tooltips = [("IN", "@SOMA_ENTRA"),
                      ("OUT", "@SOMA_SAI"),
                      ("STOCK", "@STOCK"),
                      ("DT", "@DT{%d/%m/%Y}")]
    # use 'datetime' formatter for 'DT' field
    hover.formatters = {"DT": 'datetime'}
    p.add_tools(hover)

    show(p)
Run Code Online (Sandbox Code Playgroud)

Ari*_*esh 5

您可以定义glyphrenderer要附加到悬停工具的内容。为此,首先命名您的字形,例如

r1 = p.vbar(...)

然后将renderers参数添加到悬停 -

hover.renderers =[r1]

请参阅下面的完整代码-

from bokeh.models import (HoverTool,
                          ColumnDataSource,
                          Title,
                          DatetimeTickFormatter)
import pandas as pd
from bokeh.io import show, output_file
from bokeh.plotting import figure


def assembly_chart(df, complements):
    """function to assembly the chart"""

    output_file("movigrama_chart.html")

    source = ColumnDataSource(df)

    p = figure(x_axis_type='datetime',
               x_axis_label='days of moviment',
               y_axis_label='unities movimented',
               plot_width=1230,
               plot_height=500,
               active_scroll='wheel_zoom')

    r1 = p.vbar(x='DT',
           bottom=0,
           top='STOCK',
           width=pd.Timedelta(days=1),
           fill_alpha=0.4,
           color='#99d8c9',
           source=source)

    r2 = p.vbar(x='DT',
           bottom=0,
           top='SOMA_SAI',
           width=pd.Timedelta(days=1),
           fill_alpha=0.8,
           color='crimson',
           source=source)

    r3 = p.vbar(x='DT',
           bottom=0,
           top='SOMA_ENTRA',
           width=pd.Timedelta(days=1),
           fill_alpha=0.8,
           color='seagreen',
           source=source)

    p.add_layout(Title(text=complements['warehouse'],
                       text_font='helvetica',
                       text_font_size='10pt',
                       text_color='orangered',
                       text_alpha=0.5,
                       align='center',
                       text_font_style="italic"), 'above')
    p.add_layout(Title(text=complements['product'],
                       text_font='helvetica',
                       text_font_size='10pt',
                       text_color='orangered',
                       text_alpha=0.5,
                       align='center',
                       text_font_style="italic"), 'above')
    p.add_layout(Title(text='Movigrama Endicon',
                       text_font='helvetica',
                       text_font_size='16pt',
                       text_color='orangered',
                       text_alpha=0.9,
                       align='center',
                       text_font_style="bold"), 'above')

    p.x_range.range_padding = 0.1
    p.y_range.range_padding = 0.1

    p.outline_line_width = 4
    p.outline_line_alpha = 0.1
    p.outline_line_color = 'orangered'

    p.axis.major_label_text_color = 'gray'
    p.axis.major_label_text_font_style = 'bold'

    p.axis.axis_label_text_color = 'gray'
    p.axis.axis_label_text_font_style = 'bold'

    p.axis.major_tick_out = 10
    p.axis.minor_tick_in = -3
    p.axis.minor_tick_out = 6
    p.axis.minor_tick_line_color = 'gray'

    p.xaxis.formatter = DatetimeTickFormatter(
                days=['%d/%m'],
                months=['%m/%Y'],
                years=['%Y'])

    # iniciate hover object
    hover = HoverTool()
    hover.mode = 'vline'  # activate hover by vertical line
    hover.tooltips = [("IN", "@SOMA_ENTRA"),
                      ("OUT", "@SOMA_SAI"),
                      ("STOCK", "@STOCK"),
                      ("DT", "@DT{%d/%m/%Y}")]
    # use 'datetime' formatter for 'DT' field
    hover.formatters = {"DT": 'datetime'}
    hover.renderers = [r1]
    p.add_tools(hover)

    show(p)
Run Code Online (Sandbox Code Playgroud)