您当前的位置:首页 > 计算机 > 编程开发 > Python

使用Jupyter Notebook+bokeh绘图入门完整步骤

时间:01-08来源:作者:点击数:
城东书院 www.cdsy.xyz

Bokeh是一款基于浏览器的交互式绘图工具,在IPython Notebook中具有非常好的表现。

安装anaconda3,单击开始菜单,单击下图红色箭头所指菜单启动Jupyter Notebook:

图片

然后在浏览器中单击下图中红色箭头指向的菜单:

图片

然后在单元格内编写下面的代码:

frombokeh.plottingimportfigure, output_file, show

fromrandomimportrandrange

# 生成测试数据

x =list(range(1,21))

y = [randrange(1,10)for_inrange(20)]

output_file('lines.html', title='lines')

p = figure(title='lines', x_axis_label='x', y_axis_label='y')

# 设置图例

p.line(x, y, legend='lines', line_width=2)

show(p)

代码截图为:

图片

单击上图中红色箭头所指,运行代码,生成的图片如下图所示,注意红色方框内有一些工具可以对图片进行缩放、平移或保存。

图片

把代码改为:

frombokeh.plottingimportfigure, output_file, show

fromrandomimportrandrange

x =list(range(1,21))

y = [randrange(1,10)for_inrange(20)]

output_file('triangle.html', title='triangle')

p = figure(title='triangle', x_axis_label='x', y_axis_label='y')

p.triangle(x, y, legend='triangle', line_width=2)

show(p)

重新运行代码,得到的图像为:

图片

把代码改为:

frombokeh.plottingimportfigure, output_file, show

fromrandomimportrandrange

x =list(range(1,21))

y = [randrange(1,10)for_inrange(20)]

output_file('circle.html', title='circle')

p = figure(title='circle', x_axis_label='x', y_axis_label='y')

p.circle(x, y, legend='circle', line_width=2)

show(p)

重新运行代码,得到的图形为:

图片
城东书院 www.cdsy.xyz
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门