Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 735
famargar Avatar asked Dec 14 '25 21:12

famargar


2 Answers

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!

like image 200
famargar Avatar answered Dec 16 '25 22:12

famargar


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)

like image 33
claudiodsf Avatar answered Dec 16 '25 21:12

claudiodsf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!