Hello coder friend!
I discovered the bokeh libraries and its uses, but I have been blocking for two days now on something that will surely seem trivial to you.
I want to average over N points which I recover via an “inputAverage” and then plot a line on the graph (which is dynamic)
but i didnt know how to get value of “nAverage”… As you can see, nAverage gonna keep her value of “0”… I dont know why… Could u help me please ?
from bokeh.plotting import figure, output_file
from bokeh.models import Panel, Tabs
import numpy as np
import math
from bokeh.io import curdoc
from bokeh.io import output_notebook, show
output_notebook()
from bokeh.layouts import column, row
from bokeh.models.widgets import TextInput, Button, Paragraph
from bokeh.models import CheckboxButtonGroup,PreText,Select,CustomJS, ColumnDataSource, Slider, Panel, Tabs
from random import *
from bokeh.driving import count
#bokeh serve --show Interface1.py
UPDATE_INTERVAL = 250
ROLLOVER = 20
def DrowAverageSelected(x, y, plot):
if nAverage != 0 :
ymoy = sum(y) / len(y)
plot.line(x, ymoy, color="orange", line_width=5, alpha=0.6)
else:
return
def CreateFigure (labelY, labelX, color, source, w=600, h=300, legend=None):
#p.y_range = Range1d(0, 45)
fig = figure(plot_width=w, plot_height=h, tools=["box_select","lasso_select"])
fig.xaxis.axis_label = labelX
fig.yaxis.axis_label = labelY
if (legend == None) :
fig.line('x', 'y',source=source,line_width=2, line_color=color)
else :
for i in range (len(legend)) :
fig.line('x', 'y',source=source[i], line_width=2, line_color=colors[i], legend_label=legend[i])
# if (xmoy != None and ymoy != None):
# fig.circle('x', 'y', color='color', size=8, alpha=0.4, source=xmoy, selection_color="firebrick", selection_alpha=0.4)
# DrowAverageSelected(x,y,fig,xmoy,ymoy)
return fig
@count()
def update(x):
global source, X, Y, figure
for i in range (len(source)):
source[i].stream({"x": [x], "y": [int(random()*10)]}, rollover=ROLLOVER)
if inputAverage.value.isdigit():
nAverage = int(inputAverage.value)
else:
nAverage = 0
DrowAverageSelected(X[-nAverage:], Y[-nAverage:],figure)
def CallbackInputNAverage(attr,old, new):
global nAverage
nAverage = int(new)
print(nAverage, new)
source = [ColumnDataSource({"x": [], "y": []})]
source += [ColumnDataSource({"x": [], "y": []})]
source += [ColumnDataSource({"x": [], "y": []})]
nAverage = 0
inputAverage = TextInput(title="Number of point for Mean")
inputAverage.on_change('value', CallbackInputNAverage)#lamba : création d'une fonction sans passer par def
inputs = column(inputAverage, width=150, height=150)
X = source[0].data['x']
Y = source[0].data['y']
tab = []
fig = CreateFigure('Température (°C)', 'Temps (s)', 'navy', source[0])
tab += [Panel(child=fig, title="Temperature")]
fig = CreateFigure('Pression (Pa)', 'Temps (s)', 'green', source[1])
tab += [Panel(child=fig, title="Pression")]
fig = CreateFigure('Hygrométrie (%)', 'Temps (s)', 'orange', source[2])
tab += [Panel(child=fig, title="Hygrométrie")]
lasso = ["box_select","lasso_select"]
legend = ['Température (°C)', 'Pression (Pa)', 'Hygrométrie (%)']
colors = ['navy', 'green', 'orange']
fig = CreateFigure('', 'Temps (s)', colors, source, w=500, h=500, legend=legend)
tab += [Panel(child=fig, title="All")]
#Layout
tabs = Tabs(tabs=tab)
interface1=row(tabs,inputs)
#curdoc
doc=curdoc() #il va recréer un nouveau document a chaque fois
doc.add_root(interface1)
doc.add_periodic_callback(update, UPDATE_INTERVAL)``