【问题标题】:Bokeh plot: output file is appending instead of overwritting散景图:输出文件是附加而不是覆盖
【发布时间】:2018-09-15 02:16:35
【问题描述】:

下面的代码运行良好,但最终它开始在每次执行代码时在同一个 HTML 文件中附加多个绘图。这是一个奇怪的行为,根据 Bokeh 文档,函数 output_file() 应该覆盖 HTML 文件,而不是附加它。

如何防止每次执行脚本时输出文件附加多个绘图?

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

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)

【问题讨论】:

    标签: python python-3.x plot data-visualization bokeh


    【解决方案1】:

    output_file() 函数应该负责处理它,每次执行时都会覆盖 HTML 文件。至少 bokeh.io [link] 的文档中是这样写的。

    警告

    此输出文件将在每次保存时被覆盖,例如,每次 调用 show() 或 save()。

    因此,如果您遇到此问题,解决方案很简单。您只需从 bokeh.io 导入 reset_output,然后在调用 output_file 函数后立即调用它。

    from bokeh.io import reset_output
    
    [...code here...]
    
    output_file("filename.html")
    reset_output()
    
    [...more code here...]
    

    【讨论】:

    • 这确实解决了这个问题。我最近看到了同样的行为,奇怪的是它在没有 reset_output 的情况下工作(就像它应该的那样)。我想知道这个问题是否特定于散景版本?我用散景 0.13.0 看到了这个
    猜你喜欢
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 2018-09-15
    • 1970-01-01
    • 2015-05-25
    • 2020-07-01
    相关资源
    最近更新 更多