famargar
famargar

Reputation: 3448

How to show a histogram in log scale with bokeh?

The question is all in the title. I am trying to use the Histogram object imported from bokeh.charts, but can't figure out how to show it in log scale. In my particular case, I would need both the x and the y axis to be displayed in log scale.

Upvotes: 3

Views: 1844

Answers (2)

claudiodsf
claudiodsf

Reputation: 131

Note that the above solution by @famagar will not work with recent version of bokeh (just tried it with 0.12.14 -- see this issue).

The problem is that log scale cannot properly handle zeros.

To fix that, one has to put the bottom argument to some non-zero value. For instance, to get the same result as in the figure above bottom=1:

p.quad(top=hist, bottom=1, left=edges[:-1], right=edges[1:], line_color=None)

Upvotes: 5

famargar
famargar

Reputation: 3448

Ok it seems that it is possible to have logarithmic scales. However, rather than using the charts API, I should be using the plotting API:

import numpy as np
from bokeh.plotting import figure, show, output_notebook

output_notebook()

# generate random data from a powerlaw
measured = 1/np.random.power(2.5, 100000)

hist, edges = np.histogram(measured, bins=50)

p = figure(title="Power law (a=2.5)", x_axis_type="log", y_axis_type='log')
p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], line_color=None)

show(p)

enter image description here

Thanks bigreddot for the help from another question!

Upvotes: 6

Related Questions