The Physical World and Realtime Plotting
In this post I’m going to show you a few of the fun and exciting things that can be done with Bokeh. Given the recent MakerFaire events in New York and Italy, I thought it would be fun to bring a bit of hardware into the mix rather than simply simulate streaming data. I decided to incorporate some XBee Modules, which are wireless devices popular among hardware enthusiasts and wireless sensor network (WSN) developers.
I set up a WSN with two XBees: one acts as the base station and the second is a mobile unit. The base station records the signal strength (RSSI) of the incoming packets from the mobile unit and dumps the data into a database. The closer the mobile unit is to the base station, the stronger the signal will be. I recorded several minutes of myself (with the mobile unit attached) running around the base station in order to generate a wide landscape of values. My plotting script can then ask for the last X points of data or a slice of rows within the DB.
After pulling the data, plotting a line plot should be quite familiar to other plotting libraries.
Below is a screenshot—you can see that the while signal is noisy, the farther the mobile unit is from the base station, the smaller the signal strength is:
I’ve added a few other plot elements in addition to plotting the data (x,rssi): height/width, title, legend, and pan/zoom/resize (more on this later)
The above plot is merely an image. But because Bokeh is a web-enabled plotting toolkit, if I render my plot in Bokeh I can interact with my data in exciting and novel ways. Below is the sample plot above but rendered with Bokeh:
You can manipulate this plot: zooming, panning, and resizing all in the browser! To achieve this level of interactivity, I added the additional arguments to the line function
Painless Realtime Plotting
Lastly, with Bokeh we can easily animate our plot and view the RSSI signal in realtime. For this we will need a server — and luckily enough, Bokeh ships with its own server. Calling
bokeh-server instantiates everything we need to serve up streaming plots.
With less than 15 lines of code, we’ve animated the plot and can serve it on the web. Below is a captured GIF and you can also see the plots live here. Note: more streaming/animation improvements are coming in 0.4 release
Code and Install
The example code can be found in my xbee-bokeh example repository and installing dependencies can either be done through conda or pip. For use with a Raspberry Pi orBeagleBone download the minimal anaconda installer for ARM architectures.
Conda is quite easy — simply add the following Anaconda.org channels to your .condarc file:
Then create an new environment:
conda create -n bokeh_rssi python=2.7 pip sqlite pyserial xbee flask-restful bokeh
Similarly, pip can also install all the necessary packages.
Interactivity through the web is more than just a novel feature. For many, it means changing their entire workflow. If I no longer have to take/generate a snapshot, this means my colleagues and collaborators can see analysis as it happens — no photo uploads or emails of static plots. And, as the data changes the plots similarly should update as well. Additionally, building a tool with the Python analyst/developer as our target audience means not having to reach for another language and stack of technologies to learn and manage.