0% found this document useful (0 votes)
51 views

Python数据科学速查表 - Bokeh

Bokeh is an interactive visualization library for Python that can generate large, complex datasets as interactive visualizations in a web browser. It uses a common plotting interface with figures and glyphs to generate visualizations. Key steps include preparing data, creating a figure, adding glyphs to visualize the data, specifying output, and displaying. Visualizations can be output as HTML, exported as images, or used as components. The library provides tools for customizing visual aspects like legends, selections, linked brushing, and more.

Uploaded by

Keith Ng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Python数据科学速查表 - Bokeh

Bokeh is an interactive visualization library for Python that can generate large, complex datasets as interactive visualizations in a web browser. It uses a common plotting interface with figures and glyphs to generate visualizations. Key steps include preparing data, creating a figure, adding glyphs to visualize the data, specifying output, and displaying. Visualizations can be output as HTML, exported as images, or used as components. The library provides tools for customizing visual aspects like legends, selections, linked brushing, and more.

Uploaded by

Keith Ng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Python 数据科学 速查表 3 渲染器与自定义可视化

Bokeh
呆鸟 译 图示符 栅格布局
散点标记 >>> from bokeh.layouts import gridplot

天善智能 商业智能与大数据社区 www.hellobi.com


>>> p1.circle(np.array([1,2,3]), np.array([3,2,1]), >>> row1 = [p1,p2]
fill_color='white') >>> row2 = [p3]
>>> p2.square(np.array([1.5,3.5,5.5]), [1,4,3], >>> layout = gridplot([[p1,p2],[p3]])
线型图示符
使用 Bokeh 绘图 标签布局
>>> p1.line([1,2,3,4], [3,4,5,6], line_width=2)
Bokeh 是 Python 的交互式可视图库,用于生成在浏览器里显
>>> p2.multi_line(pd.DataFrame([[1,2,3],[5,6,7]]), >>> from bokeh.models.widgets import Panel, Tabs
>>> tab1 = Panel(child=p1, title="tab1")
示的大规模数据集高性能可视图。
pd.DataFrame([[3,4,5],[3,2,1]]),
color="blue") >>> tab2 = Panel(child=p2, title="tab2")
>>> layout = Tabs(tabs=[tab1, tab2])
自定义图示符 参阅 数据
链接图
Bokeh 的中间层通用 bokeh.plotting 界面主要为两个组件: 图示符选择与反选
数据与图示符。 >>> p = figure(tools='box_select') 链接坐标轴
>>> p.circle('mpg', 'cyl', source=cds_df, >>> p2.x_range = p1.x_range
selection_color='red', >>> p2.y_range = p1.y_range
nonselection_alpha=0.1) 链接刷
绘图区内部
>>> p4 = figure(plot_width = 100,
+ = tools='box_select,lasso_select')
>>> from bokeh.models import HoverTool >>> p4.circle('mpg', 'cyl', source=cds_df)
数据 图示符 图形 >>> hover = HoverTool(tooltips=None, >>> p5 = figure(plot_width = 200,
mode='vline') >>> p3.add_tools(hover) tools='box_select,lasso_select')
使用 bokeh.plotting 界面绘图的基本步骤为: >>> p5.circle('mpg', 'hp', source=cds_df)
色彩表 >>> layout = row(p4,p5)
1. 准备数据
US

>>> from bokeh.models import CategoricalColorMapper


Asia
Europe

Python列表、Numpy数组、Pandas数据框或其它序列值
2. 创建图形
>>> color_mapper = CategoricalColorMapper(
factors=['US', 'Asia', 'Europe'], 4 输出与导出
3. 为数据添加渲染器,自定义可视化图
palette=['blue', 'red', 'green'])
>>> p3.circle('mpg', 'cyl', source=cds_df, Notebook
4. 指定生成的输出类型 color=dict(field='origin',
5. 显示视图或保存结果 transform=color_mapper), >>> from bokeh.io import output_notebook, show
legend='Origin') >>> output_notebook()

HTML
>>> from bokeh.plotting import figure
>>> from bokeh.io import output_file, show 图例位置
脱机HTML
>>> x = [1, 2, 3, 4, 5]
绘图区内部
Step 1
>>> y = [6, 7, 2, 4, 5]
>>> p = figure(title="simple line example", Step 2 >>> from bokeh.embed import file_html
>>> p.legend.location = 'bottom_left'
>>> from bokeh.resources import CDN
绘图区外部
x_axis_label='x',
>>> html = file_html(p, CDN, "my_plot")
y_axis_label='y')
>>> p.line(x, y, legend="Temp.", line_width=2) Step 3 >>> from bokeh.models import Legend
>>> r1 = p2.asterisk(np.array([1,2,3]), np.array([3,2,1]) >>> from bokeh.io import output_file, show
>>> output_file("lines.html") Step 4 >>> r2 = p2.line([1,2,3,4], [3,4,5,6]) >>> output_file('my_bar_chart.html', mode='cdn')
>>> show(p) Step 5 >>> legend = Legend(items=[("One" ,[p1, r1]),("Two",[r2])],
组件
location=(0, -30))
>>> p.add_layout(legend, 'right')
1 数据 参阅列表、Numpy 及 Pandas
图例方向
>>> from bokeh.embed import components
>>> script, div = components(p)

通常,Bokeh在后台把数据转换为列数据源,不过也可手动转换:
>>> p.legend.orientation = "horizontal" PNG
>>> import numpy as np >>> p.legend.orientation = "vertical"
>>> from bokeh.io import export_png
图例背景与边框
>>> import pandas as pd >>> export_png(p, filename="plot.png")
>>> df = pd.DataFrame(np.array([[33.9,4,65, 'US'],

SVG
[32.4,4,66, 'Asia'],
[21.4,4,109, 'Europe']]), >>> p.legend.border_line_color = "navy"
columns=['mpg','cyl', 'hp', 'origin'], >>> p.legend.background_fill_color = "white"
index=['Toyota', 'Fiat', 'Volvo']) >>> from bokeh.io import export_svgs
>>> from bokeh.models import ColumnDataSource 行列布局 >>> p.output_backend = "svg"
>>> export_svgs(p, filename="plot.svg")
>>> cds_df = ColumnDataSource(df) 行

2 绘图
>>> from bokeh.layouts import row
>>> layout = row(p1,p2,p3) 5 显示或保存图形
>>> from bokeh.plotting import figure 列 >>> show(p1) >>> show(layout)
>>> p1 = figure(plot_width=300, tools='pan,box_zoom') >>> from bokeh.layouts import columns >>> save(p1) >>> save(layout)

行列嵌套
>>> p2 = figure(plot_width=300, plot_height=300, >>> layout = column(p1,p2,p3)

原文作者 DataCamp
x_range=(0, 8), y_range=(0, 8))
>>>layout = row(column(p1,p2), p3)
>>> p3 = figure() Learn Python for Data Science Interactively

You might also like