[雪峰磁針石部落格]Bokeh資料視覺化工具1快速入門

書籍尋找發表於2018-08-18

簡介

資料視覺化python庫參考

python資料視覺化庫最突出的為Matplotlib、Seaborn和Bokeh。前兩個,Matplotlib和Seaborn,繪製靜態圖。Bokeh可以繪製互動式圖。

安裝


conda install bokeh

pip2 install bokeh

pip3 install bokeh

檢驗安裝


from bokeh.plotting import figure, output_file, show

#HTML file to output your plot into

output_file("bokeh.html")

#Constructing a basic line plot

x = [1,2,3]

y = [4,5,6]

p = figure()

p.line(x,y)

show(p)

635
image.png

問題討論:

https://groups.google.com/a/anaconda.com/forum/#!forum/bokeh

bug跟蹤:https://github.com/bokeh/bokeh/issues

應用程式:Bokeh應用程式是在瀏覽器中執行的Bokeh渲染文件

Glyph:Glyph是Bokeh的基石,它們是線條,圓形,矩形等。

伺服器:Bokeh伺服器用於共享和釋出互動式圖表

小部件Widgets::Bokeh中的小部件是滑塊,下拉選單等

輸出方法有:output_file(`plot.html`)和output_notebook()

構建圖片的方式:


#Code to construct a figure

from bokeh.plotting import figure

# create a Figure object

p = figure(plot_width=500, plot_height=400, tools="pan,hover")

繪圖基礎

線狀圖


#Creating a line plot

#Importing the required packages

from bokeh.io import output_file, show

from bokeh.plotting import figure

#Creating our data arrays used for plotting the line plot

x = [5,6,7,8,9,10]

y = [1,2,3,4,5,6]

#Calling the figure() function to create the figure of the plot

plot = figure()

#Creating a line plot using the line() function

plot.line(x,y)

#Creating markers on our line plot at the location of the intersection between x and y

plot.cross(x,y, size = 15)

#Output the plot

output_file(`line_plot.html`)

show(plot)

631
image.png

柱形圖


#Creating bar plots

#Importing the required packages

from bokeh.plotting import figure, show, output_file

#Points on the x axis

x = [8,9,10]

#Points on the y axis

y = [1,2,3]

#Creating the figure of the plot

plot = figure()

#Code to create the barplot

plot.vbar(x,top = y, color = "blue", width= 0.5)

#Output the plot

output_file(`barplot.html`)

show(plot)

615
image.png

補丁圖


#Creating patch plots

#Importing the required packages

from bokeh.io import output_file, show

from bokeh.plotting import figure

#Creating the regions to map

x_region = [[1,1,2,], [2,3,4], [2,3,5,4]]

y_region = [[2,5,6], [3,6,7], [2,4,7,8]]

#Creating the figure

plot = figure()

#Building the patch plot

plot.patches(x_region, y_region, fill_color = [`yellow`, `black`, `green`], line_color = `white`)

#Output the plot

output_file(`patch_plot.html`)

show(plot)

700
image.png

雜湊圖


#Creating scatter plots

#Importing the required packages

from bokeh.io import output_file, show

from bokeh.plotting import figure

#Creating the figure

plot = figure()

#Creating the x and y points

x = [1,2,3,4,5]

y = [5,7,2,2,4]

#Plotting the points with a cirle marker

plot.circle(x,y, size = 30)

#Output the plot

output_file(`scatter.html`)

show(plot)

625
image.png

更多資源


#- cross()

#- x()

#- diamond()

#- diamond_cross()

#- circle_x()

#- circle_cross()

#- triangle()

#- inverted_triangle()

#- square()

#- square_x()

#- square_cross()

#- asterisk()

#Adding labels to the plot

plot.figure(x_axis_label = "Label name of x axis", y_axis_label = "Label name of y axis")

#Customizing transperancy of the plot

plot.circle(x, y, alpha = 0.5)

plot.circle(x, y, alpha = 0.5)

參考資料


相關文章