In this article, we will learn about Bokeh, an interactive Python visualization library.

Bokeh basis

Bokeh is an interactive visual Python library specifically for rendering capabilities in Web browsers. This is the core difference between Bokeh and other visualization libraries.

Bokeh drawing steps

① Obtaining data

② Construct canvas figure()

Add layers, draw line, Circle, square, Scatter, multi_line, etc. Parameters co LOR, Legend

④ Customize visual attributes

⑤ Selectively display broken line data, set up checkbox to activate display, checkbox

Import libraries and data

import numpy as np
import bokeh
from bokeh.layouts import gridplot
from bokeh.plotting import figure, output_file, show
Copy the code

The chart instance

1. Scatter plot

import numpy as np
import bokeh
from bokeh.layouts import gridplot
from bokeh.plotting import figure, output_file, show
# output_file("patch.html") # output webpage form
p = figure(plot_width=100, plot_height=100)
# dataN = 9 x = np. Linspace (2, 2, N) y = x * * 2 sizes = np. Linspace (10, 20, N) XPTS = np. Array ([0.09, 0.12, 0.0, 0.12, 0.09]) Ypts = np. Array ([0.1, 0.02, 0.1, 0.02, 0.1]) p = figure (title ="annular_wedge") p.a nnular_wedge (x, y, 10,20,0.3, 4.1, color ="#8888ee",inner_radius_units="screen",outer_radius_units="screen")
# Set to output the plot in the notebook
output_notebook()
show(p)
Copy the code

2. Scatterplot of multiple categories

from bokeh.sampledata.iris import flowers
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
# color
colormap={'setosa':'red'.'versicolor':'green'.'virginica':'blue'}
colors=[colormap[x] for x in flowers['species']]
# the canvas
p=figure(title='Tris Morphology')
# drawing
#flowers['petal_length'] = x,flowers['petal_width'] = y, fill_alpha=0.3
p.circle(flowers['petal_length'],flowers['petal_width'], color = colors, fill_alpha = 0.3, size = 10)# show
output_notebook()
show(p)
Copy the code

3. The value is represented by the size of scatter diagram

import numpy as np
from bokeh.sampledata.iris import flowers
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
x=[1,2,3,4]
y=[5,7,9,12]
sizes=np.array(y)+10  # Bubble size
p=figure(title='bubble chart')
p=figure(plot_width=300,plot_height=300)
p.scatter(x,y,marker="circle",size=sizes,color="navy")
output_notebook()
show(p)
Copy the code

4. Line chart

from bokeh.layouts import column, gridplot
from bokeh.models import BoxSelectTool, Div
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
# data
x = [1, 2, 3, 4, 5, 6, 7]
y = [6, 7, 2, 4, 5, 10, 4]
# Canvas: Axis label, canvas size
p = figure(title="line example", x_axis_label='x', y_axis_label='y', width=400, height=400)
# Drawing: data, legend, line width
p.line(x, y, legend="Temp.", line_width=2)  # line chart
# show
output_notebook()
show(p)
Copy the code

5. Show different functions at the same time, in the form of scatter and broken line

# Data, showing different functions at the same time, in the form of scatter and broken lineX = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0] y0 = [I * * 2for i in x]
y1 = [10**i for i in x]
y2 = [10**(i**2) for i in x]
# create canvas
p = figure(
    tools="pan,box_zoom,reset,save",
    y_axis_type="log", title="log axis example",
    x_axis_label='sections', y_axis_label='particles',
    width=700, height=350)  # y axis type: log exponential or linear
# Add layers and draw
p.line(x, x, legend="y=x")
p.circle(x, x, legend="y=x", fill_color="white", size=8)
p.line(x, y0, legend="y=x^2", line_width=3)
p.line(x, y1, legend="y=10^x", line_color="red")
p.circle(x, y1, legend="y=10^x", fill_color="red", line_color="red", size=6)
p.line(x, y2, legend="y=10^x^2", line_color="orange", line_dash="4 4")
# show
output_notebook()
show(p)
Copy the code

Different colors and shapes show different kinds of things

# Data, showing different functions at the same time, in the form of scatter and broken lineX = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0] y0 = [I * * 2for i in x]
y1 = [10**i for i in x]
y2 = [10**(i**2) for i in x]
# create canvas
p = figure(
    tools="pan,box_zoom,reset,save",
    y_axis_type="log", title="log axis example",
    x_axis_label='sections', y_axis_label='particles',
    width=700, height=350)  # y axis type: log exponential or linear
# Add layers and draw
p.line(x, x, legend="y=x")
p.circle(x, x, legend="y=x", fill_color="white", size=8)
p.line(x, y0, legend="y=x^2", line_width=3)
p.line(x, y1, legend="y=10^x", line_color="red")
p.circle(x, y1, legend="y=10^x", fill_color="red", line_color="red", size=6)
p.line(x, y2, legend="y=10^x^2", line_color="orange", line_dash="4 4")
# show
output_notebook()
show(p)
Copy the code

7. Different function Settings create check box libraries for selective display

x = np.linspace(0, 4 * np.pi, 100)
# the canvas
p = figure()
# Broken line propertyProps = dict (line_width = 4, line_alpha = 0.7)# Draw 3 sequences of functions
l0 = p.line(x, np.sin(x), color=Viridis3[0], legend="Line 0", **props)
l1 = p.line(x, 4 * np.cos(x), color=Viridis3[1], legend="Line 1", **props)
l2 = p.line(x, np.tan(x), color=Viridis3[2], legend="Line 2", **props)
# checkbox activate display, checkbox, three function sequences can be optionally displayed
checkbox = CheckboxGroup(labels=["Line 0"."Line 1"."Line 2"],
                         active=[0, 1, 2], width=100)
#
checkbox.callback = CustomJS(args=dict(l0=l0, l1=l1, l2=l2, checkbox=checkbox), code="""
l0.visible = 0 in checkbox.active;
l1.visible = 1 in checkbox.active;
l2.visible = 2 in checkbox.active;
""")
# Add layer
layout = row(checkbox, p)
output_notebook()
# show
show(layout)
Copy the code

8. Sequence chart trend and scatter chart of closing price

import numpy as np
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
from bokeh.layouts import row  #row() puts multiple images in a row in the same image
from bokeh.palettes import Viridis3
from bokeh.models import CheckboxGroup, CustomJS  #CheckboxGroup Creates a checkbox library
# data
aapl = np.array(AAPL['adj_close'])
aapl_dates = np.array(AAPL['date'], dtype=np.datetime64)
window_size = 30
window = np.ones(window_size)/float(window_size)
aapl_avg = np.convolve(aapl, window, 'same')
# the canvas
p = figure(width=800, height=350, x_axis_type="datetime")
# layer
p.circle(aapl_dates, aapl, size=4, color='darkgrey', legend = alpha = 0.2'close') # a scatter diagram
p.line(aapl_dates, aapl_avg, color='red', legend='avg') # Broken line sequence diagram
# Customize visual attributes
p.title.text = "AAPL One-Month Average"
p.legend.location = "top_left"
p.grid.grid_line_alpha=0
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = 'Price'
p.ygrid.band_fill_color="gray"P.y grid. Band_fill_alpha = 0.1 p.l egend. Click_policy ="hide" Click the legend to show hidden data
# display result
output_notebook()
show(p)
Copy the code