散景滑块不更新数据

时间:2021-07-06 01:52:56

标签: python bokeh

我通过散景构建交互式热图,但是当我更改滑块上的值时,地图不会更新。我想我已经正确使用了 Bokeh 服务器并且我的代码没有显示错误,地图只是没有更新,所以我不确定有什么问题这是我的代码: 如果有人能帮我解决这个问题,那就太好了!谢谢!

#import requried libraries and Bokeh functions 
import math 
from bokeh.io import show
from bokeh.models import BasicTicker, ColorBar, LinearColorMapper, PrintfTickFormatter
from bokeh.plotting import figure
from bokeh.io import output_notebook 
import numpy as np
import pandas as pd
import math 
import numpy as np
import matplotlib.pyplot as plt 
import matplotlib as mpl
import scipy.special as s   
import scipy.integrate as integrate
from math import pi
import pandas as pd

from bokeh.io import output_file, show
from bokeh.models import BasicTicker, ColorBar, LinearColorMapper, ColumnDataSource, PrintfTickFormatter
from bokeh.plotting import figure
from bokeh.transform import transform

from bokeh.application import Application
from bokeh.application.handlers import FunctionHandler
import sys
import numpy
np.set_printoptions(threshold=np.inf)
pd.options.display.max_seq_items = 2000

import math 
import bokeh.plotting.figure as bk_figure
from bokeh.io import curdoc, show
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Slider, TextInput
from bokeh.io import output_notebook 
import numpy as np

from bokeh.application import Application
from bokeh.application.handlers import FunctionHandler
output_notebook()

# Set up data and function 
v= 2.55
a = 0.1 
b = 1.52
time = 100
x = np.linspace(1.000000000000001,10000,100)
y = np.linspace(-5000,5000,100)
C = np.zeros([len(x),len(y)])
for i in range(0,len(x)):
    for j in range(0,len(y)):
        t = time*365
        DL = v*0.83*(math.log10(x[i]))**2.414
        Dt = a * DL
        def f(r):
            return math.exp((-((x[i]-(v*r))**2)/(4*DL*r))-(((y[j])**2)/(4*Dt*r)))*(1/r)
        results,err = integrate.quad(f,0,t)
        C[i,j] =((5*2*10**8/b)/(4*math.pi*((DL*Dt)**0.5)))*results
CC = np.transpose(C)
CCC = np.clip(CC,0,5)

df = pd.DataFrame(
    CCC,
    columns=x,
    index=y)
df.columns.name = 'x'
df.index.name = 'y'
df = df.stack().rename("value").reset_index()
source = ColumnDataSource(data=dict(df))

colors = ['#d7191c', '#fdae61', '#ffffbf', '#a6d96a', '#1a9641']
mapper = LinearColorMapper(palette=colors, low=df.value.min(), high=df.value.max())
 
p = figure(title="Concentration Signals (2 Dimension)")

p.rect(x="x", y="y",width=100,height=100,
       source=df,
       fill_color=transform('value', mapper),line_color= "white")
color_bar = ColorBar(
    color_mapper=mapper,
    location=(0, 0),
    ticker=BasicTicker(desired_num_ticks=len(colors)))

p.add_layout(color_bar, 'right')

# Set up widgets aka. sliders and text box
text = TextInput(title="title", value='Concentration Signals (2 Dimension)')
vv = Slider(title="Velocity (m/day)", value=0.1, start=0.1, end=5, step=0.2)
tt = Slider(title="Time (Year)", value=1, start=1, end=100, step=1)
aa = Slider(title="Dispersion Fraction", value=0.05, start=0.05, end=0.2,step=0.01)
bb = Slider(title="Squifer Thickness", value=0.03, start=0.03, end=3, step=0.01)
def update_title(attrname, old, new):
    plot.title.text = text.value

def update_data(attrname, old, new):
    # Get the current slider values
    v = vv.value
    time = tt.value
    a = aa.value
    b = bb.value

    # Generate the new curve
    x = np.linspace(1.000000000000001,10000,100)
    y = np.linspace(-5000,5000,100)
    C = np.zeros([len(x),len(y)])
    for i in range(0,len(x)):
        for j in range(0,len(y)):
            t = time*365
            DL = v*0.83*(math.log10(x[i]))**2.414
            Dt = a * DL
            def f(r):
                return math.exp((-((x[i]-(v*r))**2)/(4*DL*r))-(((y[j])**2)/(4*Dt*r)))*(1/r)
        results,err = integrate.quad(f,0,t)
        C[i,j] =((5*2*10**8/b)/(4*math.pi*((DL*Dt)**0.5)))*results
    CC = np.transpose(C)
    CCC = np.clip(CC,0,5)
    df = pd.DataFrame(
    CCC,
    columns=x,
    index=y)
    df.columns.name = 'x'
    df.index.name = 'y'
    df = df.stack().rename("value").reset_index()
    source.data = dict(df)

for w in [vv, tt, aa, bb]:
    w.on_change('value', update_data)


# Set up layouts and add to document
inputs = widgetbox(text, vv, tt, aa, bb)
layout = row(p,widgetbox(text, vv, tt, aa, bb))


def modify_doc(doc):
    doc.add_root(row(layout, width=800))
    doc.title = "Sliders"
    text.on_change('value', update_title)

#show the plot and sliders 
handler = FunctionHandler(modify_doc)
app = Application(handler)
show(app)

0 个答案:

没有答案