Bokeh 简明教程
Bokeh - Pandas
在以上所有示例中,要绘制的数据以 Python 列表或 numpy 数组的形式提供。也可以以 pandas DataFrame 对象的形式提供数据源。
In all the examples above, the data to be plotted has been provided in the form of Python lists or numpy arrays. It is also possible to provide the data source in the form of pandas DataFrame object.
DataFrame 是一种二维数据结构。数据帧中的列可以是不同的数据类型。Pandas 库具有从各种来源(例如 CSV 文件、Excel 工作表、SQL 表等)创建数据帧的功能。
DataFrame is a two-dimensional data structure. Columns in the dataframe can be of different data types. The Pandas library has functions to create dataframe from various sources such as CSV file, Excel worksheet, SQL table, etc.
为了后面的示例,我们使用一个由表示数字 x 及 10x 的两列组成的 CSV 文件。test.csv 文件如下 -
For the purpose of following example, we are using a CSV file consisting of two columns representing a number x and 10x. The test.csv file is as below −
x,pow
0.0,1.0
0.5263157894736842,3.3598182862837818
1.0526315789473684,11.28837891684689
1.5789473684210527,37.926901907322495
2.1052631578947367,127.42749857031335
2.631578947368421,428.1332398719391
3.1578947368421053,1438.449888287663
3.6842105263157894,4832.930238571752
4.2105263157894735,16237.76739188721
4.7368421052631575,54555.947811685146
我们将使用 pandas 中的 read_csv() 函数将此文件读入一个数据帧对象中。
We shall read this file in a dataframe object using read_csv() function in pandas.
import pandas as pd
df = pd.read_csv('test.csv')
print (df)
数据帧如下所示 -
The dataframe appears as below −
x pow
0 0.000000 1.000000
1 0.526316 3.359818
2 1.052632 11.288379
3 1.578947 37.926902
4 2.105263 127.427499
5 2.631579 428.133240
6 3.157895 1438.449888
7 3.684211 4832.930239
8 4.210526 16237.767392
9 4.736842 54555.947812
“x”列和“pow”列用作 bokeh 绘图 figure 中折线字形的的数据序列。
The ‘x’ and ‘pow’ columns are used as data series for line glyph in bokeh plot figure.
from bokeh.plotting import figure, output_file, show
p = figure()
x = df['x']
y = df['pow']
p.line(x,y,line_width = 2)
p.circle(x, y,size = 20)
show(p)