SoFunction
Updated on 2025-04-27

The use of Bokeh, a powerful tool for interactive visualization in Python

1. Introduction to Bokeh

Bokeh is a Python library focusing on interactive data visualization on the web. It is rendered based on JavaScript's BokehJS, so that the generated chart can be directly embedded in HTML and supports interactive operations. Compared with traditional static drawing libraries such as Matplotlib and Seaborn, Bokeh has obvious advantages in handling large-scale data and interactivity.

1.1 Why choose Bokeh

  • Strong interactiveness: supports interactive functions such as zoom, pan, hover prompts.
  • Efficient rendering: Use WebGL to improve the drawing performance of large-scale datasets.
  • Compatible with Pandas: Can directly process DataFrame data.
  • Easy to embed: Visualization results can be embedded into HTML, Flask, Django, and Jupyter Notebook.

1.2 Installation and Environment Configuration

Installing Bokeh is very simple, you can install it directly through pip:

pip install bokeh

After installation, you can test it in a Python environment:

from  import figure, show
from  import output_file

output_file("")  # Generate HTML filesp = figure(title="Example Picture", x_axis_label="X-axis", y_axis_label="Y-axis")
([1, 2, 3, 4], [10, 20, 30, 40], line_width=2)
show(p)  # Show charts in browser

After running the code, an HTML page will be opened in the default browser to display a simple line chart.

2. Bokeh Basics

The core concepts of Bokeh mainly include:

  • figure: Drawing area, used to create charts.
  • glyph: Visualize primitives, such as lines, points, bar charts, etc.
  • ColumnDataSource: Data source, easy to manage data and interaction.
  • output_file/output_notebook: Specify the output method.
  • show/save: Show or save the chart.

2.1 Creating a basic drawing

Bokeh offers a variety of basic chart types, including line charts, scatter charts, bar charts, etc. Here are some common examples.

2.1.1 Line chart

from  import figure, show

p = figure(title="Line Chart Example", x_axis_label="X", y_axis_label="Y")
([1, 2, 3, 4, 5], [5, 7, 2, 3, 6], line_width=2, color="blue")
show(p)

2.1.2 Scatter plot

p = figure(title="Scatter plot example", x_axis_label="X", y_axis_label="Y")
([1, 2, 3, 4, 5], [5, 7, 2, 3, 6], size=10, color="red", alpha=0.5)
show(p)

2.1.3 Bar chart

from  import show
from  import figure
from  import factor_cmap
from  import ColumnDataSource

fruits = ["apple", "banana", "orange", "Grape"]
values = [10, 20, 15, 30]

source = ColumnDataSource(data=dict(fruits=fruits, values=values))
p = figure(x_range=fruits, title="Fruit Sales", toolbar_location=None, tools="")
(x="fruits", top="values", width=0.4, source=source)
show(p)

3. Interactive functions

One of the highlights of Bokeh is interactive visualization, mainly throughHoverToolTapToolBoxSelectTooland other tools to implement.

3.1 Mouse hover to display data

from  import HoverTool

p = figure(title="Hoom prompt example", x_axis_label="X", y_axis_label="Y")
([1, 2, 3, 4], [10, 20, 30, 40], size=10, color="navy", alpha=0.5)

hover = HoverTool(tooltips=[("X-axis", "$x"), ("Y-axis", "$y")])
p.add_tools(hover)
show(p)

3.2 Select and Zoom

p = figure(title="Select and Zoom Example", tools="box_select,pan,wheel_zoom,reset")
([1, 2, 3, 4], [10, 20, 30, 40], size=10, color="green", alpha=0.5)
show(p)

4. Data flow processing

Bokeh supports dynamic data updates and is suitable for real-time data visualization, such as sensor data, stock market data, etc.

4.1 Dynamic data update

from  import ColumnDataSource
from  import figure, curdoc
import numpy as np

source = ColumnDataSource(data=dict(x=[], y=[]))
p = figure(title="Dynamic Data Flow", x_axis_label="X", y_axis_label="Y")
("x", "y", source=source, line_width=2)

def update():
    new_data = dict(x=[()], y=[()])
    (new_data, rollover=50)

curdoc().add_root(p)
curdoc().add_periodic_callback(update, 1000)  # Update once every second

When running this code, the Bokeh server continuously updates the data and displays the curve changes in real time in the browser.

5. Bokeh integrates with Pandas, Flask/Django

Bokeh can process data in conjunction with Pandas and integrate with Flask or Django for web applications.

5.1 Bokeh + Pandas

import pandas as pd
data = ({"x": [1, 2, 3, 4], "y": [10, 20, 30, 40]})
source = ColumnDataSource(data)

p = figure(title="Pandas Data Plot")
("x", "y", source=source, line_width=2)
show(p)

5.2 Bokeh + Flask

from flask import Flask, render_template
from  import components

app = Flask(__name__)

@("/")
def index():
    p = figure(title="Flask Integration Example")
    ([1, 2, 3, 4], [10, 20, 30, 40])
    script, div = components(p)
    return render_template("", script=script, div=div)

if __name__ == "__main__":
    (debug=True)

6. Summary

Bokeh is one of the most powerful interactive visualization tools in the Python ecosystem for large-scale data, web embedding, and dynamic data flow visualization. Its flexibility, ease of use and powerful interaction capabilities make it an ideal choice for data science, financial analytics, and IoT data visualization.

This is the article about the use of Bokeh, a powerful tool for Python interactive visualization. For more related Python Bokeh content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!