Trexion Kameha
Trexion Kameha

Reputation: 3580

python Bokeh histogram: adjusting x scale and chart style

I have a Histogram in python using Bokeh:

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)

I would like to adjust the following: - X scale change to log10 - Instead of bars, I would like a smoothed line (like a distribution plot)

Does anyone know how to make these adjustments?

Upvotes: 3

Views: 5880

Answers (1)

bigreddot
bigreddot

Reputation: 34628

This can be accomplished with the bokeh.plotting API, giving you more control over the binning and smoothing. Here is a complete example (also plots the histogram for good measure):

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)

With the output:

enter image description here

Upvotes: 7

Related Questions