George C
George C

Reputation: 1223

Bokeh - matplotlib - histogram - blank plot


I have the following code that runs in a jupyter notebook with python 3:

from bokeh import mpl
from bokeh.plotting import figure, show, output_notebook
import matplotlib.pyplot as plt

output_notebook()

plt.hist([1,2,3,3,3,3,4,5,4])

show(mpl.to_bokeh())

This plots me a blank plot with no draws in it.

jupyter console Is a bug or I am doing something wrong?

Upvotes: 1

Views: 1295

Answers (1)

Ianhi
Ianhi

Reputation: 3152

It seems that plt.hist is behaving somewhat like plt.plot and plt.show. If you call plt.show() before show(mpl.to_bokeh()) you will get the same result for an example system as you can see here: enter image description here

I am not sure of the root cause of this behavior but a workaround is to simply create the histogram using bokeh. This is not hard if you follow the bokeh example:

http://docs.bokeh.org/en/latest/docs/gallery/histogram.html

For the example in your question you can do the following:

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

output_notebook()
hist, edges = np.histogram([1,2,3,3,3,3,4,5,4])

p1 = figure(title="Bokeh Hist",background_fill_color="#E8DDCB")

p1.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:],
        fill_color="#036564", line_color="#033649")
show(p1)

Giving:

enter image description here

Upvotes: 1

Related Questions