Python交互圖表可視化Bokeh:7. 工具欄


 

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模塊

# 工具欄 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)

# 工具欄 tools
# (1)設置位置

p = figure(plot_width=300, plot_height=300,
           toolbar_location="below",
           toolbar_sticky=False)
# 工具欄位置設置為"below"時,可增加toolbar_sticky參數使得toolsbar不被遮擋
p.circle(np.random.randn(100),np.random.randn(100))
show(p)

2. 移動、放大縮小、存儲、刷新 

# 工具欄 tools
# (2)移動、放大縮小、存儲、刷新

TOOLS = '''
        pan, xpan, ypan,             
        box_zoom,
        wheel_zoom, xwheel_zoom, ywheel_zoom,   
        zoom_in, xzoom_in, yzoom_in,
        zoom_out, xzoom_out, yzoom_out,
        save,reset
        '''
#pan是直接移動;xpan和ypan分別是橫軸、豎軸移動;box_zoom是矩形框放大,wheel_zoom滾輪縮放:直接縮放、X軸縮放、Y軸縮放;通過鼠標點擊縮放zoom_in
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)  

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)

#聯動
from bokeh.layouts import gridplot


TOOLS = '''
        box_select,lasso_select,
        reset
        '''
df = pd.DataFrame(np.random.randn(500,2), columns = ['A', 'B'])
source = ColumnDataSource(data=df)

p1 = figure(plot_width=400, plot_height=400,toolbar_location="above",tools = TOOLS)
p2 = figure(plot_width=400, plot_height=400,toolbar_location="above",tools = TOOLS)


p1.circle(x='index', y='A',source=source)
p2.line(x='index', y='B',source=source)
s = gridplot([[p1, p2]])
show(s)

 

 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())
# 創建數據 → 包含四個標簽

p1 = figure(plot_width=800, plot_height=400,toolbar_location="above",
            tools=['hover,box_select,reset,wheel_zoom,pan,crosshair'])   # 注意這里書寫方式;  hover它的作用是只是會顯示出點的每個標簽;crossshair是顯示十字叉
# 如果不設置標簽,就只寫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)

 

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它的作用是只是會顯示出點的每個標簽;crossshair是顯示十字叉
# 如果不設置標簽,就只寫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) #就是一個生成器

HoverTool(id='3b80258a-2940-4c8a-af3e-9a3905cb7c09', ...)

 

5. 篩選數據 

 隱藏

# 1、篩選數據 - 隱藏
# legend.click_policy

from bokeh.palettes import Spectral4
# 導入顏色模塊

df = pd.DataFrame({'A':np.random.randn(500).cumsum(),
                  'B':np.random.randn(500).cumsum(),
                  'C':np.random.randn(500).cumsum(),
                  'D':np.random.randn(500).cumsum()},
                 index = pd.date_range('20180101',freq = 'D',periods=500))
# 創建數據

p = figure(plot_width=800, plot_height=400, x_axis_type="datetime")
p.title.text = '點擊圖例來隱藏數據'

for col,color in zip(df.columns.tolist(),Spectral4):
    p.line(df.index,df[col],line_width=2, color=color, alpha=0.8,legend = col)

p.legend.location = "top_left"
p.legend.click_policy="hide"
# 設置圖例,點擊隱藏

show(p)

消隱

# 1、篩選數據 - 消隱
# legend.click_policy

from bokeh.palettes import Spectral4
# 導入顏色模塊

df = pd.DataFrame({'A':np.random.randn(500).cumsum(),
                  'B':np.random.randn(500).cumsum(),
                  'C':np.random.randn(500).cumsum(),
                  'D':np.random.randn(500).cumsum()},
                 index = pd.date_range('20180101',freq = 'D',periods=500))
# 創建數據

p = figure(plot_width=800, plot_height=400, x_axis_type="datetime")
p.title.text = '點擊圖例來隱藏數據'

for col,color in zip(df.columns.tolist(),Spectral4):
    p.line(df.index,df[col],line_width=2, color=color, alpha=0.8,legend = col,
           muted_color=color, muted_alpha=0.2)   # 設置消隱后的顯示顏色、透明度 可以設置muted_color = 'black'

p.legend.location = "top_left"
p.legend.click_policy="mute"
# 設置圖例,點擊隱藏

show(p)

6. 交互工具 

# 2、交互小工具
# 圖表分頁

from bokeh.models.widgets import Panel, Tabs
# 導入panel,tabs模塊

p1 = figure(plot_width=500, plot_height=300)
p1.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)
tab1 = Panel(child=p1, title="circle")
# child → 頁碼
# title → 分頁名稱

p2 = figure(plot_width=500, plot_height=300)
p2.line([1, 2, 3, 4, 5], [4, 2, 3, 8, 6], line_width=3, color="navy", alpha=0.5)
tab2 = Panel(child=p2, title="line")

tabs = Tabs(tabs=[ tab1, tab2 ])
# 設置分頁圖表

show(tabs)

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM