Bokeh數(shù)據(jù)可視化工具2繪圖進(jìn)階

其他數(shù)據(jù)結(jié)構(gòu)繪圖

使用numpy創(chuàng)建線狀圖

#Creating line plots using NumPy arrays
#Import required packages
import numpy as np
import random
from bokeh.io import output_file, show
from bokeh.plotting import figure

#Creating an array for the points along the x and y axes
array_x =np.array([1,2,3,4,5,6])
array_y = np.array([5,6,7,8,9,10])

#Creating a line plot
plot = figure()
plot.line(array_x, array_y)

#Output the plot
output_file('numpy_line.html')
show(plot)

image.png

使用numpy創(chuàng)建散列圖

#Creating scatter plots using NumPy arrays
#Import required packages
import numpy as np
import random
from bokeh.io import output_file, show
from bokeh.plotting import figure

#Creating arrays for two different categories of points
x_red = np.array([1,2,3,4,5])
y_red = np.array([5,6,7,8,9])
x_blue = np.array([10,11,12,13])
y_blue = np.array([14,15,16,17])

#Creating the categorical scatter plot 

plot = figure()
plot.circle(x_red, y_red, size = 9, color = 'red', alpha = 0.8)
plot.circle(x_blue, y_blue, size = 9, color = 'blue', alpha = 0.8)

#Output the plot 
output_file('numpy_scatter.html')
show(plot)
image.png

使用pandas DataFrame創(chuàng)建時(shí)序圖

蘋果股票的高值:

#Creating a time series plot using a Pandas DataFrame
#Importing the required packages
import pandas as pd

#Read in the data
df = pd.read_csv('/home/andrew/code/kaggle-code/stock_data/all_stocks_5yr.csv')

#Filtering for apple stocks
df_apple = df[df['Name'] == 'AAL']
#df_apple.loc['date'] = df_apple['date'].astype('datetime64')
df_apple['date'] = pd.to_datetime(df_apple['date'])
print(df_apple.dtypes)

#Import the required packages
from bokeh.io import output_file, show
from bokeh.plotting import figure

#Create the time series plot
plot = figure(x_axis_type = 'datetime', x_axis_label = 'date', y_axis_label = 'High Prices')
plot.line(x = df_apple['date'], y = df_apple['high'])

#Output the plot
output_file('pandas_time.html')
show(plot)

image.png
  • 參考資料

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.astype.html
https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas

另外一個更簡單的演示:

import pandas as pd
import numpy as np
from bokeh.plotting import figure, output_file, show
from bokeh.layouts import row

output_file('fig.html')

test = pd.DataFrame({'datetime':pd.date_range('1/1/1880', periods=2000),'foo':np.arange(2000)})

print(test['datetime'])

fig = figure(x_axis_type="datetime")
fig.line(x='datetime',y='foo', source=test)

test = test.set_index('datetime')

fig2 = figure(x_axis_type="datetime")
fig2.line(x='datetime', y='foo', source=test)
show(row(fig, fig2))
image.png
  • 參考資料

https://stackoverflow.com/questions/34974615/timeseries-in-bokeh-using-a-dataframe-with-index

https://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html

https://github.com/CNuge/kaggle-code

  • 同一行創(chuàng)建多個圖
  • 同一列創(chuàng)建多個圖
  • 行和列中創(chuàng)建多個圖
  • 選項(xiàng)卡式布局創(chuàng)建多個繪圖
  • 創(chuàng)建強(qiáng)大的網(wǎng)格布局
  • 將多個圖表鏈接在一起

同一行創(chuàng)建多個圖

bokeh自帶了很多數(shù)據(jù)日麸,可以用如下方式下載:

In [2]: import bokeh

In [3]: bokeh.sampledata.download()
Creating /home/andrew/.bokeh directory
Creating /home/andrew/.bokeh/data directory
Using data directory: /home/andrew/.bokeh/data
Downloading: CGM.csv (1589982 bytes)
   1589982 [100.00%]
Downloading: US_Counties.zip (3182088 bytes)
   3182088 [100.00%]
Unpacking: US_Counties.csv
Downloading: us_cities.json (713565 bytes)
    713565 [100.00%]
Downloading: unemployment09.csv (253301 bytes)
    253301 [100.00%]
Downloading: AAPL.csv (166698 bytes)
    166698 [100.00%]
Downloading: FB.csv (9706 bytes)
      9706 [100.00%]
Downloading: GOOG.csv (113894 bytes)
    113894 [100.00%]
Downloading: IBM.csv (165625 bytes)
    165625 [100.00%]
Downloading: MSFT.csv (161614 bytes)
    161614 [100.00%]
Downloading: WPP2012_SA_DB03_POPULATION_QUINQUENNIAL.zip (5148539 bytes)
   5148539 [100.00%]
Unpacking: WPP2012_SA_DB03_POPULATION_QUINQUENNIAL.csv
Downloading: gapminder_fertility.csv (64346 bytes)
     64346 [100.00%]
Downloading: gapminder_population.csv (94509 bytes)
     94509 [100.00%]
Downloading: gapminder_life_expectancy.csv (73243 bytes)
     73243 [100.00%]
Downloading: gapminder_regions.csv (7781 bytes)
      7781 [100.00%]
Downloading: world_cities.zip (646858 bytes)
    646858 [100.00%]
Unpacking: world_cities.csv
Downloading: airports.json (6373 bytes)
      6373 [100.00%]
Downloading: movies.db.zip (5067833 bytes)
   5067833 [100.00%]
Unpacking: movies.db
Downloading: airports.csv (203190 bytes)
    203190 [100.00%]
Downloading: routes.csv (377280 bytes)
    377280 [100.00%]
#Preparing all the plots needed for this chapter

import pandas as pd
from bokeh.sampledata.stocks import AAPL

df_apple = pd.DataFrame(AAPL)
df_apple['date'] = pd.to_datetime(df_apple['date'])


#Import the required packages

from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.plotting import ColumnDataSource


#Create the ColumnDataSource object

data = ColumnDataSource(data = {
    'x' : df_apple['high'],
    'y' : df_apple['low'],
    'x1': df_apple['open'],
    'y1': df_apple['close'],
    'x2': df_apple['date'],
    'y2': df_apple['volume'], 
})


#Create the first scatter plot
plot1 = figure()
plot1.cross(x = 'x', y = 'y', source = data, color = 'red', size = 10, alpha = 0.8)

#Create the second scatter plot
plot2 = figure()
plot2.circle(x = 'x1', y = 'y1', source = data, color = 'green', size = 10, alpha = 0.3)

#Create the third scatter plot
plot3 = figure(x_axis_type = 'datetime', x_axis_label = 'date', y_axis_label = 'Volume Traded')
plot3.line(x = 'x2', y = 'y2', source = data, color = 'red')
plot3.circle(x = 'x2', y = 'y2', source = data, fill_color = 'white', size = 3)


#Output the 3 plots
#Output the plot1
output_file('first_plot.html')
show(plot1)

#Output the plot2
output_file('second_plot.html')
show(plot2)


#Output the plot3
output_file('third_plot.html')
show(plot3)


#Creating multiple plots along the same row
#Import the required packages
from bokeh.layouts import row
from bokeh.io import output_file, show

#Group the 3 plots into a 'row' layout
row_layout = row(plot1,plot2,plot3)

#Output the plot
output_file('horizontal.html')
show(row_layout)


#Creating multiple plots along the same column

#Import the required packages

from bokeh.layouts import column
from bokeh.io import output_file, show

#Group the 2 plots into a 'column' layout
col_layout = column(plot1,plot2)

#Output the plot
output_file('vertical.html')
show(col_layout)

#Creating multiple plots along a row and column

#Import the required packages

from bokeh.layouts import column, row
from bokeh.io import output_file, show

#Construct the nested layout
nested_layout = column(row(plot1,plot2), plot3)

#Output the plot
output_file('nested.html')
show(nested_layout)



#Creating multiple plots on a tabbed layout

#Import the required packages
from bokeh.models.widgets import Tabs, Panel
from bokeh.io import output_file, show
from bokeh.layouts import column, row

#Create the two panels 

tab1 = Panel(child = plot1, title = 'Tab One')
tab2 = Panel(child = column(plot2,plot3), title = 'Tab Two')

#Feed the tabs into a Tabs object
tabs_object = Tabs(tabs = [tab1, tab2])

#Output the plot
output_file('tab_layout.html')
show(tabs_object)



#Creating a grid layout
#Import required packages

from bokeh.io import output_file, show
from bokeh.layouts import gridplot

#Create the grid layout
grid_layout = gridplot([plot1, plot2], [plot3, None])

#Output the plot
output_file('grid.html')
show(grid_layout)


#Linking multiple plots along the y axis
#Import the required packages

from bokeh.io import output_file, show
from bokeh.layouts import row

#Creating equal y axis ranges
plot3.y_range = plot1.y_range

#Create the row layout
row_layout = row(plot3, plot1)

#Output the plot
output_file('grid.html')
show(row_layout)



#Linking multiple plots along the x axis
#Import the required packages
from bokeh.io import output_file, show
from bokeh.layouts import row

#Creating equal x-axis ranges
plot2.x_range = plot1.x_range

#Create the row layout
row_layout = row(plot2, plot1)

#Output the plot
output_file('row.html')
show(row_layout)
圖片.png
圖片.png
圖片.png
圖片.png
圖片.png
圖片.png
圖片.png
圖片.png
圖片.png
圖片.png

參考資料

圖片.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末代箭,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子涕刚,更是在濱河造成了極大的恐慌嗡综,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件杜漠,死亡現(xiàn)場離奇詭異极景,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)驾茴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進(jìn)店門盼樟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人锈至,你說我怎么就攤上這事晨缴。” “怎么了裹赴?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵喜庞,是天一觀的道長诀浪。 經(jīng)常有香客問我,道長延都,這世上最難降的妖魔是什么雷猪? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮晰房,結(jié)果婚禮上求摇,老公的妹妹穿的比我還像新娘。我一直安慰自己殊者,他們只是感情好与境,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著猖吴,像睡著了一般摔刁。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上海蔽,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天共屈,我揣著相機(jī)與錄音,去河邊找鬼党窜。 笑死拗引,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的幌衣。 我是一名探鬼主播矾削,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼豁护!你這毒婦竟也來了哼凯?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤择镇,失蹤者是張志新(化名)和其女友劉穎挡逼,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體腻豌,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年嘱能,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了吝梅。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,030評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡惹骂,死狀恐怖苏携,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情对粪,我是刑警寧澤右冻,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布装蓬,位于F島的核電站,受9級特大地震影響纱扭,放射性物質(zhì)發(fā)生泄漏牍帚。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一柬脸、第九天 我趴在偏房一處隱蔽的房頂上張望掠兄。 院中可真熱鬧扰魂,春花似錦、人聲如沸蹂随。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽岳锁。三九已至,卻和暖如春蹦魔,著一層夾襖步出監(jiān)牢的瞬間浸锨,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工版姑, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留柱搜,地道東北人。 一個月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓剥险,卻偏偏與公主長得像聪蘸,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子表制,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評論 2 355

推薦閱讀更多精彩內(nèi)容