Python Bokeh 直方图:调整 x 轴刻度和图表样式

3
我使用 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 - 不再使用条形图,而是使用平滑线(例如分布图)
请问有谁知道如何进行这些调整?

请看这个链接是否有帮助:https://dev59.com/ZGEh5IYBdhLWcg3wKgug - Dalton Cézane
1个回答

7

使用 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)

输出结果为:

图片描述在此


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接