下面的代码工作正常,但最终它在每次执行代码时都开始在同一个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)
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...]