HoloViews is a Python library that makes analyzing and visualizing scientific or engineering data much simpler, more intuitive, and more reproducible. For instance, if you have a two-dimensional dataset like the fractal below (mandelbrot.npy), it only takes one line of HoloViews code to mark it with a horizontal line indicating a cross section, add its histogram, sample to get the cross section, and display the result:
In [ ]:
import numpy as np
import holoviews as hv
%reload_ext holoviews.ipython
fractal = hv.Image(np.load('mandelbrot.npy'))
((fractal * hv.HLine(y=0)).hist() + fractal.sample(y=0))
To use HoloViews, you first wrap your data in a HoloViews component along with optional metadata describing it. It will then display itself automatically on its own or in combination with any other HoloViews component. The separate matplotlib library does the plotting, but none of the data structures depend on the plotting code, so that you can easily create, save, load, and manipulate HoloViews objects from within your own programs. HoloViews objects support arbitrary combination, selection, slicing, sorting, sampling, and animation, to allow you to focus on whatever aspect of your data you wish. Instead of writing or maintaining complex plotting code, just declare what data you want to see, and HoloViews will handle the rest.
Even extremely complex relationships between data elements can be expressed succinctly in HoloViews:
In [ ]:
%%opts Points [scaling_factor=50] Contours (color='w')
dots = np.linspace(-0.45, 0.45, 19)
hv.HoloMap({y: (fractal * fractal.sample([(i,y) for i in dots]).to.points(['x','y'], 'z') +
fractal.sample(y=y) +
hv.operation.threshold(fractal, level=np.percentile(fractal.sample(y=y).data, 90)) +
hv.operation.contours(fractal, levels=[np.percentile(fractal.sample(y=y).data, 60)]))
for y in np.linspace(-0.3, 0.3, 21)}, kdims=['Y']).collate().cols(2)
Here we have taken the same fractal data and indicated a horizontal cross section using a set of dots with sizes proportional to the underlying data values in A, illustrating how even a simple annotation can be used to reflect other data of interest. We then add a cross section curve B, a thresholded version of the data C, and a version of the data with a contour outline overlaid D. Here the threshold and contour levels are not fixed, but are calculated as the 90th or 60th percentile of the data values along that cross section. All of this data is then packaged into a single data structure for a range of cross sections, allowing the data for a particular cross section to be revealed by moving the Y-value slider at right. Even with these complicated interrelationships between data elements, the code still only needs to focus on the data that you want to see, not on the details of the plotting.
Note that just as the 2D array became a 1D curve automatically by sampling to get the cross section, this entire figure would become a single static frame with no slider bar if you chose a specific Y
value by re-running with .select(Y=0.3)
before .cols(2)
. In fact, there is nothing in the code above that adds the slider bar explicitly -- it appears automatically just because there is an additional dimension of data (Y
in this case) that has not been laid out spatially. Additional sliders would appear if there were other dimensions being varied, e.g. for parameter space explorations.
This functionality is designed to complement the IPython Notebook interface, though it can be used just as well separately. This web page and all the HoloViews Tutorials are runnable notebooks, which allow you to interleave text, Python code, and graphical results easily. With HoloViews, you can just put a minimum of code in the notebook (typically one or two lines per subfigure), specifying what you would like to see rather than the details of how it should be plotted. HoloViews makes the IPython Notebook a practical solution for both exploratory research (since viewing nearly any chunk of data just takes a line or two of code) and for long-term reproducibility of the work (because both the code and the resulting figure are preserved in the notebook file forever). See the Tutorials for detailed examples, and then start enjoying working with your data!