An interactive visual Python Library -- Bokeh

This is the tenth article of Python data visualization. Let's learn an interactive visualization Python library, Bokeh.

Bokeh Foundation

Bokeh is an interactive visual Python library for Web browser rendering. This is the core difference between bokeh and other visualization libraries.

Bokeh drawing steps

① Get data

② Build canvas figure()

③ Add layer, draw line, circle, square, scatter, multi line, etc.; parameter co lor,legend

④ Custom visual properties

⑤ Selective display of broken line data, establishment of check box to activate display, check box

Import libraries and data

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

Illustrator example

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 web page form
p = figure(plot_width=100, plot_height=100)
#data
N=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.annular_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)

2. Multi class scatter diagram

from bokeh.sampledata.iris import flowers
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
#Color matching
colormap={'setosa':'red','versicolor':'green','virginica':'blue'}
colors=[colormap[x] for x in flowers['species']]
#canvas
p=figure(title='Tris Morphology')
#Mapping
#Flowers ['petal'u length '] is x, flowers ['petal'u width'] is y, fill'u alpha = 0.3 is fill transparency
p.circle(flowers['petal_length'],flowers['petal_width'],color=colors,fill_alpha=0.3,size=10)   
#display
output_notebook()
show(p)

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

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)

4. line

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 labels, canvas size
p = figure(title="line example", x_axis_label='x', y_axis_label='y', width=400, height=400)
# Drawing: data, legend, lineweight
p.line(x, y, legend="Temp.", line_width=2)  # Broken line diagram
# display
output_notebook()
show(p)

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

# Data, showing different functions at the same time, in the form of scattered points and broken lines
x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y0 = [i**2 for 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 index or linear
# Adding layers, drawing
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")
# display
output_notebook()
show(p)

6. Different colors and shapes represent different kinds of things

# Data, showing different functions at the same time, in the form of scattered points and broken lines
x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y0 = [i**2 for 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 index or linear
# Adding layers, drawing
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")
# display
output_notebook()
show(p)

7. Different function settings create check box library selective display

x = np.linspace(0, 4 * np.pi, 100)
# canvas
p = figure()
# Broken line attribute
props = dict(line_width=4, line_alpha=0.7)
# Drawing 3 function sequences
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)
# The check box is activated and displayed. The three function sequences can be displayed selectively
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 layers
layout = row(checkbox, p)
output_notebook()
# display
show(layout)

8. Time 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() is used to place multiple images in the same image in rows
from bokeh.palettes import Viridis3
from bokeh.models import CheckboxGroup, CustomJS  #CheckboxGroup create check box 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')
# canvas
p = figure(width=800, height=350, x_axis_type="datetime")
# layer
p.circle(aapl_dates, aapl, size=4, color='darkgrey', alpha=0.2, legend='close') #Scatter plot
p.line(aapl_dates, aapl_avg, color='red', legend='avg') #Broken line sequence diagram
# Custom visual properties
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.ygrid.band_fill_alpha = 0.1
p.legend.click_policy="hide" # Click the legend to show the hidden data
# Display result
output_notebook()
show(p)

Tags: Python Attribute

Posted on Tue, 17 Mar 2020 10:37:57 -0700 by emmbec