Python交互圖表可視化Bokeh:1. 可視交互化原理| 基本設置


 Bokeh

pandas和matplotlib就可以直接出分析的圖表了,最基本的出圖方式。是面向數據分析過程中出圖的工具;Seaborn相比matplotlib封裝了一些對數據的組合和識別的功能;用Seaborn出一些針對seaborn的圖表是很快的,比如說分布圖、熱圖、分類分布圖等。如果用matplotlib需要先group by先分組再出圖;

Seaborn在出圖的方式上,除了圖表的可視化好看,還多了出圖的公用性的東西; 關聯數據用get去做,空間數據用echart、powmart去做。

什么是Bokeh 

基於web端的python數據可視化工具包,可交互()

matplotlib和seaborn都是面向過程的,在數據分析過程中可以呈現;Bokeh是在最后的結果呈現,可做動圖,可只打開一張表,也可以做儀表盤的排版,可做圖表的聯動。

matplotlib是基於numpy和pandas做圖表可視化工具包,而seaborn又是基於matplotlib做的

 

繪圖空間基本設置


① 創建繪圖空間
② 輸出方式
③ 繪圖figure基本設置

輸出:jupyter notebook / spyder等非notebook空間

參考官方文檔:http://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#text-properties

 

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
% matplotlib inline

import warnings
warnings.filterwarnings('ignore') 
# 不發出警告
# 在notebook中創建繪圖空間

from bokeh.plotting import figure,show
# 導入圖表繪制、圖標展示模塊

from bokeh.io import output_notebook
# 導入notebook繪圖模塊

output_notebook()
# notebook繪圖命令

p = figure(plot_width=400, plot_height=400)   # 創建圖表,設置寬度、高度
p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)
# 創建一個圓形散點圖

show(p)
# 繪圖

 

# 在spyder等非notebook中創建繪圖空間

from bokeh.plotting import figure,show,output_file # 導入圖表繪制、圖標展示模塊
# output_file → 非notebook中創建繪圖空間

import os
# os.chdir('C:/Users/Hjx/Desktop/')
os.chdir('C:/Users/Administrator/Desktop')
# 創建工作目錄
output_file("line.html")
# notebook繪圖命令,創建html文件
# 運行后會彈出html窗口
p = figure(plot_width=400, plot_height=400)   # 創建圖表,設置寬度、高度
p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="blue", alpha=0.5)
# 創建一個圓形散點圖

show(p)
# 繪圖

會彈出一個HTML的頁面進行展示:(文件也會存儲在創建的目錄里邊)

 1. 圖表繪制工具 figure()

p = figure(plot_width = 600, plot_height = 400, tools = 'pan,wheel_zoom,box_zoom,save,reset,help', toolbar_location='above', 
           x_axis_label = 'A', y_axis_label = 'B',x_range = [-3,3], y_range = [-3,3],  title="測試圖表" )
p.circle() 繪制散點圖
 
# 創建圖表工具 
# figure()
df = pd.DataFrame(np.random.randn(100, 2), columns = ['A', 'B'])
p = figure(plot_width = 600, plot_height = 400, # 圖表寬度、高度
           tools = 'pan,wheel_zoom,box_zoom,save,reset,help',  # 設置工具欄,默認全部顯示
           toolbar_location='above',     # 工具欄位置:"above","below","left","right"
           x_axis_label = 'A', y_axis_label = 'B',    # X,Y軸label
           x_range = [-3,3], y_range = [-3,3],        # X,Y軸范圍
           title="測試圖表"                          # 設置圖表title
          )
# figure創建圖表,設置基本參數
# tool參考文檔:https://bokeh.pydata.org/en/latest/docs/user_guide/tools.html

p.title.text_color = "red"
p.title.text_font = "times"
p.title.text_font_style = "italic"
p.title.background_fill_color = "black"
# 設置標題:顏色、字體、風格、背景顏色  

p.circle(df['A'], df['B'], size = 20, alpha = 0.5) # 創建散點圖  # 這里.circle()是figure的一個繪圖方法
show(p)

 顏色設置

# 顏色設置

p = figure(plot_width=600, plot_height=400)
# 創建繪圖空間

p.circle(df.index, df['A'], color = 'green', size=10,  alpha=0.5) 
p.circle(df.index, df['B'], color = '#FF0000', size=10,  alpha=0.5) #這兩個散點圖會在一塊一張圖上,如果想分開就p1,p2
show(p)  
# 顏色設置
# ① 147個CSS顏色,參考網址:http://www.colors.commutercreative.com/grid/
# ② RGB顏色值,參考網址:https://coolors.co/87f1ff-c0f5fa-bd8b9c-af125a-582b11

圖表邊框線參數設置

p.outline_line_width = 7         # 邊框線寬
p.outline_line_alpha = 0.3       # 邊框線透明度
p.outline_line_color = "navy"    # 邊框線顏色
p.outline_line_dash = [6, 4]
# 圖表邊框線參數設置

p = figure(plot_width=600, plot_height=400)
p.circle(df.index, df['A'], color = 'green', size=10,  alpha=0.5)
p.circle(df.index, df['B'], color = '#FF0000', size=10,  alpha=0.5)
# 繪制散點圖
 
p.outline_line_width = 7         # 邊框線寬
p.outline_line_alpha = 0.3       # 邊框線透明度
p.outline_line_color = "navy"    # 邊框線顏色
p.outline_line_dash = [6, 4]
# 設置圖表邊框

show(p)

 繪圖空間背景

p.background_fill_color = "beige"    # 繪圖空間背景顏色
p.background_fill_alpha = 0.5        # 繪圖空間背景透明度
# 設置繪圖空間背景

p = figure(plot_width=600, plot_height=400)
p.circle(df.index, df['A'], color = 'green', size=10,  alpha=0.5)
p.circle(df.index, df['B'], color = '#FF0000', size=10,  alpha=0.5)
# 繪制散點圖

p.background_fill_color = "beige"    # 繪圖空間背景顏色
p.background_fill_alpha = 0.5        # 繪圖空間背景透明度
# 背景設置參數

show(p)

 外邊界背景 

p.border_fill_color = "whitesmoke" # 外邊界背景顏色 p.border_fill_alpha = 0.5 #透明度 p.min_border_left = 80 # 外邊界背景 - 左邊寬度 p.min_border_right = 80 # 外邊界背景 - 右邊寬度 p.min_border_top = 10 # 外邊界背景 - 上寬度 p.min_border_bottom = 10 
# 設置外邊界背景

p = figure(plot_width=600, plot_height=400)
p.circle(df.index, df['A'], color = 'green', size=10,  alpha=0.5)
p.circle(df.index, df['B'], color = '#FF0000', size=10,  alpha=0.5)
# 繪制散點圖

p.border_fill_color = "whitesmoke"    # 外邊界背景顏色
p.border_fill_alpha = 0.5             #透明度
p.min_border_left = 80                # 外邊界背景 - 左邊寬度
p.min_border_right = 80               # 外邊界背景 - 右邊寬度
p.min_border_top = 10                 # 外邊界背景 - 上寬度
p.min_border_bottom = 10              # 外邊界背景 - 下寬度

show(p)

軸線(X Y軸)設置 

# Axes - 軸線設置
# 軸線標簽、軸線線寬、軸線顏色
# 字體顏色、字體角度

p = figure(plot_width=400, plot_height=400)
p.circle([1,2,3,4,5], [2,5,8,2,7], size=10)
# 繪制圖表

p.xaxis.axis_label = "Temp"
p.xaxis.axis_line_width = 3
p.xaxis.axis_line_color = "red"
p.xaxis.axis_line_dash = [6, 4] #虛線 6線4個格子
# 設置x軸線:標簽、線寬、軸線顏色

p.yaxis.axis_label = "Pressure"
p.yaxis.major_label_text_color = "orange"
p.yaxis.major_label_orientation = "vertical"
# 設置y軸線:標簽、字體顏色、字體角度

p.axis.minor_tick_in = 20     # 刻度往繪圖區域內延伸長度;設置成負的就是往外邊延伸了。
p.axis.minor_tick_out = 3   # 刻度往繪圖區域外延伸長度
# 設置刻度

p.xaxis.bounds = (2, 4)
# 設置軸線范圍

show(p)

 軸線標簽設置 

# Axes - 軸線設置
# 標簽設置

p = figure(plot_width=400, plot_height=400)
p.circle([1,2,3,4,5], [2,5,8,2,7], size=10)

p.xaxis.axis_label = "Lot Number"
p.xaxis.axis_label_text_color = "#aa6666"
p.xaxis.axis_label_standoff = 30  #偏移距離
# 設置標簽名稱、字體顏色、偏移距離

p.yaxis.axis_label = "Bin Count"
p.yaxis.axis_label_text_font_style = "italic" #字體風格
# 設置標簽名稱、字體

show(p)

Grid格網設置 -- 線型

# Grid - 格網設置
# 線型設置

p = figure(plot_width=600, plot_height=400)
p.circle(df.index, df['A'], color = 'green', size=10,  alpha=0.5)
p.circle(df.index, df['B'], color = '#FF0000', size=10,  alpha=0.5)
# 繪制散點圖

p.xgrid.grid_line_color = 'red'
# 顏色設置,None時則不顯示

p.ygrid.grid_line_alpha = 0.8
p.ygrid.grid_line_dash = [6, 4]
# 設置透明度,虛線設置
# dash → 通過設置間隔來做虛線

p.xgrid.minor_grid_line_color = 'navy'
p.xgrid.minor_grid_line_alpha = 0.1
# minor_line → 設置次軸線

show(p)

格網顏色填充

p.ygrid.band_fill_alpha = 0.1
p.ygrid.band_fill_color = "navy"
# Grid - 格網設置
# 顏色填充

p = figure(plot_width=600, plot_height=400)
p.circle(df.index, df['A'], color = 'green', size=10,  alpha=0.5)
p.circle(df.index, df['B'], color = '#FF0000', size=10,  alpha=0.5)
# 繪制散點圖

p.xgrid.grid_line_color = None
# 設置顏色為空

p.ygrid.band_fill_alpha = 0.1
p.ygrid.band_fill_color = "navy"
# 設置顏色填充,及透明度

#p.grid.bounds = (-1, 1)
# 設置填充邊界

show(p)

Legend圖例的設置 

p.line(x, y, legend="sin(x)")
p.legend.location = "bottom_left" 位置、

p.legend.orientation = "vertical"排列方向 默認
p.legend.label_text_font = "times"
p.legend.label_text_font_style = "italic"  # 斜體
p.legend.label_text_color = "navy"
p.legend.label_text_font_size = '12pt'
# 設置圖例:字體、風格、顏色、字體大小

p.legend.border_line_width = 3
p.legend.border_line_color = "navy"
p.legend.border_line_alpha = 0.5
# 設置圖例外邊線:寬度、顏色、透明度

p.legend.background_fill_color = "gray"
p.legend.background_fill_alpha = 0.2
# 設置圖例背景:顏色、透明度
# Legend - 圖例設置
# 設置方法 → 在繪圖時設置圖例名稱 + 設置圖例位置

p = figure(plot_width=600, plot_height=400)
# 創建圖表

x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)
# 設置x,y

p.circle(x, y, legend="sin(x)")
p.line(x, y, legend="sin(x)")
# 繪制line1,設置圖例名稱

p.line(x, 2*y, legend="2*sin(x)",line_dash=[4, 4], line_color="orange", line_width=2)
# 繪制line2,設置圖例名稱

p.square(x, 3*y, legend="3*sin(x)", fill_color=None, line_color="green")
p.line(x, 3*y, legend="3*sin(x)", line_color="green")
# 繪制line3,設置圖例名稱

p.legend.location = "bottom_left"
# 設置圖例位置:"top_left"、"top_center"、"top_right" (the default)、"center_right"、"bottom_right"、"bottom_center"
# "bottom_left"、"center_left"、"center"

p.legend.orientation = "vertical"
# 設置圖例排列方向:"vertical" (默認)or "horizontal"

p.legend.label_text_font = "times"
p.legend.label_text_font_style = "italic"  # 斜體
p.legend.label_text_color = "navy"
p.legend.label_text_font_size = '12pt'
# 設置圖例:字體、風格、顏色、字體大小

p.legend.border_line_width = 3
p.legend.border_line_color = "navy"
p.legend.border_line_alpha = 0.5
# 設置圖例外邊線:寬度、顏色、透明度

p.legend.background_fill_color = "gray"
p.legend.background_fill_alpha = 0.2
# 設置圖例背景:顏色、透明度

show(p)

總結一下:
Line Properties → 線設置
Fill Properties → 填充設置
Text Properties → 字體設置

1、Line Properties → 線設置
(1)line_color,設置顏色
(2)line_width,設置寬度
(3)line_alpha,設置透明度
(4)line_join,設置連接點樣式:'miter' miter_join,'round' round_join,'bevel' bevel_join
(5)line_cap,設置線端口樣式,'butt' butt_cap,'round' round_cap,'square' square_cap
(6)line_dash,設置線條樣式,'solid','dashed','dotted','dotdash','dashdot',或者整型數組方式(例如[6,4])

2、Fill Properties → 填充設置
(1)fill_color,設置填充顏色
(2)fill_alpha,設置填充透明度

3、Text Properties → 字體設置
(1)text_font,字體
(2)text_font_size,字體大小,單位為pt或者em( '12pt', '1.5em')
(3)text_font_style,字體風格,'normal' normal text,'italic' italic text,'bold' bold text
(4)text_color,字體顏色
(5)text_alpha,字體透明度
(6)text_align,字體水平方向位置,'left', 'right', 'center'
(7)text_baseline,字體垂直方向位置,'top','middle','bottom','alphabetic','hanging'

4、可見性
p.xaxis.visible = False
p.xgrid.visible = False
基本參數中都含有.visible參數,設置是否可見

 


免責聲明!

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



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