avatarStan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3490

Abstract

iled gallery and user guide 3. input x, y variable as a list</p><div id="cd8f"><pre> <span class="hljs-selector-tag">p</span><span class="hljs-selector-attr">[i]</span><span class="hljs-selector-class">.line</span>(x list, y list, options)</pre></div><figure id="52cc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*NotywFDMHk0zKmTSTp1Wzw.png"><figcaption>example of plots Bokeh can generate from <a href="https://docs.bokeh.org/en/latest/docs/gallery.html">Bokeh site</a></figcaption></figure><p id="1572"><b>Workflows to automatically Generated Dashboard</b></p><p id="b1eb"><b>Example 1: Line plots,</b> Following is an example to plot and create a dashboard automatically.</p><div id="5f6a"><pre>p=<span class="hljs-selector-attr">[0]</span><span class="hljs-built_in">len</span>(features) <span class="hljs-keyword">for</span> <span class="hljs-selector-tag">i</span>, feature <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(features): <span class="hljs-selector-id">#wrap</span> <span class="hljs-keyword">in</span> <span class="hljs-selector-tag">a</span> loop</pre></div><div id="e533"><pre><span class="hljs-comment">#1. define the figure object</span> p[i] = figure(<span class="hljs-attribute">width</span>=400, <span class="hljs-attribute">height</span>=200) </pre></div><div id="512f"><pre>#<span class="hljs-number">2</span>. plot individual feature <span class="hljs-selector-tag">p</span><span class="hljs-selector-attr">[i]</span><span class="hljs-selector-class">.line</span>(confM<span class="hljs-selector-class">.index</span><span class="hljs-selector-class">.values</span>, confM<span class="hljs-selector-attr">[feature]</span>, line_color=<span class="hljs-string">'green'</span>, legend_label=f<span class="hljs-string">"{feature}"</span>) <span class="hljs-selector-tag">p</span><span class="hljs-selector-attr">[i]</span><span class="hljs-selector-class">.title</span><span class="hljs-selector-class">.text</span> = f<span class="hljs-string">'{feature} '</span></pre></div><div id="e2f2"><pre><span class="hljs-comment">#3. create a plot object list and assign to dashboard in gridlayout </span> <span class="hljs-attr">res</span>=[p[i] for i in range(<span class="hljs-number">4</span>)] <span class="hljs-comment"># create a list from above plot list</span> <span class="hljs-attr">grid_layout</span> = grid(res, nrows=<span class="hljs-number">2</span>) <span class="hljs-comment"># design how many rows to show</span></pre></div><div id="fe51"><pre><span class="hljs-function"><span class="hljs-title">show</span><span class="hljs-params">(grid_layout)</span></span> <span class="hljs-selector-id">#show</span> the plots</pre></div><figure id="3f7a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*cGuJrEyEW7CNI5Ttc0Hbpw.png"><figcaption></figcaption></figure><p id="b6ab"><b>Example 2: create scatter plots </b>and dashboard automatically repeat the same pattern above.</p><div id="f090"><pre>p=<span class="hljs-selector-attr">[0]</span><span class="hljs-built_in">len</span>(features) <span class="hljs-keyword">for</span> <span class="hljs-selector-tag">i</span>, featurex <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(features): <span class="hljs-selector-tag">p</span><span class="hljs-selector-attr">[i]</span> = <span class="hljs-selector-tag">figure</span>(<span class="hljs-attribute">width</span>=<span class="hljs-num

Options

ber">300</span>, height=<span class="hljs-number">300</span>) <span class="hljs-selector-tag">p</span><span class="hljs-selector-attr">[i]</span><span class="hljs-selector-class">.circle</span>(x=<span class="hljs-string">'sepal_length'</span>, y=featurex, source=confM) <span class="hljs-selector-tag">p</span><span class="hljs-selector-attr">[i]</span><span class="hljs-selector-class">.title</span><span class="hljs-selector-class">.text</span> = f<span class="hljs-string">'sepal_len vs {featurex} '</span></pre></div><div id="2091"><pre><span class="hljs-attr">res</span>=[p[i] for i in range(<span class="hljs-number">4</span>)] <span class="hljs-attr">grid_layout</span> = grid(res, nrows=<span class="hljs-number">2</span>)</pre></div><div id="5382"><pre><span class="hljs-function"><span class="hljs-title">show</span><span class="hljs-params">(grid_layout)</span></span></pre></div><figure id="682f"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*l2DE9vs1bCNxQdpzdkfK-g.png"><figcaption></figcaption></figure><p id="4ee6"><b>Example 3.</b> Create histogram and dashboard. Following example is plotting histograms. we use dataframe generated by pandas.describe() function to illustrate the functionality</p><div id="07b2"><pre>confM.<span class="hljs-built_in">describe</span>()</pre></div><figure id="e88b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*YiyY94cag7g_QE0WeEEwoQ.png"><figcaption></figcaption></figure><div id="ff43"><pre>p=[0]*len(confM) <span class="hljs-attribute">i</span>=0 <span class="hljs-keyword">for</span> stat, dfrow <span class="hljs-keyword">in</span> confM.describe().iterrows(): <span class="hljs-attribute">xlabels</span>=dfrow.index.tolist() p[i] = figure(<span class="hljs-attribute">x_range</span>=xlabels, <span class="hljs-attribute">width</span>=300, <span class="hljs-attribute">height</span>=200) p[i].vbar(<span class="hljs-attribute">x</span>=dfrow.index.tolist(), <span class="hljs-attribute">top</span>=dfrow.values.tolist(), <span class="hljs-attribute">width</span>=0.9) p[i].title.text = f<span class="hljs-string">'{stat} '</span> i+=1</pre></div><div id="2315"><pre><span class="hljs-attr">res</span>=[p[i] for i in range(<span class="hljs-number">8</span>)] <span class="hljs-attr">grid_layout</span> = grid(res, nrows=<span class="hljs-number">3</span>)</pre></div><div id="39c5"><pre><span class="hljs-function"><span class="hljs-title">show</span><span class="hljs-params">(grid_layout)</span></span></pre></div><figure id="273c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*8PZIadvVER0QD_HFarCpCw.png"><figcaption></figcaption></figure><p id="0fdf"><b>In summary, the key learning is using array to store Bokeh plots and using grid_layout API to arrange multiple plots.</b></p><p id="d5f8">If you found this article useful, please clap or follow or signup my email list, happy learning!</p><p id="09e3"><b>References</b></p><p id="6ac3"><a href="https://readmedium.com/how-to-turn-your-jupyter-notebook-into-a-dashboard-using-bokeh-397e4cf37f9e">https://readmedium.com/how-to-turn-your-jupyter-notebook-into-a-dashboard-using-bokeh-397e4cf37f9e</a></p><p id="7013"><a href="https://docs.bokeh.org/en/latest/docs/gallery.html">https://docs.bokeh.org/en/latest/docs/gallery.html</a></p><p id="7df9"><a href="https://docs.bokeh.org/en/latest/docs/user_guide.html">https://docs.bokeh.org/en/latest/docs/user_guide.html</a></p></article></body>

How to Make Bokeh Dashboard Scalable

photo is from unsplash.com

In our previous article we demonstrated how to turn your Jupyter notebook into a Bokeh dashboard.

One challenge remains that there is no obvious way to automatically create dashboards for multiple features.

Often there are many features in our dataset, manually creating dashboard is tedious and error prone. Although there are many blogs, stackoverlow threads about how to use Bokeh, there is not much articles about how to make Bokeh dashborads scalable.

After few failed attempts, a moment of epiphany comes when I used array to record figure objects. and then the plots are arranged using Gridlayout to plot dashboards. The intuition is since the Bokeh is built on plotly and matplotlib, those operations should be transferable to Bokeh.

A tutorial to automatically create and arrange plots in dashboard is illustrated in this article. The workflow could be used at different stage of your machine learning process (i.e. EDA, model training, building and monitoring). Enjoy!

load dataset and package: we will use seaborn iris dataset

import pandas as pd
import numpy as np
import seaborn as sns
# Embedding plots in Jupyter/Colab Notebook
import pandas_bokeh
from bokeh.io import show, reset_output,output_notebook
from bokeh.plotting import figure
reset_output()
output_notebook()
confM=sns.load_dataset('iris')
confM
exampel of iris dataset

Generate feature list create feature list from dataframe

features=confM.columns
features

we will loop through the feature list and use bokeh plot API to create plot automatically for each feature; and those plots will be arranged automatically in a dashboard fashion. The trick is using array to store the objects and use Gridlayout function to arrange dashboard.

Bokeh Plot Basic Bokeh plots following following template. 1. p[i] if initialized as figure object; 2. plot type such as line, vbar or circle. here you can find more detailed gallery and user guide 3. input x, y variable as a list

 p[i].line(x list, y list, options)
example of plots Bokeh can generate from Bokeh site

Workflows to automatically Generated Dashboard

Example 1: Line plots, Following is an example to plot and create a dashboard automatically.

p=[0]*len(features)
for i, feature in enumerate(features): #wrap in a loop
#1. define the figure object
    p[i] = figure(width=400, height=200)  
#2. plot individual feature
    p[i].line(confM.index.values, confM[feature],   line_color='green', legend_label=f"{feature}")
    p[i].title.text = f'{feature} '
#3. create a plot object list and assign to dashboard in gridlayout 
res=[p[i] for i in range(4)] # create a list from above plot list
grid_layout = grid(res, nrows=2) # design how many rows to show
show(grid_layout) #show the plots

Example 2: create scatter plots and dashboard automatically repeat the same pattern above.

p=[0]*len(features)
for i, featurex in enumerate(features):
    p[i] = figure(width=300, height=300) 
    p[i].circle(x='sepal_length', y=featurex, source=confM)
    p[i].title.text = f'sepal_len vs {featurex} '
res=[p[i] for i in range(4)]
grid_layout = grid(res, nrows=2)
show(grid_layout)

Example 3. Create histogram and dashboard. Following example is plotting histograms. we use dataframe generated by pandas.describe() function to illustrate the functionality

confM.describe()
p=[0]*len(confM)
i=0
for stat, dfrow in confM.describe().iterrows():
    xlabels=dfrow.index.tolist()
    p[i] = figure(x_range=xlabels, width=300, height=200) 
    p[i].vbar(x=dfrow.index.tolist(), top=dfrow.values.tolist(), width=0.9)
    p[i].title.text = f'{stat} '
    i+=1
res=[p[i] for i in range(8)]
grid_layout = grid(res, nrows=3)
show(grid_layout)

In summary, the key learning is using array to store Bokeh plots and using grid_layout API to arrange multiple plots.

If you found this article useful, please clap or follow or signup my email list, happy learning!

References

https://readmedium.com/how-to-turn-your-jupyter-notebook-into-a-dashboard-using-bokeh-397e4cf37f9e

https://docs.bokeh.org/en/latest/docs/gallery.html

https://docs.bokeh.org/en/latest/docs/user_guide.html

Recommended from ReadMedium