我在Bokeh和matplotlib中绘制了一个箱线图。对于相同的数据,Bokeh的绘图速度大约慢了100倍。为什么Bokeh需要这么长时间呢?以下是我在Jupyter notebook中运行的代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from bokeh.charts import BoxPlot, output_notebook, show
from time import time
%matplotlib inline
# Generate data
N = 100000
x1 = 2 + np.random.randn(N)
y1 = ['a'] * N
x2 = -2 + np.random.randn(N)
y2 = ['b'] * N
X = list(x1) + list(x2)
Y = y1 + y2
data = pd.DataFrame()
data['Vals'] = X
data['Class'] = Y
df = data.apply(np.random.permutation)
# Time the bokeh plot
start_time = time()
p = BoxPlot(data, values='Vals', label='Class',\
title="MPG Summary (grouped by CYL, ORIGIN)")
output_notebook()
show(p)
end_time = time()
print("Total time taken for Bokeh is {0}".format(end_time - start_time))
# time the matplotlib plot
start_time = time()
data.boxplot(column='Vals', by='Class', sym = 'o')
end_time = time()
print("Total time taken for matplotlib is {0}".format(end_time - start_time))
打印语句会产生以下输出:
Bokeh所需的总时间为11.8056321144104
Matplotlib所需的总时间为0.1586170196533203
bokeh
纯粹使用Python编写?matplotlib
是建立在明显更快的numpy
之上的。 - roganjosh