PythonBokeh库的基本绘图函数
发布时间:2023-12-23 04:03:24
PythonBokeh是一个用于数据可视化的Python库。它可以轻松地创建交互式和动态的图表、图形和仪表板。Bokeh提供了一些基本的绘图函数,以帮助用户快速创建各种类型的图表。下面是一些常用的基本绘图函数,以及它们的使用示例。
1. figure()函数:创建一个图形对象,用来存储和管理图表的所有组件。
from bokeh.plotting import figure, output_file, show
# 创建一个图形对象
p = figure(title="Line Plot", x_axis_label='x', y_axis_label='y')
# 添加数据点
p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2)
# 输出到HTML文件
output_file("line_plot.html")
# 显示图表
show(p)
2. line()函数:绘制直线图。
from bokeh.plotting import figure, output_file, show
# 创建一个图形对象
p = figure(title="Line Plot", x_axis_label='x', y_axis_label='y')
# 添加数据点
p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2)
# 输出到HTML文件
output_file("line_plot.html")
# 显示图表
show(p)
3. scatter()函数:绘制散点图。
from bokeh.plotting import figure, output_file, show
# 创建一个图形对象
p = figure(title="Scatter Plot", x_axis_label='x', y_axis_label='y')
# 添加数据点
p.scatter([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=10)
# 输出到HTML文件
output_file("scatter_plot.html")
# 显示图表
show(p)
4. bar()函数:绘制柱状图。
from bokeh.plotting import figure, output_file, show
# 创建一个图形对象
p = figure(title="Bar Plot", x_axis_label='x', y_axis_label='y')
# 添加数据点
p.vbar(x=[1, 2, 3, 4, 5], top=[6, 7, 2, 4, 5], width=0.5)
# 输出到HTML文件
output_file("bar_plot.html")
# 显示图表
show(p)
5. pie()函数:绘制饼图。
from bokeh.plotting import figure, output_file, show
from bokeh.transform import cumsum
from math import pi
# 创建一个图形对象
p = figure(title="Pie Chart")
# 添加数据点
data = {'Fruits': ['Apples', 'Pears', 'Bananas', 'Strawberries'],
'Quantity': [20, 10, 15, 25]}
p.wedge(x=0, y=0, radius=0.8, start_angle=cumsum('angle', include_zero=True),
end_angle=cumsum('angle'), line_color='white', fill_color='blue',
legend_field='Fruits', source=data)
# 输出到HTML文件
output_file("pie_chart.html")
# 显示图表
show(p)
这些是常用的基本绘图函数及其使用示例。Bokeh库提供了更多的绘图函数和选项,可以根据需要进行深入学习和研究。
