Search code examples
pythonholoviews

How can I color a Curve by the value of a kdim with Holoviews?


I would like to .overlay on key dimension, but also use a gradient palette to color the Curve by the value of the key dimension.

So a simple overlay would be

%%opts Curve [show_legend=False]
hv.HoloMap({i:hv.Curve([i]*100) for i in range(10)}, ["i"]).overlay("i")

with horizontal lines.

How can I use matplotlib.cm.viridis colors with a gradient from i=0 blue to i=9 yellow?


Solution

  • To draw multiple paths it is usually best to use the Path and Contours element. More specifically, if each curve you want to draw has a single value associated with it the Contours element is most appropriate. To do what you want you create a list of curves with x and y values along with the appropriate value of 'i' as dictionaries, which you can then pass to the Contours constructor. The final step is to declare 'i' as a value dimension (vdim) of the Contours and set color_index='i' and cmap:

    curves = [{'x': np.arange(100), 'y': [i]*100, 'i': i} for i in range(10)]
    hv.Contours(curves, vdims=['i']).options(color_index='i', cmap='viridis')
    

    enter image description here