Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 用于堆叠vbar的Bokeh滑块,以增加段大小;悬停工具_Python 3.x_Hover_Widget_Bokeh_Mousehover - Fatal编程技术网

Python 3.x 用于堆叠vbar的Bokeh滑块,以增加段大小;悬停工具

Python 3.x 用于堆叠vbar的Bokeh滑块,以增加段大小;悬停工具,python-3.x,hover,widget,bokeh,mousehover,Python 3.x,Hover,Widget,Bokeh,Mousehover,我的目标是在下面的代码中创建一个带有bokeh的堆叠条形图,并附加滑块,这样我就可以增加或减少每个条形图段的大小,并依次移动其他条形图段 我现在的问题是,它在从bokeh服务器运行时不会更新。我猜bokeh在更新源代码后可能不会再次运行计算。。。或者我得到了一个源冲突。(到目前为止,我只为“工程”实现了它。我想在解决其余问题之前让它发挥作用 其他值得注意的事情。我正在使用一种被贬低的技术,为每个glyph提供底部/顶部数据以及一个源。这是因为这是我能够让hovertool显示的唯一方法 我唯一能

我的目标是在下面的代码中创建一个带有bokeh的堆叠条形图,并附加滑块,这样我就可以增加或减少每个条形图段的大小,并依次移动其他条形图段

我现在的问题是,它在从bokeh服务器运行时不会更新。我猜bokeh在更新源代码后可能不会再次运行计算。。。或者我得到了一个源冲突。(到目前为止,我只为“工程”实现了它。我想在解决其余问题之前让它发挥作用

其他值得注意的事情。我正在使用一种被贬低的技术,为每个glyph提供底部/顶部数据以及一个源。这是因为这是我能够让hovertool显示的唯一方法

我唯一能做到这一点的方法是完全重新绘制图形,我同意这个选项,但它是将图形堆叠在一起。有没有办法清除Bokeh中以前的所有图形?显然,我更喜欢一种只改变数据而不完全重新绘制图形的解决方案

from bokeh.plotting import figure, show, curdoc
from bokeh.models import NumeralTickFormatter
from bokeh.models import HoverTool
from bokeh.models import ColumnDataSource
from bokeh.layouts import widgetbox, column
from bokeh.models import CustomJS, Slider
from matplotlib import colors
import pandas as pd
import numpy as np

# Read Data
df=pd.read_csv('/home/mint/SAGD_Costs.csv')


# Master source
source = ColumnDataSource(df)


# Bar Tops Data
engtop = source.data['Engineering'][0]
equiptop = source.data['Engineering'][0] + source.data['Equipment'][0]
bulktop = source.data['Engineering'][0] + source.data['Equipment'][0] + source.data['Bulk_Materials'][0]
inditop = source.data['Engineering'][0] + source.data['Equipment'][0] + source.data['Bulk_Materials'][0] + source.data['Indirects'][0]
labtop = source.data['Engineering'][0] + source.data['Equipment'][0] + source.data['Bulk_Materials'][0] + source.data['Indirects'][0] + source.data['Labour'][0]


# Source for Stupid Hovertool
engsource = ColumnDataSource(data=dict(x=[0], y=[engtop], desc = ['Engineering']))
equipsource = ColumnDataSource(data=dict(x=[0], y=[equiptop-engtop], desc = ['Equipment']))
bulksource = ColumnDataSource(data=dict(x=[0], y=[bulktop-equiptop], desc = ['Bulk Materials']))
indisource = ColumnDataSource(data=dict(x=[0], y=[inditop-bulktop], desc = ['Indirects']))
labsource = ColumnDataSource(data=dict(x=[0], y=[labtop-inditop], desc = ['Labour']))

# HoverTool Label
hover = HoverTool(
    tooltips=[
('Item', '@desc'),
('Cost', '@y{$ 0.00 a}'),
    ]
)


# Other Tools
TOOLS = 'box_zoom, box_select, resize, reset'


# Figure
p = figure(title="Capital Costs Breakdown", title_location="above", plot_width=600, plot_height=600, x_range=(-2, 2), tools=[TOOLS, hover])


# Plots
engbar = p.vbar(x=source.data['Year'][0], width=2, bottom=0,
   top=engtop, alpha=0.75, color="darkslategrey", legend="Engineering", source=engsource)

equipbar = p.vbar(x=[source.data['Year'][0]], width=2, bottom=engtop, 
   top = equiptop, alpha=0.75, color="teal", legend="Equipment", source=equipsource)

bulkbar = p.vbar(x=[source.data['Year'][0]], width=2, bottom=equiptop, 
   top=bulktop, alpha=0.75, color="cyan", legend="Bulk Materials", source=bulksource)

indibar = p.vbar(x=[source.data['Year'][0]], width=2, bottom=bulktop, 
   top=inditop, alpha=0.75, color="powderblue", legend="Indirects", source=indisource)

labbar = p.vbar(x=[source.data['Year'][0]], width=2, bottom=inditop, 
   top=labtop, alpha=0.75, color="lavender", legend="Labour", source=labsource)


# Format
p.yaxis[0].formatter = NumeralTickFormatter(format="$0,000")


# Set up widgets
eng_slider = Slider(start=5000000, end=100000000, value=40000000, step=5000000, title="Engineering")


def update_data(attrname, old, new):

    # Get the current slider values
    a = eng_slider.value


    # Generate the new curve
    df['Engineering'][0] = a


    source = ColumnDataSource(df)
    #source.data = dict(x=x, y=y)


for w in [eng_slider]:
    w.on_change('value', update_data)


# Set up layouts and add to document
inputs = widgetbox(eng_slider)


# Show!
curdoc().add_root(column(inputs, p))
curdoc().title = "Sliders"


不确定回答您自己问题的礼仪…它基本上是固定的,但是Hovertool工作不正常。由于Hovertool是@y,它显示每个项目的堆栈总数。我希望它显示差异。是否可以计算Hovertool的值

如果我的情况对某人有所帮助,我在上面犯的错误是我用滑块更改了一个值,该值必须在传递到Glyph之前通过计算

正确的方法是在更新功能中进行任何计算

如果您像我一样来自Pandas&Matplotlib,您可能最终会在图表中构建df列调用,例如x=df['column_name'][0]。在bokeh中使用图示符绘图时,我认为正确的方法是使用所需的数据创建源,这样您就可以将x和y传递到图示符中。请参阅:主源、获取源数据、计算顶部和底部以及下面的代码中的新源

# Read Data
df=pd.read_csv('/home/mint/SAGD_Costs.csv')


# Master source
source = ColumnDataSource(df)


# Get source data
a = source.data['Engineering'][0]
b = source.data['Equipment'][0]
c = source.data['Bulk_Materials'][0]
d = source.data['Indirects'][0]
e = source.data['Labour'][0]

# Calculate Top & Bottom
ab = 0
at = a
bb = a
bt = a + b
cb = a + b
ct = a + b + c
db = a + b + c
dt = a + b + c + d
eb = a + b + c + d
et = a + b + c + d + e


# New sources
engsource = ColumnDataSource(data=dict(x=[ab], y=[at], desc = ['Engineering']))
equipsource = ColumnDataSource(data=dict(x=[bb], y=[bt], desc = ['Equipment']))
bulksource = ColumnDataSource(data=dict(x=[cb], y=[ct], desc = ['Bulk Materials']))
indisource = ColumnDataSource(data=dict(x=[db], y=[dt], desc = ['Indirects']))
labsource = ColumnDataSource(data=dict(x=[eb], y=[et], desc = ['Labour']))


# HoverTool Label
hover = HoverTool(
        tooltips=[
    ('Item', '@desc'),
    ('Cost', '@y{$ 0.00 a}'),
        ]
    )


# Other Tools
TOOLS = 'box_zoom, box_select, resize, reset'


# Figure
p = figure(title="Capital Costs Breakdown", title_location="above", plot_width=600, plot_height=600, x_range=(-2, 2), tools=[TOOLS, hover])


# Plots
engbar = p.vbar(x=0, width=2, bottom = 'x',
       top ='y', alpha=0.75, color="darkslategrey", legend="Engineering", source=engsource)

equipbar = p.vbar(x=0, width=2, bottom = 'x', 
       top = 'y', alpha=0.75, color="teal", legend="Equipment", source=equipsource)

bulkbar = p.vbar(x=0, width=2, bottom = 'x', 
       top ='y', alpha=0.75, color="cyan", legend="Bulk Materials", source=bulksource)

indibar = p.vbar(x=0, width=2, bottom = 'x', 
       top ='y', alpha=0.75, color="powderblue", legend="Indirects", source=indisource)

labbar = p.vbar(x=0, width=2, bottom = 'x', 
       top = 'y', alpha=0.75, color="lavender", legend="Labour", source=labsource)


# Format
p.yaxis[0].formatter = NumeralTickFormatter(format="$0,000")


# Set up widgets
eng_slider = Slider(start=5000000, end=100000000, value=40000000, step=5000000, title="Engineering")
equip_slider = Slider(start=5000000, end=100000000, value=40000000, step=5000000, title="Equipment")
bulk_slider = Slider(start=5000000, end=100000000, value=40000000, step=5000000, title="Bulk_Materials")
indi_slider = Slider(start=5000000, end=100000000, value=40000000, step=5000000, title="Indirects")
lab_slider = Slider(start=5000000, end=100000000, value=40000000, step=5000000, title="Labour")

def update_data(attrname, old, new):

    # Get the current slider values
    a = eng_slider.value
    b = equip_slider.value
    c = bulk_slider.value
    d = indi_slider.value
    e = lab_slider.value

    # Calculate Top & Bottom
    ab = 0
    at = a
    bb = a
    bt = a + b
    cb = a + b
    ct = a + b + c
    db = a + b + c
    dt = a + b + c + d
    eb = a + b + c + d
    et = a + b + c + d + e


    # New sources
    engsource.data=dict(x=[ab], y=[at], desc = ['Engineering'])
    equipsource.data=dict(x=[bb], y=[bt], desc = ['Equipment'])
    bulksource.data=dict(x=[cb], y=[ct], desc = ['Bulk Materials'])
    indisource.data=dict(x=[db], y=[dt], desc = ['Indirects'])
    labsource.data=dict(x=[eb], y=[et], desc = ['Labour'])



for w in [eng_slider, equip_slider, bulk_slider, indi_slider, lab_slider]:
    w.on_change('value', update_data)


# Set up layouts and add to document
inputs = widgetbox(eng_slider, equip_slider, bulk_slider, indi_slider, lab_slider)


# Show!
curdoc().add_root(column(inputs, p))
curdoc().title = "Sliders"

不确定回答您自己问题的礼仪…它基本上是固定的,但是Hovertool不能正常工作。由于Hovertool是@y,它显示每个项目的堆栈总数。我希望它显示差异。是否可以计算Hovertool的值

如果我的情况对某人有所帮助,我在上面犯的错误是我用滑块更改了一个值,该值必须在传递到Glyph之前通过计算

正确的方法是在更新功能中进行任何计算

如果您像我一样来自Pandas&Matplotlib,您可能最终会在图表中构建df列调用,例如x=df['column_name'][0]。在bokeh中使用图示符绘图时,我认为正确的方法是使用所需的数据创建源,这样您就可以将x和y传递到图示符中。请参阅:主源、获取源数据、计算顶部和底部以及下面的代码中的新源

# Read Data
df=pd.read_csv('/home/mint/SAGD_Costs.csv')


# Master source
source = ColumnDataSource(df)


# Get source data
a = source.data['Engineering'][0]
b = source.data['Equipment'][0]
c = source.data['Bulk_Materials'][0]
d = source.data['Indirects'][0]
e = source.data['Labour'][0]

# Calculate Top & Bottom
ab = 0
at = a
bb = a
bt = a + b
cb = a + b
ct = a + b + c
db = a + b + c
dt = a + b + c + d
eb = a + b + c + d
et = a + b + c + d + e


# New sources
engsource = ColumnDataSource(data=dict(x=[ab], y=[at], desc = ['Engineering']))
equipsource = ColumnDataSource(data=dict(x=[bb], y=[bt], desc = ['Equipment']))
bulksource = ColumnDataSource(data=dict(x=[cb], y=[ct], desc = ['Bulk Materials']))
indisource = ColumnDataSource(data=dict(x=[db], y=[dt], desc = ['Indirects']))
labsource = ColumnDataSource(data=dict(x=[eb], y=[et], desc = ['Labour']))


# HoverTool Label
hover = HoverTool(
        tooltips=[
    ('Item', '@desc'),
    ('Cost', '@y{$ 0.00 a}'),
        ]
    )


# Other Tools
TOOLS = 'box_zoom, box_select, resize, reset'


# Figure
p = figure(title="Capital Costs Breakdown", title_location="above", plot_width=600, plot_height=600, x_range=(-2, 2), tools=[TOOLS, hover])


# Plots
engbar = p.vbar(x=0, width=2, bottom = 'x',
       top ='y', alpha=0.75, color="darkslategrey", legend="Engineering", source=engsource)

equipbar = p.vbar(x=0, width=2, bottom = 'x', 
       top = 'y', alpha=0.75, color="teal", legend="Equipment", source=equipsource)

bulkbar = p.vbar(x=0, width=2, bottom = 'x', 
       top ='y', alpha=0.75, color="cyan", legend="Bulk Materials", source=bulksource)

indibar = p.vbar(x=0, width=2, bottom = 'x', 
       top ='y', alpha=0.75, color="powderblue", legend="Indirects", source=indisource)

labbar = p.vbar(x=0, width=2, bottom = 'x', 
       top = 'y', alpha=0.75, color="lavender", legend="Labour", source=labsource)


# Format
p.yaxis[0].formatter = NumeralTickFormatter(format="$0,000")


# Set up widgets
eng_slider = Slider(start=5000000, end=100000000, value=40000000, step=5000000, title="Engineering")
equip_slider = Slider(start=5000000, end=100000000, value=40000000, step=5000000, title="Equipment")
bulk_slider = Slider(start=5000000, end=100000000, value=40000000, step=5000000, title="Bulk_Materials")
indi_slider = Slider(start=5000000, end=100000000, value=40000000, step=5000000, title="Indirects")
lab_slider = Slider(start=5000000, end=100000000, value=40000000, step=5000000, title="Labour")

def update_data(attrname, old, new):

    # Get the current slider values
    a = eng_slider.value
    b = equip_slider.value
    c = bulk_slider.value
    d = indi_slider.value
    e = lab_slider.value

    # Calculate Top & Bottom
    ab = 0
    at = a
    bb = a
    bt = a + b
    cb = a + b
    ct = a + b + c
    db = a + b + c
    dt = a + b + c + d
    eb = a + b + c + d
    et = a + b + c + d + e


    # New sources
    engsource.data=dict(x=[ab], y=[at], desc = ['Engineering'])
    equipsource.data=dict(x=[bb], y=[bt], desc = ['Equipment'])
    bulksource.data=dict(x=[cb], y=[ct], desc = ['Bulk Materials'])
    indisource.data=dict(x=[db], y=[dt], desc = ['Indirects'])
    labsource.data=dict(x=[eb], y=[et], desc = ['Labour'])



for w in [eng_slider, equip_slider, bulk_slider, indi_slider, lab_slider]:
    w.on_change('value', update_data)


# Set up layouts and add to document
inputs = widgetbox(eng_slider, equip_slider, bulk_slider, indi_slider, lab_slider)


# Show!
curdoc().add_root(column(inputs, p))
curdoc().title = "Sliders"

HOVERTOOLS回答说,我没有意识到可以在ColumnDataSource中添加额外的计算变量,然后使用@function从工具提示中引用它们,见下文。
labsource=ColumnDataSource(data=dict(x=[eb],y=[et],desc=['Labour'],info=[et dt])
phover=HoverTool(工具提示=[('Item','@desc'),('Cost','@info{$0.00a}'),],)
HOVERTOOLS-ANSWER,我不知道您可以在ColumnDataSource中添加额外的计算变量,然后使用@function从工具提示中引用这些变量,请参见下文。
labsource=ColumnDataSource(data=dict(x=[eb],y=[et],desc=[Labour],info=[et dt]))
phover=HoverTool(工具提示=[('Item','@desc'),('Cost','@info{$0.00a}'),]),)