Skip to content

asher-pembroke/bokeh-tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

This is a collection of functions for scientific visualization in the lovely web-based graphing library Bokeh.

Bokeh has many great features for interactive 2D plots, particularly for lines and glyphs. However, it doesn't do shading or filled contouring, though these features will come eventually. In the meantime, one option is to create rastered images of your data, but this takes a lot more bandwidth and slows down plots on the user end. Here is a work-around that creates filled contour plots and renders them as a set of polygons, based on matplotlib's contourf function.

Below shows output from Bokeh's image function (left) vs the filled contoured version (right):

Note: images do not use the same contours or palettes.

Requirements

Methodology

I used output from matplotlib's contourf function to get a set of path objects. These are converted into polygons with holes. Matplotlib uses a MoveTo command to carve out interior holes from exterior polygons, but bokeh's plotting routines don't have this functionality: all polygons have to be simple. However, we can convert a polygon with holes into a simple polygon by introducing cuts that join the exterior boundaries to the interior holes. I used the solution described here: http://cs.stackexchange.com/questions/43758/turning-a-simple-polygon-with-holes-into-exterior-bounded-only

The main goal is to find a single path which traverses the exterior and interior holes, touching all vertices (some twice, where bridges between the holes occur). First, we compute the edges of a delaunay graph of all the points, giving each point a label indicating which hole it belonged to and 0 for the exterior boundary. Then, we compute the minimum spanning tree (mst) using edge lengths for the weights, which generates a graph that touches all the points once. The mst looks a lot like the original polygons, plus a few bridges. For some holes, there may be more than one bridge connecting it to another hole, so to get a unique bridge we create a graph with the exterior + holes as the nodes and bridges as the edges. We then compute the minimum spanning tree of that graph to get the unique bridges that connect all the holes with each other and the exterior boundary. To get the final path, we put the original points in a directed graph, insert bridges as degenerate pairs of edges, then compute the shortest path from any starting vertex back to itself.