转载别人,bokeh

[python]bokeh学习总结——QuickStart

bokeh是python中一款基于网页的画图工具库,画出的图像以html格式保存。

一个简单的例子:


     
     
  1. from bokeh.plotting import figure, output_file, show
  2. output_file( "patch.html")
  3. p = figure(plot_width= 400, plot_height= 400)
  4. # add a patch renderer with an alpha an line width
  5. p.patch([ 1, 2, 3, 4, 5], [ 6, 7, 8, 7, 3], alpha= 0.5, line_width= 2)
  6. show(p)

画出图像后:


代码中有一行为

from bokeh.plotting import figure
     
     

figure是一个什么类型的数据?通过查看源代码,发现原来figure是一个函数,返回值为Figure类,Figure类以来自bokeh.models中的Plot类为父类,Figure类继承了Plot类中的各种属性。


     
     
  1. from ..models import ColumnDataSource, Plot, Title, Tool, GraphRenderer
  2. class Figure(Plot):
  3. 省略...
  4. def figure(**kwargs):
  5. ''' Create a new :class:`~bokeh.plotting.figure.Figure` for plotting.
  6. Figure objects have many glyph methods that can be used to draw
  7. vectorized graphical glyphs:
  8. .. hlist::
  9. :columns: 3
  10. {glyph_methods}
  11. There are also two specialized methods for stacking bars:
  12. * :func:`~bokeh.plotting.figure.Figure.hbar_stack`
  13. * :func:`~bokeh.plotting.figure.Figure.vbar_stack`
  14. And one specialized method for making simple hexbin plots:
  15. * :func:`~bokeh.plotting.figure.Figure.hexbin`
  16. In addition to the standard :class:`~bokeh.plotting.figure.Figure`
  17. property values (e.g. ``plot_width`` or ``sizing_mode``) the following
  18. additional options can be passed as well:
  19. .. bokeh-options:: FigureOptions
  20. :module: bokeh.plotting.figure
  21. Returns:
  22. Figure
  23. '''
  24. return Figure(**kwargs)

Plotting with Basic Glyphs

Creating Figures

Scatter Markers

画出圆形可以使用circle()方法:


     
     
  1. from bokeh.plotting import figure, output_file, show
  2. # output to static HTML file
  3. output_file( "line.html")
  4. p = figure(plot_width= 400, plot_height= 400)
  5. # add a circle renderer with a size, color, and alpha
  6. p.circle([ 1, 2, 3, 4, 5], [ 6, 7, 2, 4, 5], size= 20, color= "navy", alpha= 0.5)
  7. # show the results
  8. show(p)

得到的图形为:


同样的,如果要画出方形,可以使用square()方法,参数都是一样,将代码中的circle替换为square即可:


     
     
  1. from bokeh.plotting import figure, output_file, show
  2. # output to static HTML file
  3. output_file( "square.html")
  4. p = figure(plot_width= 400, plot_height= 400)
  5. # add a square renderer with a size, color, and alpha
  6. p.square([ 1, 2, 3, 4, 5], [ 6, 7, 2, 4, 5], size= 20, color= "olive", alpha= 0.5)
  7. # show the results
  8. show(p)

画出的图形为:


还有许多其它图形函数,其参数也都是一样,x表示x轴的数据,y表示y轴的数据,size表示图形的大小。还有一些参数包含angle——表示角度的大小,radius——表示图形的半径。

详细使用方法见:

https://bokeh.pydata.org/en/latest/docs/reference/plotting.html#bokeh.plotting.figure.Figure.x

以annular_wedge()函数为例:


     
     
  1. from bokeh.plotting import figure, output_file, show
  2. # output to static HTML file
  3. output_file( "square.html")
  4. p = figure()
  5. x = [ 61, 62, 63, 64, 65]
  6. y = [ 66, 67, 68, 69, 70]
  7. # add a square renderer with a size, color, and alpha
  8. p.annular_wedge(x=x, y=y, inner_radius= 0.1, outer_radius= 0.3, start_angle= 0, end_angle= 5, direction= 'anticlock')
  9. # show the results
  10. show(p)

Line Glyphs

Single Lines

     
     
  1. from bokeh.plotting import figure, output_file, show
  2. output_file( "line.html")
  3. p = figure(plot_width= 400, plot_height= 400)
  4. # add a line renderer
  5. p.line([ 1, 2, 3, 4, 5], [ 6, 7, 2, 4, 5], line_width= 2)
  6. show(p)

Step Lines

     
     
  1. from bokeh.plotting import figure, output_file, show
  2. output_file( "line.html")
  3. p = figure(plot_width= 400, plot_height= 400)
  4. # add a steps renderer
  5. p.step([ 1, 2, 3, 4, 5], [ 6, 7, 2, 4, 5], line_width= 2, mode= "center")
  6. show(p)

Multiple Lines

     
     
  1. from bokeh.plotting import figure, output_file, show
  2. output_file( "patch.html")
  3. p = figure(plot_width= 400, plot_height= 400)
  4. p.multi_line([[ 1, 3, 2], [ 3, 4, 6, 6]], [[ 2, 1, 4], [ 4, 7, 8, 5]],
  5. color=[ "firebrick", "navy"], alpha=[ 0.8, 0.3], line_width= 4)
  6. show(p)

需要注意的是,第一个list表示x轴的数据,[[1,3,2],[3,4,6,6]]中的两个list代表lines是分离的;第二个list表示y轴的数据。

Missing Points

NaN可以作为line()和multi_line()函数参数的一部分,用该值可以表示不连续点。若x=NaN,则对应的y值将被忽略。


     
     
  1. from bokeh.plotting import figure, output_file, show
  2. output_file( "line.html")
  3. p = figure(plot_width= 400, plot_height= 400)
  4. # add a line renderer with a NaN
  5. nan = float( 'nan')
  6. p.line([ 1, 2, 3, nan, 3, 5], [ 6, 7, 2, 4, 4, 5], line_width= 2)
  7. show(p)

Bars and Rectangles

Rectangles

     
     
  1. from bokeh.plotting import figure, show, output_file
  2. output_file( 'rectangles.html')
  3. p = figure(plot_width= 400, plot_height= 400)
  4. p.quad(top=[ 2, 3, 4], bottom=[ 1, 2, 3], left=[ 1, 2, 3],
  5. right=[ 1.2, 2.5, 3.7], color= "#B3DE69")
  6. show(p)

还有一个例子:


     
     
  1. from math import pi
  2. from bokeh.plotting import figure, show, output_file
  3. output_file( 'rectangles_rotated.html')
  4. p = figure(plot_width= 400, plot_height= 400)
  5. p.rect(x=[ 1, 2, 3], y=[ 1, 2, 3], width= 0.2, height= 40, color= "#CAB2D6",
  6. angle=pi/ 3, height_units= "screen")
  7. show(p)

Bars

vertical bars:


     
     
  1. from bokeh.plotting import figure, show, output_file
  2. output_file( 'vbar.html')
  3. p = figure(plot_width= 400, plot_height= 400)
  4. p.vbar(x=[ 1, 2, 3], width= 0.5, bottom= 0,
  5. top=[ 1.2, 2.5, 3.7], color= "firebrick")
  6. show(p)

horizon bars:


     
     
  1. from bokeh.plotting import figure, show, output_file
  2. output_file( 'hbar.html')
  3. p = figure(plot_width= 400, plot_height= 400)
  4. p.hbar(y=[ 1, 2, 3], height= 0.5, left= 0,
  5. right=[ 1.2, 2.5, 3.7], color= "navy")
  6. show(p)

Hex Tiles


     
     
  1. import numpy as np
  2. from bokeh.io import output_file, show
  3. from bokeh.plotting import figure
  4. from bokeh.util.hex import axial_to_cartesian
  5. output_file( "hex_coords.py")
  6. q = np.array([ 0, 0, 0, -1, -1, 1, 1])
  7. r = np.array([ 0, -1, 1, 0, 1, -1, 0])
  8. p = figure(plot_width= 400, plot_height= 400, toolbar_location= None)
  9. p.grid.visible = False
  10. p.hex_tile(q, r, size= 1, fill_color=[ "firebrick"]* 3 + [ "navy"]* 4,
  11. line_color= "white", alpha= 0.5)
  12. x, y = axial_to_cartesian(q, r, 1, "pointytop")
  13. p.text(x, y, text=[ "(%d, %d)" % (q,r) for (q, r) in zip(q, r)],
  14. text_baseline= "middle", text_align= "center")
  15. show(p)


     
     
  1. import numpy as np
  2. from bokeh.io import output_file, show
  3. from bokeh.plotting import figure
  4. from bokeh.transform import linear_cmap
  5. from bokeh.util.hex import hexbin
  6. n = 50000
  7. x = np.random.standard_normal(n)
  8. y = np.random.standard_normal(n)
  9. bins = hexbin(x, y, 0.1)
  10. p = figure(tools= "wheel_zoom,reset", match_aspect= True, background_fill_color= '#440154')
  11. p.grid.visible = False
  12. p.hex_tile(q= "q", r= "r", size= 0.1, line_color= None, source=bins,
  13. fill_color=linear_cmap( 'counts', 'Viridis256', 0, max(bins.counts)))
  14. output_file( "hex_tile.html")
  15. show(p)

Patch Glyphs

Single Patches

     
     
  1. from bokeh.plotting import figure, output_file, show
  2. output_file( "patch.html")
  3. p = figure(plot_width= 400, plot_height= 400)
  4. # add a patch renderer with an alpha an line width
  5. p.patch([ 1, 2, 3, 4, 5], [ 6, 7, 8, 7, 3], alpha= 0.5, line_width= 2)
  6. show(p)

Multiple Patches

     
     
  1. from bokeh.plotting import figure, output_file, show
  2. output_file( "patch.html")
  3. p = figure(plot_width= 400, plot_height= 400)
  4. p.patches([[ 1, 3, 2], [ 3, 4, 6, 6]], [[ 2, 1, 4], [ 4, 7, 8, 5]],
  5. color=[ "firebrick", "navy"], alpha=[ 0.8, 0.3], line_width= 2)
  6. show(p)

Missing Points

     
     
  1. from bokeh.plotting import figure, output_file, show
  2. output_file( "patch.html")
  3. p = figure(plot_width= 400, plot_height= 400)
  4. # add a patch renderer with a NaN value
  5. nan = float( 'nan')
  6. p.patch([ 1, 2, 3, nan, 4, 5, 6], [ 6, 7, 5, nan, 7, 3, 6], alpha= 0.5, line_width= 2)
  7. show(p)

Ovals and Ellipses


     
     
  1. from math import pi
  2. from bokeh.plotting import figure, show, output_file
  3. output_file( 'ovals.html')
  4. p = figure(plot_width= 400, plot_height= 400)
  5. p.oval(x=[ 1, 2, 3], y=[ 1, 2, 3], width= 0.2, height= 40, color= "#CAB2D6",
  6. angle=pi/ 3, height_units= "screen")
  7. show(p)


     
     
  1. from math import pi
  2. from bokeh.plotting import figure, show, output_file
  3. output_file( 'ellipses.html')
  4. p = figure(plot_width= 400, plot_height= 400)
  5. p.ellipse(x=[ 1, 2, 3], y=[ 1, 2, 3], width=[ 0.2, 0.3, 0.1], height= 0.3,
  6. angle=pi/ 3, color= "#CAB2D6")
  7. show(p)

Segments and Rays

Sometimes it is useful to be able to draw many individual line segments at once. Bokeh provides the segment() and ray() glyph methods to render these.


     
     
  1. from bokeh.plotting import figure, show
  2. p = figure(plot_width= 400, plot_height= 400)
  3. p.segment(x0=[ 1, 2, 3], y0=[ 1, 2, 3], x1=[ 1.2, 2.4, 3.1],
  4. y1=[ 1.2, 2.5, 3.7], color= "#F4A582", line_width= 3)
  5. show(p)

The ray() function accepts start points xy with a length (in screen units) and an angle. The default angle_units are "rad" but can also be changed to "deg". To have an “infinite” ray, that always extends to the edge of the plot, specify 0 for the length:


     
     
  1. from bokeh.plotting import figure, show
  2. p = figure(plot_width= 400, plot_height= 400)
  3. p.ray(x=[ 1, 2, 3], y=[ 1, 2, 3], length= 45, angle=[ 30, 45, 60],
  4. angle_units= "deg", color= "#FB8072", line_width= 2)
  5. show(p)

Wedges and Arcs


     
     
  1. from bokeh.plotting import figure, show
  2. p = figure(plot_width= 400, plot_height= 400)
  3. p.arc(x=[ 1, 2, 3], y=[ 1, 2, 3], radius= 0.1, start_angle= 0.4, end_angle= 4.8, color= "navy")
  4. show(p)


     
     
  1. from bokeh.plotting import figure, show
  2. p = figure(plot_width= 400, plot_height= 400)
  3. p.wedge(x=[ 1, 2, 3], y=[ 1, 2, 3], radius= 0.2, start_angle= 0.4, end_angle= 4.8,
  4. color= "firebrick", alpha= 0.6, direction= "clock")
  5. show(p)


     
     
  1. from bokeh.plotting import figure, show
  2. p = figure(plot_width= 400, plot_height= 400)
  3. p.annular_wedge(x=[ 1, 2, 3], y=[ 1, 2, 3], inner_radius= 0.1, outer_radius= 0.25,
  4. start_angle= 0.4, end_angle= 4.8, color= "green", alpha= 0.6)
  5. show(p)


     
     
  1. from bokeh.plotting import figure, show
  2. p = figure(plot_width= 400, plot_height= 400)
  3. p.annulus(x=[ 1, 2, 3], y=[ 1, 2, 3], inner_radius= 0.1, outer_radius= 0.25,
  4. color= "orange", alpha= 0.6)
  5. show(p)

Combining Multiple Glyphs


     
     
  1. from bokeh.plotting import figure, output_file, show
  2. x = [ 1, 2, 3, 4, 5]
  3. y = [ 6, 7, 8, 7, 3]
  4. output_file( "multiple.html")
  5. p = figure(plot_width= 400, plot_height= 400)
  6. # add both a line and circles on the same plot
  7. p.line(x, y, line_width= 2)
  8. p.circle(x, y, fill_color= "white", size= 8)
  9. show(p)

Setting Ranges

两种方法设置range:

1.可以通过从bokeh.models中导入Range1d(x,y)对象来实现:

By default, Bokeh will attempt to automatically set the data bounds of plots to fit snugly around the data. Sometimes you may need to set a plot’s range explicitly. This can be accomplished by setting the x_range or y_range properties using a Range1dobject that gives the start and end points of the range you want:

p.x_range = Range1d(0, 100)
     
     

2.在figure()里直接调用x_range()和y_range():

As a convenience, the figure() function can also accept tuples of (start, end) as values for the x_range or y_range parameters.

看一个例子:


     
     
  1. from bokeh.plotting import figure, output_file, show
  2. from bokeh.models import Range1d
  3. output_file( "title.html")
  4. # create a new plot with a range set with a tuple
  5. p = figure(plot_width= 400, plot_height= 400, x_range=( 0, 20))
  6. # set a range using a Range1d
  7. p.y_range = Range1d( 0, 15)
  8. p.circle([ 1, 2, 3, 4, 5], [ 2, 5, 8, 2, 7], size= 10)
  9. show(p)

Specifying Axis Types

Categorical Axes

上面的所有例子中,x和y轴都是数字,有些时候希望坐标轴显示的是字符,可以使用如下方法:


     
     
  1. from bokeh.plotting import figure, output_file, show
  2. factors = [ "a", "b", "c", "d", "e", "f", "g", "h"]
  3. x = [ 50, 40, 65, 10, 25, 37, 80, 60]
  4. output_file( "categorical.html")
  5. p = figure(y_range=factors)
  6. p.circle(x, factors, size= 15, fill_color= "orange", line_color= "green", line_width= 3)
  7. show(p)

此时,y轴是factors:


Log Scale Axes

     
     
  1. from bokeh.plotting import figure, output_file, show
  2. x = [ 0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
  3. y = [ 10**xx for xx in x]
  4. output_file( "log.html")
  5. # create a new plot with a log axis type
  6. p = figure(plot_width= 400, plot_height= 400, y_axis_type= "log")
  7. p.line(x, y, line_width= 2)
  8. p.circle(x, y, fill_color= "white", size= 8)
  9. show(p)

Twin Axes


     
     
  1. from numpy import pi, arange, sin, linspace
  2. from bokeh.plotting import output_file, figure, show
  3. from bokeh.models import LinearAxis, Range1d
  4. x = arange( -2*pi, 2*pi, 0.1)
  5. y = sin(x)
  6. y2 = linspace( 0, 100, len(y))
  7. output_file( "twin_axis.html")
  8. p = figure(x_range=( -6.5, 6.5), y_range=( -1.1, 1.1))
  9. p.circle(x, y, color= "red")
  10. p.extra_y_ranges = { "foo": Range1d(start= 0, end= 100)}
  11. p.circle(x, y2, color= "blue", y_range_name= "foo")
  12. p.add_layout(LinearAxis(y_range_name= "foo"), 'left')
  13. show(p)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值