python – the application Bokeh behaves strangely

Below, the code for a work extract.

Here is what is strange in the behavior:

  • By pressing the button, the application freezes frequently.
  • The group at the bottom right never contributes to the histogram when it is selected.

Enjoy the comments on the code.

import numpy as np
import pandas as pd

to bokeh.layouts to import a grid
of bokeh.models import BoxSelectTool, LassoSelectTool
of bokeh.plotting import figure, curdoc
of bokeh.io import output_file, show
import button from bokeh.models.widgets
from bokeh.models, import ColumnDataSource
since bokeh.models import DataRange1d

button = Button (label = "Draw points and redo the histogram", button_type = "success")

generated def ():
global x
global y

size1 = np.random.randint (1000, size = 1)[0]
    size2 = np.random.randint (1000, size = 1)[0]
    size3 = np.random.randint (1000, size = 1)[0]

    # create three normal population samples with different parameters
x1 = np.random.normal (loc = 5.0, size = size1) * 100; y1 = np.random.normal (loc = 10.0, size = size1) * 10
x2 = np.random.normal (loc = 5.0, size = size2) * 50; y2 = np.random.normal (loc = 5.0, size = size2) * 10
x3 = np.random.normal (loc = 55.0, size = size3) * 10; y3 = np.random.normal (loc = 4.0, size = size3) * 10

x = np.concatenate ((x1, x2, x3))
y = np.concatenate ((y1, y2, y3))


generated ()


TOOLS = "pan, wheel_zoom, box_select, lasso_select, reset"

p = figure (tools = TOOLS, plot_width = 700, plot_height = 600, min_border = 10, min_border_left = 50,
toolbar_location = "above", x_axis_location = None, y_axis_location = None,
title = "Scatter plot")

pointchart_source = ColumnDataSource (pd.DataFrame ({x: x, y: y}}))
r = p.scatter (x = "x", y = "y", size = 3, color = "# 3A5785", alpha = 0.6, source = pointchart_source)


# create a histogram
hhist, hedges = np.histogram (x, bins = 10) #Set bin numebr here
hzeros = np.zeros (len (hedges) -1)
hmax = max (hhist) * 1.1

LINE_ARGS = dict (color = "# 3A5785", line_color = None)

ph = figure (toolbar_location = None, plot_width = 600, plot_height = 300, x_range = p.x_range,
y_range = (- hmax, hmax), min_border = 10, min_border_left = 50, y_axis_location = "right")

white_hist_glyph = ph.quad (bottom = 0, left = hurdles[:-1], right = hurdles[1:], top = hhist, color = "white", line_color = "# 3A5785")

blue_hist_glyph = ph.quad (bottom = 0, left = hurdles[:-1], right = hurdles[1:], top = hzeros, alpha = 0.5, ** LINE_ARGS)


bins_count = 20

def on_selection_callback (attr, old, new):

x = pd.DataFrame (pointchart_source.data) .loc[new]['x']

    

    

    

    inds = new

allvalues ​​= pd.DataFrame (pointchart_source.data) .loc[:]['x']

    

    

    

    blue_hist_values, _ = np.histogram (x[inds], bins = white_hist_edges)
#lesson: set this in the original's trays
#white_hist_edges
blue_hist_glyph.data_source.data["top"]   = blue_hist_values


r.data_source.selected.on_change (& # 39; indices & # 39; on_selection_callback)


def new_source_callback ():
global white_hist_edges

#Returns new global x and y tables
generated ()

#New plotted points (fails)
pointchart_source.data = ColumnDataSource (pd.DataFrame ({x: x, y: y}})). data

allvalues ​​= pd.DataFrame (pointchart_source.data['x'])
white_hist_values, white_hist_edges = np.histogram (all values, bins = bins_count)
white_hist_glyph.data_source.data["top"] = white_hist_values

#blue_hist_values, _ = np.histogram (pointchart_source.data['x'], bins = white_hist_edges)
blue_hist_glyph.data_source.data["top"] = np.zeros (len (white_hist_edges) -1)


ph.x_range = p.x_range

ph.y_range.start = -max (white_hist_values) * 1.1
ph.y_range.end = max (white_hist_values) * 1.1


button.on_click (new_source_callback)


layout = gridplot ([[p, ph, button]], merge_tools = False)
curdoc (). add_root (layout)
curdoc (). title = "Selection chart"


! powershell -command {- bokeh serve --show Histogram_test_v5.ipynb & # 39;}