https://docs.bokeh.org/en/latest/
通常 bokeh 画图的流程包括这六步:
准备画图数据
导入相关库
指定输出方式
创建新画布
添加字形
显示结果
字形最常用的视觉参数包括:
1)x:横坐标
2)y:纵坐标
3)alpha:透明度(取值范围在0-1之间)
4)size:大小
5)fill_color:填充颜色
6)line_color:轮廓颜色
常用函数
annulus() 圆环,
annular_wedge() 环形楔形
wedge() 楔型,
rect()矩形,
quad()轴对齐四边形
vbar() 垂直条形,
hbar()水平条形,
image() 给定标量数据和颜色映射的图像
image_rgba() 给定RGBA数据的图像 ,
image_url() 给定URL数据的图像
patch() 单个块
patches() 多个块
line() 线,
multi_line() 多条线
circle() 圆形
oval()卵型
ellipse() 椭圆型
arc() 弧形
quadratic() 抛物线
brzier()贝塞尔曲线。
line_width:线条宽度,默认是1;
line_color:线条颜色;
line_alpha:线条透明度;
line_dash:线型,可以是 “solid”(实线),
“dashed”(虚线), “dotted”(点线),
“dotdash”(点横线)等。
作者:Cherich_sun
链接:https://www.jianshu.com/p/e81501648648
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
from bokeh.plotting import figure, output_file, show
output_file("test.html")
p = figure()
p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2)
output_notebook()
show(p)
from bokeh.plotting import figure, output_notebook, show
from bokeh.io import reset_output
# 准备数据
x = [1, 2.5, 3, 2]
y = [2, 3, 1, 1.5]
# 创建新画布
p = figure(plot_width=500, plot_height=500,
x_axis_label='x', y_axis_label='y',
tools="pan,reset,save")
# 添加一个圆形的字形
p.circle(x, y, radius=0.3, alpha=0.5)
p.min_border = 40 # 设置四周边距一致
# 指定输出到 notebook 中
output_notebook()
# 输出到web网页
# output_file('circle.html')
# 显示结果
show(p)
from bokeh.plotting import figure, output_notebook, show
# 准备画图所需的数据:折点的坐标数据
x = [1, 2, 3, 4, 5] # 表示每个折点的横坐标
y = [6, 7, 2, 4, 5] # 表示每个折点的纵坐标
# 指定输出在notebook中
output_notebook()
# 创建一个带有图标题和坐标轴标签的画布
p = figure(title="简单折线图", x_axis_label='x', y_axis_label='y', plot_width=500, plot_height=500)
# 添加一条折线
p.line(x, y, line_width=2, color='red', line_dash='dashdot')
# 设置图距四周的距离都是40
p.min_border = 40
output_notebook()
# 显示结果
show(p)
import numpy as np
from bokeh.plotting import figure, output_notebook, show
# 准备数据:x 是等差数列,从 0 到 4*pi,一共100个元素
x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)
# 创建画布
p = figure(title="多条线的图", plot_width=500, plot_height=500)
# 画第一条线,添加圆形标记,设置图例是 "sin(x)",颜色线型都默认
p.line(x, y, legend_label="sin(x)")
p.circle(x, y, legend_label="sin(x)")
# 画第二条线,设置线型是虚线,图例是 "2*sin(x)",颜色是橘色,线宽是2
p.line(x, 2 * y, legend_label="2*sin(x)", line_dash='dashed', line_color="orange", line_width=2)
# 画第三条线,添加正方形标记,轮廓颜色是绿色,图例是 "3*sin(x)"
p.square(x, 3 * y, legend_label="3*sin(x)", fill_color=None, line_color="green")
p.line(x, 3 * y, legend_label="3*sin(x)", line_color="green")
p.min_border = 40
output_notebook()
show(p)
from bokeh.plotting import figure, output_notebook, show
xs = [[2, 2, 4], [2, 3, 4, 3]]
ys = [[3, 5, 5], [3, 4, 3, 2]]
p = figure()
p.patches(xs, ys,
fill_color=['coral', 'purple'],
line_color='white',
fill_alpha=0.6)
output_notebook()
show(p)
条形图
bokeh 使用 hbar() 和 vbar() 来画条形图(水平)和柱状图(垂直)。
hbar() 的常用参数有:
y:水平条形中心的 y 坐标
height:水平条形的高度
right:水平条形右边缘的 x 坐标
left:水平条形左边缘的 x 坐标
fill_color:条形的填充颜色
fill_alpha:条形内部的透明度
line_alpha:条形轮廓的透明度
line_color:条形轮廓的颜色
from bokeh.plotting import figure, output_notebook, show
plot = figure(plot_width=500, plot_height=500)
plot.hbar(y=[1, 2, 3, 4], height=0.5, left=0, right=[1, 2, 3, 2.5], color="skyblue")
output_notebook()
show(plot)
from bokeh.plotting import figure, output_notebook, show
# 准备数据
x = [1, 2, 3, 4, 5] # 柱子所处的横坐标
y = [1.2, 2.5, 3.7, 1.5, 2.9] # 柱子的高度
p = figure(plot_width=500, plot_height=500)
# 画柱状图
p.vbar(x, top=y, width=0.5, bottom=0,
color="firebrick")
p.grid.visible = False # 去除网格线
p.min_border = 40
output_notebook()
show(p)
数据可视化一定要准备数据,bokeh可以用作输入数据的数据格式有:
列表(直接提供数据)
Numpy 数组
DataFrame 的列
列数据源(Column Data Source)
# 导入需要的函数
from bokeh.plotting import figure, output_notebook, show
from bokeh.models import ColumnDataSource
# 数据组成的字典:键是字符串,值是列表
data = {'x_values': [1, 2, 3, 4, 5],
'y_values': [6, 7, 2, 3, 6]}
# 创建列数据源,参数是刚才创建的数据的字典,字典的键成为列数据源的列名。
source = ColumnDataSource(data=data)
# 创建画布
p = figure(plot_width=500, plot_height=500)
# 绘制圆形字形,参数 x,y 不是数据,而是列数据源的列名,source 参数指定列数据源作为数据
p.circle(x='x_values', y='y_values', source=source)
p.min_border = 40
output_notebook()
show(p)
# 导入相关函数
from bokeh.plotting import figure, output_notebook, show, ColumnDataSource
# 导入数据集
from bokeh.sampledata.iris import flowers as df
# 创建列数据源
source = ColumnDataSource(df)
# 创建画布,设置工具栏
p = figure(plot_width=500, plot_height=500, tools='box_select, lasso_select', title='请用鼠标选择区域尝试!!!')
# 创建字形,使用列数据源,指定选择外观
p.circle(x='petal_length', y='sepal_length', source=source,
selection_color='red', # 选中的颜色
nonselection_alpha=0.2, # 未选中的点的透明度
nonselection_color='grey') # 未选中的点的颜色
p.min_border = 40
output_notebook()
show(p)
#这就非常不错了。
from bokeh.plotting import figure, output_notebook, show
p = figure(plot_width=500, plot_height=500, tools="tap", title="选择一个圆")
p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=50,
# 设置选中字形的视觉属性
selection_color="firebrick",
# 设置未选中字形的视觉属性
nonselection_fill_alpha=0.2,
nonselection_fill_color="blue",
nonselection_line_color="firebrick",
nonselection_line_alpha=1.0)
p.min_border = 40
output_notebook()
show(p)
# 导入相关函数
from bokeh.models import HoverTool
from bokeh.plotting import figure, output_notebook, show
import numpy as np
# 设置悬停工具,不显示每个点的提示,悬停处的垂直线上的点突出显示
hover = HoverTool(tooltips=None, mode='vline')
# 设置工具栏包含悬停显示工具和十字准线工具
p = figure(plot_width=500, plot_height=500, tools=[hover, 'crosshair'], title="自定义悬停效果")
# x , y 分别是 500 个随机数组成的数组
x = np.random.random(size=500) # 返回[0,1)之间的随机数(浮点数)
y = np.random.random(size=500)
# 在字形函数中设置悬停字形的颜色
p.circle(x, y, size=15, hover_color='orange')
p.min_border = 40
output_notebook()
show(p)
from bokeh.plotting import figure, output_notebook, show
# 导入相关函数
from bokeh.models import CategoricalColorMapper, ColumnDataSource
# 导入数据集
from bokeh.sampledata.iris import flowers as df
# 创建列数据源
source = ColumnDataSource(df)
# 创建类别颜色映射器,指定类别数据和调色板的颜色
mapper = CategoricalColorMapper(
factors=['setosa', 'virginica', 'versicolor'],
palette=['red', 'green', 'blue'])
# 创建画布
p = figure(x_axis_label='petal_length',
y_axis_label='sepal_length',
plot_width=500, plot_height=500)
# 添加圆形字形,指定color参数使用颜色映射器
p.circle('petal_length', 'sepal_length',
size=10, source=source,
color={'field': 'species', 'transform': mapper})
p.min_border = 40
output_notebook()
show(p)
ColumnDataSource(id='5420', ...)
import bokeh.sampledata
bokeh.sampledata.download()
Creating /home/yquqing/.bokeh directory
Creating /home/yquqing/.bokeh/data directory
Using data directory: /home/yquqing/.bokeh/data
Downloading: CGM.csv (1589982 bytes)
1589982 [100.00%]
Downloading: US_Counties.zip (3171836 bytes)
3171836 [100.00%]
Unpacking: US_Counties.csv
Downloading: us_cities.json (713565 bytes)
713565 [100.00%]
Downloading: unemployment09.csv (253301 bytes)
253301 [100.00%]
Downloading: AAPL.csv (166698 bytes)
166698 [100.00%]
Downloading: FB.csv (9706 bytes)
9706 [100.00%]
Downloading: GOOG.csv (113894 bytes)
113894 [100.00%]
Downloading: IBM.csv (165625 bytes)
165625 [100.00%]
Downloading: MSFT.csv (161614 bytes)
161614 [100.00%]
Downloading: WPP2012_SA_DB03_POPULATION_QUINQUENNIAL.zip (4816256 bytes)
4816256 [100.00%]
Unpacking: WPP2012_SA_DB03_POPULATION_QUINQUENNIAL.csv
Downloading: gapminder_fertility.csv (64346 bytes)
64346 [100.00%]
Downloading: gapminder_population.csv (94509 bytes)
94509 [100.00%]
Downloading: gapminder_life_expectancy.csv (73243 bytes)
73243 [100.00%]
Downloading: gapminder_regions.csv (7781 bytes)
7781 [100.00%]
Downloading: world_cities.zip (645274 bytes)
645274 [100.00%]
Unpacking: world_cities.csv
Downloading: airports.json (6373 bytes)
6373 [100.00%]
Downloading: movies.db.zip (5053420 bytes)
5053420 [100.00%]
Unpacking: movies.db
Downloading: airports.csv (203190 bytes)
203190 [100.00%]
Downloading: routes.csv (377280 bytes)
377280 [100.00%]
Downloading: haarcascade_frontalface_default.xml (930127 bytes)
930127 [100.00%]
from math import pi
import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.sampledata.stocks import MSFT
df = pd.DataFrame(MSFT)[:50]
df["date"] = pd.to_datetime(df["date"])
inc = df.close > df.open
dec = df.open > df.close
w = 12*60*60*1000 # half day in ms
TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1000, title = "MSFT Candlestick")
p.xaxis.major_label_orientation = pi/4
p.grid.grid_line_alpha=0.3
p.segment(df.date, df.high, df.date, df.low, color="black")
p.vbar(df.date[inc], w, df.open[inc], df.close[inc], fill_color="#D5E1DD", line_color="black")
p.vbar(df.date[dec], w, df.open[dec], df.close[dec], fill_color="#F2583E", line_color="black")
#output_file("candlestick.html", title="candlestick.py example")
output_notebook()
show(p) # open a browser
import numpy as np
from scipy.integrate import odeint
from bokeh.plotting import figure, output_file, show
sigma = 10
rho = 28
beta = 8.0/3
theta = 3 * np.pi / 4
def lorenz(xyz, t):
x, y, z = xyz
x_dot = sigma * (y - x)
y_dot = x * rho - x * z - y
z_dot = x * y - beta* z
return [x_dot, y_dot, z_dot]
initial = (-10, -7, 35)
t = np.arange(0, 100, 0.006)
solution = odeint(lorenz, initial, t)
x = solution[:, 0]
y = solution[:, 1]
z = solution[:, 2]
xprime = np.cos(theta) * x - np.sin(theta) * y
colors = ["#C6DBEF", "#9ECAE1", "#6BAED6", "#4292C6", "#2171B5", "#08519C", "#08306B",]
p = figure(title="Lorenz attractor example", background_fill_color="#fafafa")
p.multi_line(np.array_split(xprime, 7), np.array_split(z, 7),
line_color=colors, line_alpha=0.8, line_width=1.5)
#output_file("lorenz.html", title="lorenz.py example")
output_notebook()
show(p)