python Bokeh直方图:调整x比例和图表样式

时间:2017-09-05 18:03:25

标签: python plot bokeh

我使用Bokeh在python中有一个直方图:

from bokeh.charts import Histogram
from bokeh.sampledata.autompg import autompg as df
#from bokeh.charts import defaults, vplot, hplot, show, output_file

p = Histogram(df, values='hp', color='cyl',
              title="HP Distribution (color grouped by CYL)",
              legend='top_right')
output_notebook()  ## output inline

show(p)

我想调整以下内容: - X比例更改为log10 - 而不是条形,我想要一条平滑的线条(如分布图)

有谁知道如何进行这些调整?

1 个答案:

答案 0 :(得分:5)

这可以使用bokeh.plotting API完成,让您可以更好地控制分箱和平滑。这是一个完整的例子(也绘制直方图以获得良好的衡量标准):

from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.sampledata.autompg import autompg as df

from numpy import histogram, linspace
from scipy.stats.kde import gaussian_kde

pdf = gaussian_kde(df.hp)

x = linspace(0,250,200)

p = figure(x_axis_type="log", plot_height=300)
p.line(x, pdf(x))

# plot actual hist for comparison
hist, edges = histogram(df.hp, density=True, bins=20)
p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], alpha=0.4)

output_file("hist.html")

show(p)

输出:

enter image description here