<jupyter_start><jupyter_text>Explanation of Bokeh PackagesFor bokeh library, we will use some packages:* output_file: that save our figure with .html extension* show: show the figure* figure: creates empty figure* ColumnarDataSource: Data source of bokeh* HoverTool: like cursor* Row and column: puts plots in row order or column order in figure* gridplot* Tabs and Panel: Panel is figure for each plot and tab is like button<jupyter_code>!pip install bokeh

import numpy as np 
import pandas as pd

def get_path(dataset_name,env_name='colab'):
    """
    This function is used to return the path of the dataset you want to use. 
    
    @params:
    dataset_name: the name of the dataset. 
    env_name: it has two values either local, or colab the default is colab
    """
    prefix = 'https://raw.githubusercontent.com/John-Ghaly88/Data_Engineering_And_Visualization_Course_Labs/tree/main/Datasets/Labs/'
    if env_name == 'colab':
        return prefix+dataset_name
    else:
        return f'../Datasets/{dataset_name}'
# bokeh packages
from bokeh.io import output_file,show,output_notebook,push_notebook
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource,HoverTool,BoxSelectTool
from bokeh.layouts import row,column,gridplot
from bokeh.models.widgets import Tabs,Panel
output_notebook()<jupyter_output><empty_output><jupyter_text>Plotting with Glyphs* Glyphs: visual shapes like circle, square, rectangle or diamond* figure: creates figure    * x_axis_label: label of x axis    * y_axis_label: label of y axis    * tools: tools to move or zoom plot        * pan: slides the plot        * box_zoom: zoom in * output_file:  that save our figure with .html extension* show: show the figure  Line Graph * line: line plot    * line_width: width of line    * fill_color: filling inside of circle with color<jupyter_code>plot = figure(x_axis_label = "x",y_axis_label = "y")
plot.line(x=[4,5,2,3,1],y=[1,2,3,4,5],line_color = "black",legend_label='Line',width=5)
plot.circle(x=[4,5,2,3,1],y=[1,2,3,4,5],fill_color = "yellow",size = 10,alpha = 0.7,legend_label='Line')
output_file("my_first_bokeh_plot.html")
show(plot)

google = pd.read_csv(get_path('GOOGL_data.csv'))
fb     = pd.read_csv(get_path('FB_data.csv'))
apple = pd.read_csv(get_path('AAPL_data.csv'))
amazon = pd.read_csv(get_path('AMZN_data.csv'))
microsoft = pd.read_csv(get_path('MSFT_data.csv'))
google.head()
f = figure()
x =np.arange(len(google.close))
f.line(x,google.close,legend_label='Google')
f.line(x,fb.close,legend_label='Facebook')
f.line(x,amazon.close,legend_label='Amazon')
f.line(x,microsoft.close,legend_label='Microsoft')
f.line(x,apple.close,legend_label='Apple')
x = google.date
f.xaxis.major_label_overrides = {x:y for (x,y) in enumerate(x)}
show(f)<jupyter_output><empty_output><jupyter_text>Scatter Graphs * circle: like scatter in matplotlib    * size: size of circles    * color: color    * alpha: opacity* Other markers:     * asterisk()     * circle()     * circle_cross()     * circle_x()     * cross()     * diamond()     * diamond_cross()     * inverted_triangle()     * square()     * square_cross()     * square_x()     * triangle()     * x()<jupyter_code>plot = figure(x_axis_label = "x",y_axis_label = "y",tools = "pan,box_zoom")
plot.add_tools(BoxSelectTool())
plot.asterisk(x=[5,4,3,2,1],y=[1,2,3,4,5],color = "black",size = 10,alpha = 0.7)
show(plot)

# There are other types of glyphs
plot = figure()
plot.diamond(x=[5,4,3,2,1],y=[1,2,3,4,5],size = 10,color = "black",alpha = 0.7)
plot.cross(x=[1,2,3,4,5],y=[1,2,3,4,5],size = 10,color = "red",alpha = 0.7)
show(plot)
# line
plot = figure()
plot.line(x=[1,2,3,4,5,6,7],y = [1,2,3,4,5,5,5],line_width = 2)
plot.circle(x=[1,2,3,4,5,6,7],y = [1,2,3,4,5,5,5],fill_color = "white",size = 10)
show(plot)<jupyter_output><empty_output><jupyter_text>Bar Charts<jupyter_code>movie_scores = pd.read_csv(get_path('movie_scores.csv'))
movie_scores
f = figure(tools=['box_select'])
x = np.arange(1,len(movie_scores.Tomatometer)+1)
width = 0.2
f.vbar(x=x-width/2, top= movie_scores.Tomatometer,bottom=0 , width=width,color='Green')
f.vbar(x=x+width/2, top= movie_scores.AudienceScore,bottom=0 , width=width)
f.xaxis.major_label_overrides = {x+1:y for (x,y) in enumerate(movie_scores.MovieTitle.values)}
show(f)<jupyter_output><empty_output><jupyter_text>Data FormatsBokeh can use list, numpy arrays and pandas as a data source. We have pandas data frame in this tutorial.  Customizing Glyphs* Selection appearance: when you select some point on data, that points shine and others burn out    * tools:        * box_select and lasso_select: selection tools    * selection_color: When you select point, it becomes selected color    * nonselection_fill_alpha: Other non selected points become non selected alpha    * nonselection_fill_color: Other non selected points become non selected color* HoverTool: cursor    * Crosshair: line cursor    * hover_color: Color of hover* Color mapping: color map of chose field. (like hue in seaborn)    * factors: names of variable to color map    * palette: color of chose factors<jupyter_code>x = np.linspace(0, 10, 1000)
y = np.sin(x) + np.random.random(1000) * 0.2
plot = figure()
plot.line(x, y)
show(plot)
hover_tool = HoverTool()
f = figure(tools=[hover_tool,'crosshair'])
f.line(x,y)
show(f)
#Hover appearance
from bokeh.models import HoverTool
hover = HoverTool(tooltips=None)
plot = figure(tools=[hover, 'crosshair','box_select','lasso_select'])
# x and y are lists of random points
plot.circle(x, y, size=15, hover_color='green',selection_color = "orange")
show(plot)
# Selection appearance
source= ColumnDataSource(google)
plot = figure(tools="box_select,lasso_select")
plot.circle(x="close",y = "open",source=source,color = "black",
            selection_color = "orange",
            nonselection_fill_alpha = 0.2,
           nonselection_fill_color = "blue")
show(plot)
# Hover appearance
hover = HoverTool(tooltips = [("The closing price","@close"),("The opening price","@open")], mode="hline")
plot = figure(tools=[hover,"crosshair"])
plot.circle(x= "close",y = "open",source=source,color ="black",hover_color ="red")
show(plot)<jupyter_output><empty_output><jupyter_text>LayoutsArranging multiple plots like subplot in matplot library.* Row and columns: puts plots in row order or column order in figure* Grid arrangement: list of order for layout    * toolbar_location: location of tool bar that can be below above left right or none* Tabbed layout    * Panel: like a figure    * Tabs: like a button<jupyter_code># Row and column
p1 = figure()
p1.circle(x = "close",y= "open",source = source,color="red")
p2 = figure()
p2.circle(x = "open",y= "high",source = source,color="black")
p3 = figure()
p3.circle(x = "close",y= "low",source = source,color="blue")
p4 = figure()
p4.circle(x = "open",y= "volume",source = source,color="orange")
layout1 = row(p1,p2)
layout2 = row(p3,p4)
layout3= column(layout1,layout2)
show(layout3)
#nested
# I use p1, p2 and p3 that are created at above
layout = row(column(p1,p2),p3)
show(layout)
# Grid plot 
layout = gridplot([[p1,p2],[p3,None]],toolbar_location="above")
show(layout)

#Tabbed layout
#I use p1 and p2 that are created at above
tab1 = Panel(child = p1,title = "close vs. open")
tab2 = Panel(child = p2,title = "open vs. high")
tabs = Tabs(tabs=[tab1,tab2])
show(tabs)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup