How to display statsmodels Mosaics directly with Bokeh

726 Views Asked by At

I'm looking to create some mosaic plots to visualize contingency tables. Mosaic plots are not (for all I know) natively supported by Bokeh so I'm using the statsmodels library (link).

mosaicplot

Problem is plots from that library don't extend Bokeh's Figure interface, so I can't get them to show in a webpage. Ideally I want the user to be able to select their variables of interest using drop-down boxes on a webpage:

webui

How can the results of statsmodels mosaic be displayed directly by Bokeh?

1

There are 1 best solutions below

0
On

Bokeh does not support mosaic charts directly, however the mosaic function can returns all the geometric data necessary to have Bokeh render a plot itself. Additionally, if you pass ax=None:

_, rects_dict = mosaic(df, ..., ax=None)

then the generation of the Matplotlib plot will be suppressed. Below is a complete example that demonstrates how to use the return rects_dict value from mosaic:

import pandas as pd
from statsmodels.graphics.mosaicplot import mosaic
from bokeh.plotting import figure, ColumnDataSource, show
from bokeh.transform import factor_cmap

df = pd.DataFrame({
    'size' : ['small', 'medium', 'medium', 'large', 'small', 'large', 'small', 'medium'],
    'length' : ['long', 'short', 'long', 'short', 'long', 'long', 'short', 'short']
})

_, rects_dict = mosaic(df, ['size', 'length'], gap=0, ax=None)

rects = rects_dict.values()
cats = rects_dict.keys()

source = ColumnDataSource(data=dict(
    x    = [r[0]+r[2]/2 for r in rects], # bokeh wants x center, not corner
    y    = [r[1]+r[3]/2 for r in rects], # bokeh wants y center, not corner
    w    = [r[2]        for r in rects],
    h    = [r[3]        for r in rects],
    size = [c[0]        for c in cats ],
    len  = [c[1]        for c in cats ],
))

fill_cmap = factor_cmap('size', palette="Pastel1_3", factors=['small', 'medium', 'large'])

p = figure(x_range=(0,1), y_range=(0,1), x_axis_location=None, y_axis_location=None,
           tools="", toolbar_location=None, tooltips="@size @len")

p.rect(x='x', y='y', width='w', height='h', line_color="white", source=source,
       fill_color=fill_cmap)

show(p)

This results in the plot below with an interactive hover tooltip that displays the categories. You could also add a colorbar or any other Bokeh interactive features directly in standard Bokeh ways:

enter image description here