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 #import io #import random #from typing import List, Tuple #import aiohttp #import panel as pn #from PIL import Image #from transformers import CLIPModel, CLIPProcessor #pn.extension(design="bootstrap", sizing_mode="stretch_width") source = data.movies() pn.extension() temp = sorted(source['IMDB_Rating'].dropna().values) n_int_bins = int(np.ceil(max(temp))+ 1 - np.floor(min(temp))) def create_plot(bandwidth=1.0, bins=n_int_bins): plot = figure(width=300, height=300, toolbar_location=None) # Histogram #bins = np.arange(np.floor(min(temp)), np.ceil(max(temp))+1, 1) hist, edges = np.histogram(temp, bins=bins) 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) # density 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 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) bound_plot = pn.bind(create_plot, bandwidth=bw_widget, bins=bins_widget) first_app = pn.Column(bw_widget, bins_widget, bound_plot) first_app.servable()