Python数据科学速查表 - Bokeh
Python数据科学速查表 - Bokeh
Bokeh
呆鸟 译 图示符 栅格布局
散点标记 >>> from bokeh.layouts import gridplot
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