<jupyter_start><jupyter_text>Bokeh Tutorial**<NAME>**\IES, Academia Sinica<jupyter_code>import numpy as np
from bokeh.plotting import figure
from bokeh.io import output_file, save, show, output_notebook
output_notebook()<jupyter_output><empty_output><jupyter_text>Referencehttps://bokeh.pydata.org/en/latest/docs/reference.html  Let's make some fake data and quickly plot it interactively We can set the figure size as the arguments of the figure object.<jupyter_code>x = [1,2,3,4,5]
y = [6,7,8,9,10]
p = figure(plot_width=300, plot_height=300)
circ=p.circle(x,y)
show(p)<jupyter_output><empty_output><jupyter_text>Styling the markers For the list of markers:https://bokeh.pydata.org/en/latest/docs/reference/plotting.htmlbokeh-plotting - asterisk()- circle()- circle_cross()- circle_x()- cross()- diamond()- diamond_cross()- inverted_triangle()- square()- square_cross()- square_x()- triangle()- x()<jupyter_code># output_file("bokeh_tutorial.html")
x = np.random.rand(10)*10
y = np.random.rand(10)*10
p = figure(plot_width=300, plot_height=300)
p.circle(x,y,radius=0.1,fill_color='red',fill_alpha=0.7,line_dash="dashed",legend='circle')
p.triangle(np.random.rand(10)*10,np.random.rand(10)*10,legend='triangle')
p.line(np.random.rand(10)*10,np.random.rand(10)*10,line_color='black',legend='line')

p.background_fill_color='lightgray'
show(p)<jupyter_output><empty_output><jupyter_text>Title<jupyter_code>p.title.text = "Random Scatter"
p.title.text_color = "black"
p.title.text_font = "times"
p.title.text_font_size = "25px"
p.title.align = "center"

show(p)<jupyter_output><empty_output><jupyter_text>Styling the axes<jupyter_code>from bokeh.models import Range1d
p.axis.minor_tick_line_color = "blue"
p.xaxis.minor_tick_line_color = "red"
# p.xaxis.minor_tick_line_color = None
# p.yaxis.minor_tick_line_color = None

p.yaxis.major_label_orientation = "vertical"
p.xaxis.visible = True
p.xaxis.axis_label = "X-axis"
p.yaxis.axis_label = "Y-axis"

p.axis.axis_label_text_color = "blue"
p.axis.major_label_text_color = "orange"

# Axes Geometry
# p.x_range = Range1d(start=0,end=15)
p.x_range = Range1d(start=0,end=10,bounds=(-10,20))
p.y_range = Range1d(start=0,end=10)
p.xaxis[0].ticker.desired_num_ticks=2
p.yaxis[0].ticker.desired_num_ticks=10
show(p)<jupyter_output><empty_output><jupyter_text>Styling the grid<jupyter_code>p.xgrid.grid_line_color = "gray"
p.xgrid.grid_line_alpha = 0.3
p.ygrid.grid_line_color = "gray"
p.ygrid.grid_line_alpha = 0.3
p.grid.grid_line_dash = [5,3]
show(p)<jupyter_output><empty_output><jupyter_text>Legend<jupyter_code># p.legend.location = (1,1)
p.legend.location = 'top_left'
p.legend.background_fill_alpha = 0.6
p.legend.border_line_color = None
p.legend.margin = 10
p.legend.padding = 18
p.legend.label_text_color = 'olive'
p.legend.label_text_font = 'times'
show(p)<jupyter_output><empty_output><jupyter_text>ColumnDataSource<jupyter_code>from bokeh.sampledata.iris import flowers
flowers.head()
colormap={'setosa':'red','versicolor':'green','virginica':'blue'}
flowers['color'] = [colormap[x] for x in flowers['species']]
flowers['size'] = flowers['sepal_width'] * 5
flowers.tail()
from bokeh.models import ColumnDataSource
setosa = ColumnDataSource(flowers[flowers["species"]=="setosa"])
versicolor = ColumnDataSource(flowers[flowers["species"]=="versicolor"])
virginica = ColumnDataSource(flowers[flowers["species"]=="virginica"])
p = figure(height=500,width=500)
p.circle(x="petal_length", y="petal_width", size='size', fill_alpha=0.2, 
color="color", legend='Setosa', source=setosa)

p.circle(x="petal_length", y="petal_width", size='size', fill_alpha=0.2, 
color="color", legend='Versicolor', source=versicolor)

p.circle(x="petal_length", y="petal_width", size='size', fill_alpha=0.2,
color="color", legend='Virginica', source=virginica)
p.legend.location = 'top_left'
p.xaxis.axis_label = "Petal Length"
p.yaxis.axis_label = "Petal Width"
p.title.text = "Petal plot"
p.legend.click_policy='hide'
show(p)<jupyter_output><empty_output><jupyter_text>Configuring Toolbars<jupyter_code>from bokeh.models import PanTool, ResetTool, HoverTool, WheelZoomTool, BoxZoomTool, HoverTool, SaveTool
p.tools = [PanTool(),ResetTool(), WheelZoomTool(), BoxZoomTool(), SaveTool()]
hover = HoverTool(tooltips=[("Species","@species"), ("Sepal Width","@sepal_width")])
p.add_tools(hover)
p.toolbar_location = 'above'
p.toolbar.logo = None
show(p)<jupyter_output><empty_output><jupyter_text>Layout<jupyter_code>p2 = figure(height=500,width=500)
p2.circle(x="sepal_length", y="sepal_width", size='size', fill_alpha=0.2, 
color="color", legend='Setosa', source=setosa)

p2.circle(x="sepal_length", y="sepal_width", size='size', fill_alpha=0.2, 
color="color", legend='Versicolor', source=versicolor)

p2.circle(x="sepal_length", y="sepal_width", size='size', fill_alpha=0.2,
color="color", legend='Virginica', source=virginica)
p2.legend.location = 'top_left'
p2.xaxis.axis_label = "Sepal Length"
p2.yaxis.axis_label = "Sepal Width"
p2.tools = [PanTool(),ResetTool(), WheelZoomTool(), BoxZoomTool(), SaveTool()]
hover2 = HoverTool(tooltips=[("Species","@species"), ("Sepal Width","@petal_width")])
p2.add_tools(hover2)
p2.toolbar_location = 'above'
p2.toolbar.logo = None
p2.title.text = "Sepal plot"
p2.legend.click_policy='hide'
show(p2)
from bokeh.layouts import column
show(column(p, p2))
from bokeh.layouts import row
show(row(p, p2))
from bokeh.layouts import gridplot
layout1 = gridplot([[p, p2]], toolbar_location='right')
show(layout1)
layout2 = gridplot([[p, None],[None,p2]], toolbar_location='right')
show(layout2)
from bokeh.models.widgets import Tabs, Panel
# Create two panels
panel1 = Panel(child=p, title='Petal')
panel2 = Panel(child=p2, title='Sepal')
# Assign the panels to Tabs
tabs = Tabs(tabs=[panel1, panel2])

# Show the tabbed layout
show(tabs)<jupyter_output><empty_output><jupyter_text>Selecting Data Points<jupyter_code>select_tools = ['box_select', 'lasso_select', 'poly_select', 'tap', 'reset']
p = figure(height=500,width=500,x_axis_label='Petal Length',y_axis_label="Petal Width"
           ,title="Petal Plot",toolbar_location='above',tools=select_tools)
p.circle(x="petal_length", y="petal_width", size='size', fill_alpha=0.2, 
color="color", legend='Setosa', source=setosa,selection_color='deepskyblue',
           nonselection_color='lightgray',
           nonselection_alpha=0.3)

p.circle(x="petal_length", y="petal_width", size='size', fill_alpha=0.2, 
color="color", legend='Versicolor', source=versicolor,selection_color='deepskyblue',
           nonselection_color='lightgray',
           nonselection_alpha=0.3)

p.circle(x="petal_length", y="petal_width", size='size', fill_alpha=0.2,
color="color", legend='Virginica', source=virginica,selection_color='deepskyblue',
           nonselection_color='lightgray',
           nonselection_alpha=0.3)
p.legend.location = 'top_left'
p.legend.click_policy='hide'

p.toolbar.logo = None
show(p)<jupyter_output><empty_output><jupyter_text>Linking plots<jupyter_code>plot_options = dict(width=250, plot_height=250, tools='pan,wheel_zoom,reset')
x = list(range(11))
y0, y1, y2 = x, [10-i for i in x], [abs(i-5) for i in x]

# create a new plot
s1 = figure(**plot_options,title='s1: linked x and y with s2')
s1.circle(x, y0, size=10, color="navy")

# create a new plot and share both ranges
s2 = figure(x_range=s1.x_range, y_range=s1.y_range, **plot_options,title='s2: linked x and y with s1')
s2.triangle(x, y1, size=10, color="firebrick")

# create a new plot and share only one range
s3 = figure(x_range=s1.x_range, **plot_options,title='s3: linked x with s1 and s2')
s3.square(x, y2, size=10, color="olive")

p = gridplot([[s1, s2, s3]])

# show the results
show(p)
from bokeh.sampledata.iris import flowers

flowers_cds = ColumnDataSource(flowers)
TOOLS = ["box_select",'lasso_select', 'reset']
# create a new plot and add a renderer
left = figure(tools=TOOLS, width=400, height=300,title='sepal length vs. width')
left.circle('sepal_width', 'sepal_length', source=flowers_cds)

# create another new plot and add a renderer
right = figure(tools=TOOLS, width=400, height=300,title='petal length vs. width')
right.circle('petal_width', 'petal_length', source=flowers_cds)

p = gridplot([[left, right]])

show(p)
flowers.head()
tips = [
            ("sepal_length", "@sepal_length"),
            ("petal_length", "@petal_length"),
            ("species", "@species"),
        ]
TOOLS= 'lasso_select box_select reset'.split()
hover1 = HoverTool(tooltips=tips)

left = figure(tools=[hover1, *TOOLS], width=300, height=400)

left.circle('petal_length', 'petal_width', source=flowers_cds,color='red', size=10, alpha=0.3 )
left.xaxis.axis_label = 'petal_length'
left.yaxis.axis_label = 'petal_width'


hover2 = HoverTool(tooltips=tips)

right = figure(tools=[hover2, *TOOLS], width=300, height=400)

right.circle('sepal_length', 'sepal_width', source=flowers_cds,color='green', size=10, alpha=0.3) 
right.xaxis.axis_label = 'sepal_length'
right.yaxis.axis_label = 'sepal_width'

p = gridplot([[left, right]])
show(p)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16359217938804815, 'fraction_numerical': 0.02640008836849663, 'mean_word_length': 7.461682242990654, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '309811', 'license': 'MIT', 'n_tokens_mistral': 3619, 'n_tokens_neox': 3453, 'n_words': 578, 'path': 'Bokeh Tutorial.ipynb', 'repo_name': 'utpalkumariesas/bokehTutorial', 'size': '9058'}