Python交互圖表可視化Bokeh:6. 軸線| 浮動| 多圖表


 

繪圖表達進階操作

① 軸線設置
② 浮動設置
③ 多圖表設置

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

figure(x_range=name, y_range=(60,100), plot_height=350, title="考試成績",tools="")
p.circle(x = 'name', y = 'score', source = source,
         size = 20, line_color = 'black', line_dash = [6,4],
         fill_color = 'red',fill_alpha = 0.8)
# 1、軸線標簽設置
# 設置字符串

df = pd.DataFrame({'score':[98,86,74,67,87]},index = ['小明','小王','小張','小紅','小紅帽'])
df.index.name = 'name'
print(df)
# 創建數據

source = ColumnDataSource(df)
# 講數據轉化為ColumnDataSource對象

name = df.index.tolist()   # 提取name
print(name)
p = figure(x_range=name, y_range=(60,100), plot_height=350, title="考試成績",tools="")
# 通過x_range設置橫軸標簽,這里提取成list

p.circle(x = 'name', y = 'score', source = source,
         size = 20, line_color = 'black', line_dash = [6,4],
         fill_color = 'red',fill_alpha = 0.8)

show(p)

--->>>

      score
name       
小明       98
小王       86
小張       74
小紅       67
小紅帽      87
['小明', '小王', '小張', '小紅', '小紅帽']

設置時間序列 

# 1、軸線標簽設置  時間序列設置
# Dataframe DatetimeIndex + x_axis_type

from bokeh.sampledata.commits import data
print(data.head())
print(type(data.index))#datetime是個時間序列
# 導入數據,查看數據
# 這里index為時間序列

DAYS = ['Sun', 'Sat', 'Fri', 'Thu', 'Wed', 'Tue', 'Mon']
source = ColumnDataSource(data)
# 轉化為ColumnDataSource對象

p = figure(plot_width=800, plot_height=600, 
           y_range=DAYS,                     # 設置圖表的y軸刻度分類
           x_axis_type='datetime',           # 設置x軸類型 → 時間序列
           title="Commits by Time of Day (US/Central) 2012-2016")

p.circle(x='time', y='day',  source=source, alpha=0.2) #如果把x='datetime',放大會顯示時間每個年月時刻/
# 生成散點圖

p.ygrid.grid_line_color = None
# 設置其他參數

show(p)

設置對數坐標軸 

# 1、軸線標簽設置
# 設置對數坐標軸

x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y = [10**xx for xx in x]
# 創建數據

p = figure(plot_width=400, plot_height=400, 
           y_axis_type="log")    #如果不設置y_axis_type = "log"就是一個對數的展示;
# y_axis_type="log" → 對數坐標軸

p.line(x, y, line_width=2)
p.circle(x, y, fill_color="white", size=8)

show(p)

2. 浮動設置 

# 2、浮動設置(多了個在上下空間的浮動)
# Jitter
# 參考文檔:https://bokeh.pydata.org/en/latest/docs/reference/transform.html

from bokeh.transform import jitter

DAYS = ['Sun', 'Sat', 'Fri', 'Thu', 'Wed', 'Tue', 'Mon']
source = ColumnDataSource(data)
# 轉化為ColumnDataSource對象

p = figure(plot_width=800, plot_height=600, 
           y_range=DAYS,                     # 設置圖表的y軸刻度分類
           x_axis_type='datetime',           # 設置x軸類型 → 時間序列
           title="Commits by Time of Day (US/Central) 2012-2016",
          tools = 'hover') #會對每個點進行識別

p.circle(x='time', 
         y=jitter('day', width=0.6, range=p.y_range), 
         source=source, alpha=0.3)
# jitter參數 → 'day':第一參數,這里指y的值,width:間隔寬度比例,range:分類范圍對象,這里和y軸的分類一致

p.ygrid.grid_line_color = None
# 設置其他參數

show(p)

3. 多圖表設置  

# 3、多圖表設置(通過范圍做的一個共享數據)
# gridplot

from bokeh.layouts import gridplot
# 導入gridplot模塊

x = list(range(11))
y0 = x
y1 = [10-xx for xx in x]
y2 = [abs(xx-5) for xx in x]
# 創建數據  #當這些數據用的是一組數據的時候就會形成聯動,主要就是設置x_range=s1.x_range, y_range=s1.y_range,如果把s2 s3里里邊去掉就不是用的一個數據了就不會產生聯動

s1 = figure(plot_width=250, plot_height=250, title=None) #這個類似就是子圖,在x_range和y_range上做一個共享
s1.circle(x, y0, size=10, color="navy", alpha=0.5)
# 散點圖1

s2 = figure(plot_width=250, plot_height=250, x_range=s1.x_range, y_range=s1.y_range, title=None) #如果設置了tools='box_select'以放大篩選時不會有聯動,但在移動時候會有聯動
s2.triangle(x, y1, size=10, color="firebrick", alpha=0.5)
# 散點圖2,設置和散點圖1一樣的x_range/y_range → 圖表聯動

s3 = figure(plot_width=250, plot_height=250, x_range=s1.x_range, title=None)
s3.square(x, y2, size=10, color="olive", alpha=0.5)
# 散點圖3,設置和散點圖1一樣的x_range/y_range → 圖表聯動

p = gridplot([[s1, s2, s3]])  #把這3個圖全部集合,做一個聯動;
#p = gridplot([[s1, s2], [None, s3]])
# 組合圖表

show(p) #show(s1)  show(s2)  show(s3) 這樣就不會產生聯動了,去掉p = gridplot那個

# 3、多圖表設置(共享一個數據)
# gridplot

x = list(range(-20, 21))
y0 = [abs(xx) for xx in x]
y1 = [xx**2 for xx in x]
source = ColumnDataSource(data=dict(x=x, y0=y0, y1=y1)) #把數據轉換成ColumnDataSource,共享數據,同一個數據源
# 創建數據

TOOLS = "box_select,lasso_select,help"

left = figure(tools=TOOLS, plot_width=300, plot_height=300, title=None)
left.circle('x', 'y0', source=source)    # 散點圖1 left和right設置了同一個source

right = figure( plot_width=300, plot_height=300, title=None) #如果把tools=TOOLS去掉, 就是沒有共享范圍,在篩選放大時就不會有聯動了;但由於用的是同一個數據,在移動時還說有聯動
right.circle('x', 'y1', source=source)   # 散點圖2 
# 共用一個ColumnDataSource

p = gridplot([[left, right]])
# 組合圖表

show(p)

 


免責聲明!

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



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