實驗環境:win 7 python3.5.2 bokeh 0.13.0
時間:2018-08-06
一、bokeh 畫線
1)正弦曲線
from bokeh.plotting import figure,show
import numpy as np
x=np.linspace(-np.pi,np.pi,100)
y=np.sin(x)
p=figure(plot_width=400,plot_height=400)
p.line(x,y)
show(p)
會自動在當前文件加下產生一個同名html,並在瀏覽器里顯示
2) 支持padans 數據類型
import pandas as pd
from bokeh.plotting import figure, show
import tushare as ts
df=ts.get_hist_data('600848',start='2017-01-01',end='2018-05-01')
p = figure(plot_width=800, plot_height=250, x_axis_type="datetime")
p.line(pd.to_datetime(df.index), df['close'], color='navy', alpha=0.5)
show(p)
二、figure line 和circle 多種元素同圖
1) 多個元素可以同一個plot
from bokeh.plotting import figure, show
import numpy as np
x=[0.5,0.8,1.5,2.5,2.4]
y=[2,1.5,2.7,4,6]
p=figure(plot_width=800,plot_height=400)
p.circle(x,y,fill_color='white')
x1=np.linspace(-np.pi,np.pi,50)
y1=np.tan(x1)
p.line(x1,y1)
show(p)
2) 其它元素
bar
from bokeh.plotting import figure, show
import numpy as np
p=figure(plot_width=800,plot_height=400)
p.vbar(x=[1,2,3],top=[2.4,3.5,4.2],width=0.2)
show(p)
from bokeh.plotting import figure, show
p = figure(plot_width=400, plot_height=400)
p.hbar(y=[1, 2, 3], height=0.5, left=0,
right=[1.2, 2.5, 3.7], color="navy")
show(p)
3) image
from bokeh.plotting import figure, show
#url="""C:\\Users\\Administrator\\Pictures\\cat1.jpg"""
url1="""https://b-ssl.duitang.com/uploads/item/201604/15/20160415073709_d2XYs.jpeg"""
url2="""https://b-ssl.duitang.com/uploads/item/201601/19/20160119143747_i84Nx.png"""
url3="""https://b-ssl.duitang.com/uploads/item/201408/12/20140812131500_GTvHZ.jpeg"""
p=figure(x_range=(0,100),y_range=(0,100),plot_width=800,plot_height=800)
p.image_url(url=[url1,url3],x=(0,50),y=(100,50),w=50,h=50)
p.xaxis.visible=None
p.yaxis.visible=None
p.toolbar.logo=None
p.toolbar_location=None
show(p)
p.image_url(x,y的每一項對應相應圖片的左上角坐標
如果圖片為本地路徑,也是支持的。