ToolBar工具栏设置
① 位置设置
② 移动、放大缩小、存储、刷新
③ 选择
④ 提示框、十字线
1.导入相关模块
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
% matplotlib inline
import warnings
warnings.filterwarnings('ignore')
# 不发出警告
from bokeh.io import output_notebook
output_notebook()
# 导入notebook绘图模块
from bokeh.plotting import figure,show
from bokeh.models import ColumnDataSource
# 导入图表绘制、图标展示模块
# 导入ColumnDataSource模块
2.工具栏 tools-1)设置位置
p = figure(plot_width=300, plot_height=300,
toolbar_location="above")
# 工具栏位置:"above","below","left","right"
p.circle(np.random.randn(100),np.random.randn(100))
show(p)
image.png
2.工具栏 tools-2)移动、放大缩小、存储、刷新
TOOLS = '''
pan, xpan, ypan, #移动,横轴移动, 纵轴移动
box_zoom, #矩形框缩放
wheel_zoom, xwheel_zoom, ywheel_zoom, #--滚轮缩放、只是X轴缩放、只是Y轴缩
zoom_in, xzoom_in, yzoom_in,#点击方放大,、点击后,X轴放大、点击后,Y轴放大
zoom_out, xzoom_out, yzoom_out,#点击方缩小、点击后,X轴缩小、点击后,Y轴缩小
save,reset
'''
p = figure(plot_width=800, plot_height=400,toolbar_location="above",
tools = TOOLS)
# 添加toolbar
# 这里tools = '' 则不显示toolbar
p.circle(np.random.randn(500),np.random.randn(500))
show(p)
image.png
3.工具栏 tools-3)选择
TOOLS = '''
box_select,lasso_select,#多边形选择、矩形选择
reset
'''
p = figure(plot_width=800, plot_height=400,toolbar_location="above",
tools = TOOLS)
# 添加toolbar
p.circle(np.random.randn(500),np.random.randn(500))
show(p)
image.png
4.工具栏 tools-4)提示框、十字线
from bokeh.models import HoverTool
# 用于设置显示标签内容
df = pd.DataFrame({'A':np.random.randn(500)*100,
'B':np.random.randn(500)*100,
'type':np.random.choice(['pooh', 'rabbit', 'piglet', 'Christopher'],500),
'color':np.random.choice(['red', 'yellow', 'blue', 'green'],500)})
df.index.name = 'index'
source = ColumnDataSource(df)
print(df.head())
# 创建数据 → 包含四个标签
hover = HoverTool(tooltips=[
("index", "$index"),
("(x,y)", "($x, $y)"),
("A", "@A"),
("B", "@B"),
("type", "@type"),
("color", "@color"),
])
# 设置标签显示内容
# $index:自动计算 → 数据index
# $x:自动计算 → 数据x值
# $y:自动计算 → 数据y值
# @A:显示ColumnDataSource中对应字段值
p1 = figure(plot_width=800, plot_height=400,toolbar_location="above",
tools=[hover,'box_select,reset,wheel_zoom,pan,crosshair']) # 注意这里书写方式
# 如果不设置标签,就只写hover,例如 tools='hover,box_select,reset,wheel_zoom,pan,crosshair'
p1.circle(x = 'A',y = 'B',source = source,size = 10,alpha = 0.3, color = 'color')
show(p1)
p2 = figure(plot_width=800, plot_height=400,toolbar_location="above",
tools=[hover,'box_select,reset,wheel_zoom,pan'])
p2.vbar(x = 'index', width=1, top='A',source = source)
show(p2)
print(hover)
#结果:
A B type color
index
0 -28.886418 180.721277 piglet green
1 122.123888 14.324484 rabbit green
2 66.196717 -13.033131 Christopher red
3 -17.308534 -65.820978 piglet green
4 7.423024 -105.773032 rabbit red
image.png
image.png
网友评论