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))

"""
Explanation: 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:
End of explanation
"""


%%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)

"""
Explanation: 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.
More detailed example
Even extremely complex relationships between data elements can be expressed succinctly in HoloViews:
End of explanation
"""


