散點圖
① 基本散點圖繪制
② 散點圖顏色、大小設置方法
③ 不同符號的散點圖
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 # 導入圖表繪制、圖標展示模塊
p.circle()
# 1、基本散點圖繪制 s = pd.Series(np.random.randn(80)) # 創建數據 p = figure(plot_width=600, plot_height=400) p.circle(s.index, s.values, # x,y值,也可以寫成:x=s.index, y = s.values size=25, color="navy", alpha=0.5, # 點的大小、顏色、透明度(注意,這里的color是線+填充的顏色,同時線和填充可以分別上色,參數如下) fill_color = 'red',fill_alpha = 0.6, # 填充的顏色、透明度 line_color = 'black',line_alpha = 0.8,line_dash = 'dashed',line_width = 2, # 點邊線的顏色、透明度、虛線、寬度 # 同時還有line_cap、line_dash_offset、line_join參數 legend = 'scatter-circle', # 設置圖例 #radius = 2 # 設置點的半徑,和size只能同時選一個 ) # 創建散點圖,基本參數 # bokeh對line和fill是同樣的設置方法 p.legend.location = "bottom_right" # 設置圖例位置 show(p)
2. 散點圖不同 顏色上色/ 散點大小 的方法
兩種為散點圖顏色上色的方法
# 2、散點圖不同 顏色上色/散點大小 的方法 # ① 數據中有一列專門用於設置顏色 / 點大小 from bokeh.palettes import brewer rng = np.random.RandomState(1) df = pd.DataFrame(rng.randn(100,2)*100,columns = ['A','B']) # 創建數據,有2列隨機值 df['size'] = rng.randint(10,30,100) # 設置點大小字段 # colormap1 = {1: 'red', 2: 'green', 3: 'blue'} # df['color1'] = [colormap1[x] for x in rng.randint(1,4,100)] # 調色盤1; df['color1'] = np.random.choice(['red', 'green', 'blue'], 100) #跟上面兩行是一樣的; 這兩種都是在本身的數據中增加size和color1標簽,再去繪制圖標; print(df.head()) p = figure(plot_width=600, plot_height=400) p.circle(df['A'], df['B'], # 設置散點圖x,y值 line_color = 'white', # 設置點邊線為白色 fill_color = df['color1'],fill_alpha = 0.5, # 設置內部填充顏色,這里用到了顏色字段 size = df['size'] # 設置點大小,這里用到了點大小字段,按照size的隨機數去設置點的大小 ) show(p)
n = 8 colormap2 = brewer['Blues'][n] print(colormap2) df['color2'] = [colormap2[x] for x in rng.randint(0,n,100)] # 通過調色盤2的方式 print(df.head()) # # 設置顏色字段 # # 通過字典/列表,識別顏色str # # 這里設置了兩個調色盤,第二個為藍色漸變 p = figure(plot_width=600, plot_height=400) p.circle(df['A'], df['B'], # 設置散點圖x,y值 line_color = 'white', # 設置點邊線為白色 fill_color = df['color2'],fill_alpha = 0.5, # 設置內部填充顏色,這里用到了顏色字段 size = df['size'] # 設置點大小,這里用到了點大小字段,按照size的隨機數去設置點的大小 ) show(p)
# 2、散點圖不同 顏色上色/散點大小 的方法 # ② 遍歷數據分開做圖 rng = np.random.RandomState(1) df = pd.DataFrame(rng.randn(100,2)*100,columns = ['A','B']) df['type'] = rng.randint(0,7,100) print(df.head()) # 創建數據;type是做不同類別的一個分組 colors = ["red", "olive", "darkred", "goldenrod", "skyblue", "orange", "salmon"] # 創建顏色列表 p1 = figure(plot_width=600, plot_height=400) p2 = figure(plot_width=600, plot_height=400) p3 = figure(plot_width=600, plot_height=400) plst = [p1, p2, p3] # for t in df['type'].unique(): for t,pi in zip(df['type'].unique()[:3], plst): #p = figure(plot_width=600, plot_height=400,tools = "pan,wheel_zoom,box_select,lasso_select,reset") pi.circle(df['A'][df['type'] == t], df['B'][df['type'] == t], # 設置散點圖x,y值 size = 20,alpha = 0.5, color = colors[t], legend = 'type%i' % t) show(pi) # 通過分類設置顏色
3. 不同符號的散點圖
# 3、不同符號的散點圖 # asterisk(), circle(), circle_cross(), circle_x(), cross(), diamond(), diamond_cross(), inverted_triangle() # square(), square_cross(), square_x(), triangle(), x() p = figure(plot_width=600, plot_height=400,x_range = [0,3], y_range = [0,7]) p.circle_cross(1, 1, size = 30, alpha = 0.5, legend = 'circle_cross') p.asterisk(1, 2, size = 30, alpha = 0.5, legend = 'asterisk') p.circle_x(1, 3, size = 30, alpha = 0.5, legend = 'circle_x') p.cross(1, 4, size = 30, alpha = 0.5, legend = 'cross') p.diamond(1, 5, size = 30, alpha = 0.5, legend = 'diamond') p.diamond_cross(1, 6, size = 30, alpha = 0.5, legend = 'diamond_cross') p.inverted_triangle(2, 1, size = 30, alpha = 0.5, legend = 'inverted_triangle') p.square(2, 2, size = 30, alpha = 0.5, legend = 'square') p.square_cross(2, 3, size = 30, alpha = 0.5, legend = 'square_cross') p.square_x(2, 4, size = 30, alpha = 0.5, legend = 'square_x') p.triangle(2, 5, size = 30, alpha = 0.5, legend = 'triangle') p.x(2, 6, size = 30, alpha = 0.5, legend = 'x') p.legend.location = "bottom_right" # 設置圖例位置 show(p) # 詳細參數可參考文檔:http://bokeh.pydata.org/en/latest/docs/reference/plotting.html#bokeh.plotting.figure.Figure.circle