Skip to content

Instantly share code, notes, and snippets.

@bhishanpdl
Created October 5, 2018 03:42
Show Gist options
  • Save bhishanpdl/43f5ddad264c1a712979ddc63dd3d6ee to your computer and use it in GitHub Desktop.
Save bhishanpdl/43f5ddad264c1a712979ddc63dd3d6ee to your computer and use it in GitHub Desktop.
Bokeh interactive histogram #bokeh #histogram #interactive
def interactive_histogram(df,col,n_bins,bin_range,title,x_axis_label,x_tooltip):
"""Plot interactive histogram using bokeh.
df: pandas dataframe
col: column of panda dataframe to plot (eg. age of users)
n_bins: number of bins, e.g. 9
bin_range: list with min and max value. e.g. [10,100] age of users.
title: title of plot. e.g. 'Airnb Users Age Distribution'
x_axis_label: x axis label. e.g. 'Age (years)'.
x_tooltip: x axis tooltip string. e.g. 'Age'
"""
import pandas as pd
import numpy as np
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
from bokeh.models import ColumnDataSource, HoverTool, CategoricalColorMapper
from bokeh.palettes import Category10_5, Category20_16
arr_hist, edges = np.histogram(df[col],bins=n_bins,range=bin_range)
# Column data source
arr_df = pd.DataFrame({'count': arr_hist, 'left': edges[:-1], 'right': edges[1:]})
arr_df['f_count'] = ['%d' % count for count in arr_df['count']]
arr_df['f_interval'] = ['%d to %d ' % (left, right) for left, right in zip(arr_df['left'], arr_df['right'])]
# column data source
arr_src = ColumnDataSource(arr_df)
# Set up the figure same as before
p = figure(plot_width = 500,
plot_height = 500,
title = title,
x_axis_label = x_axis_label,
y_axis_label = 'Count')
# Add a quad glyph with source this time
p.quad(bottom=0,
top='count',
left='left',
right='right',
source=arr_src,
fill_color='red',
hover_fill_alpha=0.7,
hover_fill_color='blue',
line_color='black')
# Add style to the plot
p.title.align = 'center'
p.title.text_font_size = '18pt'
p.xaxis.axis_label_text_font_size = '12pt'
p.xaxis.major_label_text_font_size = '12pt'
p.yaxis.axis_label_text_font_size = '12pt'
p.yaxis.major_label_text_font_size = '12pt'
# Add a hover tool referring to the formatted columns
hover = HoverTool(tooltips = [(x_tooltip, '@f_interval'),
('Count', '@f_count')])
# Add the hover tool to the graph
p.add_tools(hover)
return p
# next cell
from bokeh.io import show, output_notebook
# To show in notebook
output_notebook()
# Show the plot
df = users
col = 'age'
n_bins = 9
bin_range = [10,100]
title = 'Airnb Users Age Distribution'
x_axis_label = 'Age (years)'
x_tooltip = 'Age'
bp = interactive_histogram(df,col,n_bins,bin_range,title,x_axis_label,x_tooltip)
show(bp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment