dcard commited on
Commit
aa6cc35
1 Parent(s): 25de15c

streamline

Browse files
Files changed (1) hide show
  1. app.py +13 -25
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  from vega_datasets import data
3
  from scipy import stats
4
  from bokeh.plotting import figure
@@ -6,55 +5,44 @@ from bokeh.models import ColumnDataSource
6
  import panel as pn
7
  import numpy as np
8
 
9
- #import io
10
- #import random
11
- #from typing import List, Tuple
12
-
13
- #import aiohttp
14
- #import panel as pn
15
- #from PIL import Image
16
- #from transformers import CLIPModel, CLIPProcessor
17
-
18
- #pn.extension(design="bootstrap", sizing_mode="stretch_width")
19
-
20
-
21
- source = data.movies()
22
-
23
  pn.extension()
24
 
 
 
25
  temp = sorted(source['IMDB_Rating'].dropna().values)
26
 
 
27
  n_int_bins = int(np.ceil(max(temp))+ 1 - np.floor(min(temp)))
28
 
 
29
  def create_plot(bandwidth=1.0, bins=n_int_bins):
30
  plot = figure(width=300, height=300, toolbar_location=None)
31
 
32
- # Histogram
33
- #bins = np.arange(np.floor(min(temp)), np.ceil(max(temp))+1, 1)
34
  hist, edges = np.histogram(temp, bins=bins)
35
 
 
36
  hist = hist / hist.sum()
37
-
38
  quad = plot.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:],
39
  fill_color="grey", line_color="white", alpha=0.5)
40
 
41
- # density
42
  kernel = stats.gaussian_kde(temp, bw_method=bandwidth)
43
  x = np.linspace(min(temp), max(temp), 100)
44
  y = kernel(x)
45
  col_source = ColumnDataSource(data=dict(x=x, y=y))
46
  line = plot.line('x', 'y', source=col_source, alpha=1.0, width=2)
47
- return plot
48
-
49
 
 
50
 
 
51
  bw_widget = pn.widgets.FloatSlider(name="Bandwidth", value=1.0, start=0.03, end=2.0, step=0.02)
52
  bins_widget = pn.widgets.IntSlider(name="Number of Bins", value=n_int_bins, start=1, end=n_int_bins*10)
53
 
 
54
  bound_plot = pn.bind(create_plot, bandwidth=bw_widget, bins=bins_widget)
55
 
 
56
  first_app = pn.Column(bw_widget, bins_widget, bound_plot)
57
-
58
- first_app.servable()
59
-
60
-
 
 
1
  from vega_datasets import data
2
  from scipy import stats
3
  from bokeh.plotting import figure
 
5
  import panel as pn
6
  import numpy as np
7
 
8
+ # Tell banel to use Bokeh
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  pn.extension()
10
 
11
+ # Get the data
12
+ source = data.movies()
13
  temp = sorted(source['IMDB_Rating'].dropna().values)
14
 
15
+ # compute an initial number of bins
16
  n_int_bins = int(np.ceil(max(temp))+ 1 - np.floor(min(temp)))
17
 
18
+ # define a function that takes the parameters and creates the plot
19
  def create_plot(bandwidth=1.0, bins=n_int_bins):
20
  plot = figure(width=300, height=300, toolbar_location=None)
21
 
22
+ # Compute the histogram with the specified number of bins
 
23
  hist, edges = np.histogram(temp, bins=bins)
24
 
25
+ # normalize and plot it
26
  hist = hist / hist.sum()
 
27
  quad = plot.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:],
28
  fill_color="grey", line_color="white", alpha=0.5)
29
 
30
+ # Compute the density with the specified bandwidth
31
  kernel = stats.gaussian_kde(temp, bw_method=bandwidth)
32
  x = np.linspace(min(temp), max(temp), 100)
33
  y = kernel(x)
34
  col_source = ColumnDataSource(data=dict(x=x, y=y))
35
  line = plot.line('x', 'y', source=col_source, alpha=1.0, width=2)
 
 
36
 
37
+ return plot
38
 
39
+ # Create widgets for the bandwidth and number of bins
40
  bw_widget = pn.widgets.FloatSlider(name="Bandwidth", value=1.0, start=0.03, end=2.0, step=0.02)
41
  bins_widget = pn.widgets.IntSlider(name="Number of Bins", value=n_int_bins, start=1, end=n_int_bins*10)
42
 
43
+ # bind the sliders to the plotting function
44
  bound_plot = pn.bind(create_plot, bandwidth=bw_widget, bins=bins_widget)
45
 
46
+ # Combine everything together
47
  first_app = pn.Column(bw_widget, bins_widget, bound_plot)
48
+ first_app.servable()