File size: 1,698 Bytes
542501c
 
 
 
6efa8c2
542501c
 
aa6cc35
542501c
 
aa6cc35
 
3bdca18
542501c
aa6cc35
25de15c
542501c
aa6cc35
25de15c
542501c
6efa8c2
aa6cc35
542501c
 
aa6cc35
542501c
 
 
 
aa6cc35
542501c
 
 
 
3bdca18
542501c
aa6cc35
542501c
aa6cc35
542501c
25de15c
542501c
aa6cc35
542501c
 
aa6cc35
542501c
aa6cc35
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from vega_datasets import data
from scipy import stats
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
import panel as pn
import numpy as np

# Tell banel to use Bokeh
pn.extension()

# Get the data
source = data.movies()
temp = sorted(source['IMDB_Rating'].dropna().values)

# compute an initial number of bins
n_int_bins = int(np.ceil(max(temp))+ 1 - np.floor(min(temp)))

# define a function that takes the parameters and creates the plot
def create_plot(bandwidth=1.0, bins=n_int_bins):
    plot = figure(width=300, height=300, toolbar_location=None)
    
    # Compute the histogram with the specified number of bins
    hist, edges = np.histogram(temp, bins=bins)

    # normalize and plot it
    hist = hist / hist.sum()
    quad = plot.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:],
             fill_color="grey", line_color="white", alpha=0.5)

    # Compute the density with the specified bandwidth
    kernel = stats.gaussian_kde(temp, bw_method=bandwidth)
    x = np.linspace(min(temp), max(temp), 100)
    y = kernel(x)
    col_source = ColumnDataSource(data=dict(x=x, y=y))
    line = plot.line('x', 'y', source=col_source, alpha=1.0, width=2)

    return plot

# Create widgets for the bandwidth and number of bins
bw_widget = pn.widgets.FloatSlider(name="Bandwidth", value=1.0, start=0.03, end=2.0, step=0.02)
bins_widget = pn.widgets.IntSlider(name="Number of Bins", value=n_int_bins, start=1, end=n_int_bins*10)

# bind the sliders to the plotting function
bound_plot = pn.bind(create_plot, bandwidth=bw_widget, bins=bins_widget)

# Combine everything together
first_app = pn.Column(bw_widget, bins_widget, bound_plot)
first_app.servable()