我将如何使用滑块在bokeh中绘制特定的.las文件-并覆盖当前文件

时间:2019-09-26 14:44:36

标签: javascript python widget bokeh

好的,因此,如上所述,我正在尝试使用bokeh绘制一些数据-我想从机器上的文件夹中加载300个左右的文件,然后让滑块选择活动的绘制-我只希望当前选择显示的情节。也就是说,我应该将滑块移至位置30,然后只看到该图-然后移至位置200,并仅看到该图。

我知道我需要为此使用回调函数-但我运气不佳,只能以无响应和空的情节告终。

到目前为止,我的代码如下:

#Includes

#Basic Includes
import numpy as np #Mathematical
import pandas as pd #Data Handling
import bokeh as bk #Plotting
#import ipywidgets as widgets #Interactable widgets
import pathlib #Access of local data path

#bokeh
from bokeh.plotting import figure, output_notebook, show, output_file
from bokeh.layouts import gridplot, column, grid, row
from bokeh.palettes import Viridis256, Plasma256
from bokeh.transform import linear_cmap
from bokeh.models import (ColumnDataSource, ColorBar, LinearColorMapper, LogColorMapper, CustomJS, BasicTicker, Title,
Slider)
from bokeh.io import output_file, show

#ipywidgets
#from ipywidgets import interact, interactive, fixed, IntSlider, interact_manual
#from IPython.display import display

#pathlib
from pathlib import Path

#Allow Graphical Output to notebook
output_notebook()

#Path setup - Path is identical to the folder where data files are stored however every '\' must become '\\'
data_path = Path('C:\\Users\\E252606\\Desktop\\Geothermal\\J39\\J39') #Folder Where Dataset files are located

# create list of all CSV's in working directory
filetype = '*.las' #This is the file extension, may be .csv or *.las or .txt
filenames = list(data_path.glob(filetype))
#print(filenames)
#filenames 
data = {}

#Initialise a variable to store a count of the total number of files
DataCount = 0

#Loop through each file and load it into a dataframe
for filename in filenames:
    data[filename.stem] = pd.read_csv(filename, skiprows=37,sep=" ",names=['Depth Metres', '°C'])  #create df from csv
    #Increment counter to return number of files
    DataCount = DataCount +1

    #Filter Data frame
    Strength = 20 #Select the strength of the filter to use
    data[filename.stem] = data[filename.stem].rolling(window= Strength).mean()

#Initial Plot Definition

#Define the tools to initialise in bokeh plot
Tools ="hover,crosshair,pan,wheel_zoom,zoom_in,zoom_out,box_zoom, reset, save, box_select,poly_select,lasso_select,"

ToolTips = [('Temperature (°C):', '@y'),
            ('Depth (m):' , '@x{0}')
           ]


#Define the figure to push graphs onto (Select title, toolbar location, x & y axis labels and dimensions of plot)
Output = figure(tools = Tools, toolbar_location = "above" , tooltips = ToolTips , title = 'Profile' ,
           x_axis_label='x', y_axis_label= 'y',
            width = 1000, height =600)

#Additional readablilty settings
Output.title.align = "left"
Output.title.text_font_size = "17px"

#Autohide Toolbar until hover
Output.toolbar.autohide = True    

full_source = ColumnDataSource(data= data[filename.stem])
source = ColumnDataSource(data= data[filename.stem])

Output.multi_line(xs = '°C' , ys = 'Depth Metres', source = source)

amp_slider = Slider(start=0, end=DataCount, value=0, step=1, title="File Select")

callback = CustomJS(args=dict(source = source, full_source = full_source, amp=amp_slider),
                    code="""

    const A = amp.value;
    const time = cb_obj.value;
    const '°C' = full_source.data['°C']
    const 'Depth Metres' = full_source.data['Depth Metres']

        for(i=0; i<full_lons.length; i++) {
        source.data['°C'][i] = °C[10].slice(0, time)
        source.data['Depth Metres'][10] = @Depth Metres[i].slice(0, time)
    }

    source.change.emit()
    """)

amp_slider.js_on_change('value', callback)
slider = Slider(start = 0, end = DataCount, value = 1, step = 1, callback = callback)
slider.js_on_change('value', callback)


layout = row(
    column(Output, slider),
)

output_file("slider.html", title="slider.py example")

show(layout)




0 个答案:

没有答案