creating html output file with plot bokeh

1.1k views Asked by At

as a very simple example when I want to export a visualisation to a html in bokeh i'd do something like this:

#importing bokeh
from bokeh.plotting import figure
from bokeh.io import output_file, show

#data prep
x=[1,2,3,4,5]
y=[6,7,8,9,10]

#prep output file
output_file('line.html')

#create a figure object
f=figure()

#create a line plot
f.square(x,y)

show(f)

which seems to work fine.

however when working with a visualisation like a histogram and when working with pandas_bokeh i cant get it to export as a html. for example:

import pandas as pd
import pandas_bokeh
pandas_bokeh.output_notebook()

df['any random numerical column'].plot_bokeh(kind='hist',)
output_file('hist.html')

anyone know how i can get this to work?

my second question is, is how would I export a viz if I was using holoviews? as in how can the code be altered here: http://holoviews.org/gallery/demos/bokeh/boxplot_chart.html for it to be done?

any light shed on these 2 questions would be greatly appreciated and please explain as simply as possible as this is all fairly new to me. thank you

1

There are 1 answers

0
mosc9575 On

To quote form the documentation for output_file():

output_file() creates an output to a file when show() is called.

Another option is to call save() after output_file() to create the HTML-File.

Example for pandas-bokeh

import pandas as pd
import pandas_bokeh
from bokeh.io import output_file, save
pandas_bokeh.output_notebook()

df = pd.DataFrame({'x':[1,2,3,4,5], 'y':[3,4,2,1,3]})
p = df.plot_bokeh()

output_file('lines.html')
save(p)

Since show() is called inside pandas-bokeh anyway an even shorter example to generate the same figure is:

import pandas as pd
import pandas_bokeh
from bokeh.io import output_file
pandas_bokeh.output_notebook()

output_file('lines.html')
df = pd.DataFrame({'x':[1,2,3,4,5], 'y':[3,4,2,1,3]})
df.plot_bokeh()

Holoviews

For holoviews there exists the method `hv.save() to genreate a html, too.

But you can also get the bokeh object by calling p = hv.render(fig) and do the same with the save from the pandas-bokeh example.

I didn't try it, but it is also possible that holoviews internally calls show() so the second example cloud also work.