当前位置: 首页 > 工具软件 > Bokeh > 使用案例 >

Bokeh Graph

金霄
2023-12-01

利用 Bokeh+数组 建图

导入figurefrom bokeh.plotting import figure 用于绘图;
导入output_filefrom bokeh.io import output_file 用于输出html图片;
导入showfrom bokeh.io import show 用于输出图片;

output_file(“图片名称.html”);
f=figure() 创建figure;
f.line(x,y) 传入x数组,y数组,绘制线型图(rectangle 方块点,circle 圆圈点);
show(f) 输出figure;

#making a basic Boken line Graph

#importing Bokeh
from bokeh.plotting import figure
#容器 figure
from bokeh.io import output_file,show

#prepare some data
x=[1,2,3,4,5]
y=[6,7,8,9,10]  #同样长度

#prepare the output file
output_file("Line.html")       #被创建的名称

#create figure object
f=figure()  #figure

#create Line plot
f.line(x,y)      #传入x,y        #line
#f.circle(x,y)

#write the plot in the figure object
show(f)   #显示figure

Bokeh+numpyarray 建图

传入csv文件建立numpyarraydf=pandas.read_csv()
x为df[‘x’]的series,即array的一个column;
同样赋值y;
后续将x,y赋予figure即可;

#making a basic Boken line Graph

#importing Bokeh
from bokeh.plotting import figure
#容器 figure
from bokeh.io import output_file,show
import pandas

#prepare some data
#x=[1,2,3,4,5]
#y=[6,7,8,9,10]  #同样长度
df=pandas.read_csv("data.csv")
x=df['x']  #读入的array 有xcolumn和ycolumn  
y=df['y']

#prepare the output file
output_file("Line_from_csv.html")       #被创建的名称

#create figure object
f=figure()  #figure

#create Line plot
f.line(x,y)      #传入x,y        #line
#f.circle(x,y)

#write the plot in the figure object
show(f)   #显示figure

给Graph添加标题等

设置宽高,plot_width=, plot_height=, tools=’’

f=figure(plot_width=500, plot_height=400, tools='pan')  #figure
f.title.text="Cool Data"
f.title.text_color="Gray"
f.title.text_font="times"
f.title.text_font_style="bold"
f.xaxis.minor_tick_line_color=None
f.yaxis.minor_tick_line_color=None
f.xaxis.axis_label="Date"
f.yaxis.axis_label="Intensity"

计算series后赋值给xy

#making a basic Boken line Graph

#importing Bokeh
from bokeh.plotting import figure
#容器 figure
from bokeh.io import output_file,show
import pandas

#prepare some data
#x=[1,2,3,4,5]
#y=[6,7,8,9,10]  #同样长度
df=pandas.read_excel("http://pythonhow.com/data/verlegenhuken.xlsx")

#df['Temperature']=[x/10 for x in df['Temperature']]
#df['Pressure']=[y/10 for y in df['Pressure']]

x=df['Temperature']/10  #读入的array 有xcolumn和ycolumn  
y=df['Pressure']/10

#prepare the output file
#create figure object

#设置figure外形
f=figure(plot_width=500, plot_height=400, tools='pan')  #figure
f.title.text="Temperature and Air Pressure"
f.title.text_color="Gray"
f.title.text_font="arial"
f.title.text_font_style="bold"
f.xaxis.minor_tick_line_color=None
f.yaxis.minor_tick_line_color=None
f.xaxis.axis_label="Temperature(℃)"
f.yaxis.axis_label="Pressure(hPa)"

output_file("circle_tp.html")       #被创建的名称
#create Line plot
f.circle(x,y,size=0.5)      #传入x,y        #line
#f.circle(x,y)

#write the plot in the figure ob
show(f)   #显示figure

visual attributes

alpha为透明度;
tools为图片右边的可选工具;

from bokeh.plotting import figure, output_file, show
p = figure(plot_width=500, plot_height=400, tools = 'pan, reset')
p.title.text = "Earthquakes"
p.title.text_color = "Orange"
p.title.text_font = "times"
p.title.text_font_style = "italic"
p.yaxis.minor_tick_line_color = "Yellow"
p.xaxis.axis_label = "Times"
p.yaxis.axis_label = "Value"
p.circle([1,2,3,4,5], [5,6,5,5,3], size = [i*2 for i in [8,12,14,15,20]], color="red", alpha=0.5)
output_file("Scatter_plotting.html")
show(p)

https://docs.bokeh.org/en/latest/docs/user_guide/styling.html
可选attributes网站;

设置x坐标为datetime

parse_date解析日期;
sizing_mode放大图片:

from bokeh.plotting import figure
#容器 figure
from bokeh.io import output_file,show
import pandas

df=pandas.read_csv("abe.csv",parse_dates=["Date"])

p=figure(width=500,height=250,x_axis_type="datetime",sizing_mode="scale_both")

p.line(df["Date"],df["Close"],color="Blue",alpha=0.5)
output_file("timefile.html")
show(p)
 类似资料:

相关阅读

相关文章

相关问答