Skip to content Skip to sidebar Skip to footer

Bokeh Equivalent Of Matplotlib Scatter_matrix

Is there a better way of reproducing matplotlibs scatter_matrix (plot all data against all data) in Bokeh than the code below: defaults.width = 100 defaults.height = 100

Solution 1:

In general, to have linked panning/zooming, you share the ranges that you want to be linked between plots. This is described here in the Users Guide:

https://docs.bokeh.org/en/latest/docs/user_guide/interaction/linking.html

You can also check out this linked SPLOM example:

https://github.com/bokeh/bokeh/blob/master/examples/models/iris_splom.py

That example is longer/more verbose because it uses the low level bokeh.models API. The important part is where it re-uses the ranges xdr and ydr on ever plot that gets created.

In your particular case, since high level charts don't accept range parameters up front (IIRC), I think you'll have to fix up the charts "after the fact", so maybe something like:

xr = scatter_plots[0].x_range
yr = scatter_plots[0].y_range
for p in scatter_plots:
    p.x_range = xr
    p.y_range = yr

Solution 2:

In case it is useful, I faced the same problem. In actual fact you don't want all the axis linked - but rather each rows y-axis linked and each columns x-axis linked. I'm surprised that this isn't a built in bokeh feature. even iris the example gets this wrong:

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

Here's a code snippet I used:

def scatter_matrix(dataset):
    dataset_source = ColumnDataSource(data=dataset)
    scatter_plots = []
    y_max = len(dataset.columns)-1
    for i, y_col in enumerate(dataset.columns):
        for j, x_col in enumerate(dataset.columns):
            p = figure(plot_width=100, plot_height=100, x_axis_label=x_col, y_axis_label=y_col)
            p.circle(source=dataset_source,x=x_col, y=y_col, fill_alpha=0.3, line_alpha=0.3, size=3)
            if j > 0:
                p.yaxis.axis_label = ""
                p.yaxis.visible = False
                p.y_range = linked_y_range
            else:
                linked_y_range = p.y_range
                p.plot_width=160
            if i < y_max:
                p.xaxis.axis_label = ""
                p.xaxis.visible = False
            else:
                p.plot_height=140
            if i > 0:
                p.x_range = scatter_plots[j].x_range

            scatter_plots.append(p)

    grid = gridplot(scatter_plots, ncols = len(dataset.columns))
    show(grid)

Post a Comment for "Bokeh Equivalent Of Matplotlib Scatter_matrix"