search
HomeBackend DevelopmentPython TutorialHow to use Bokeh to build interactive data visualization applications

How to use Bokeh to build interactive data visualization applications

Introduction:
In today's big data era, data visualization is very important. Through visualization technology, we can display data in the form of graphics to better understand the characteristics and trends of the data. Bokeh is a powerful Python library that provides a rich set of tools and functions for building interactive data visualization applications. This article describes how to use Bokeh to build interactive data visualization applications, along with code examples.

1. Install Bokeh
First, we need to install the Bokeh library. Open the command line window and enter the following command:

pip install bokeh

2. Basic concepts
Before we start, we need to understand some basic concepts. Bokeh provides two basic forms of interfaces: low-level and high-level interfaces. Low-level interfaces are the basic building blocks of the Bokeh library, through which users can build custom visualization components; while high-level interfaces create common visualizations in a more convenient and faster way. This article mainly introduces the high-level interface.

Bokeh is based on plot objects, which can be charts, icons, tables, or more complex combinations. We can create and modify these drawing objects using Bokeh's high-level interface. To display these objects we need an output mode, there are several options to choose from including displaying in the browser, saving to a file or generating a static image.

3. Quick Start
Next, let us implement a simple interactive data visualization application. We take the iris data set as an example, visualize it as a scatter plot, and implement some interactive functions.

First, we need to import the required libraries and modules:

import pandas as pd
from bokeh.plotting import figure, show
from bokeh.io import output_notebook, output_file
from bokeh.models import ColumnDataSource, CategoricalColorMapper, HoverTool

Then, we load the iris data set and create a Bokeh drawing object:

# 加载鸢尾花数据集
iris = pd.read_csv('iris.csv')

# 创建绘图对象
plot = figure(title='鸢尾花数据集', 
              x_axis_label='花瓣长度', y_axis_label='花瓣宽度',
              plot_width=600, plot_height=400)

Continue Next, we plot the data in the dataset into a scatter plot and use colors to represent flower types:

# 创建颜色映射器
color_mapper = CategoricalColorMapper(factors=['setosa', 'versicolor', 'virginica'], 
                                      palette=['red', 'green', 'blue'])

# 添加散点图
plot.circle(x='petal_length', y='petal_width', 
            color={'field': 'species', 'transform': color_mapper},
            size=10, alpha=0.5, source=ColumnDataSource(iris))

Using the above code, we plot a scatter plot where the x-axis represents the petal length and the y-axis represents the petal length. The axis indicates petal width, and colors are used to indicate flower species.

Next, we add some interactive functions, such as displaying data when the mouse hovers:

# 添加悬停工具
hover = HoverTool(tooltips=[('花的种类', '@species'),
                            ('花瓣长度', '@petal_length'),
                            ('花瓣宽度', '@petal_width')])
plot.add_tools(hover)

Using the above code, when the mouse hovers over the scatter point, the type of flower, Information on petal length and petal width.

Finally, we select the specific output mode and display the drawing object:

# 在浏览器中显示
output_notebook()

# 显示绘图对象
show(plot)

Through the above steps, we successfully implemented a simple interactive data visualization application, which can be used to hover over the See the details of each data point on the scatter plot.

Conclusion:
Bokeh is a very powerful Python library that can help us build interactive data visualization applications. This article briefly introduces the basic steps to build interactive data visualization applications using Bokeh's high-level interface, and attaches code examples. I hope that readers can understand the basic usage of Bokeh through the introduction of this article, and use it flexibly in practice to build more advanced and complex data visualization applications.

The above is the detailed content of How to use Bokeh to build interactive data visualization applications. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.