Search code examples
legenderrorbarholoviews

Holoviews: Format legend and colors of Spread and Curve Overlay


Given a tidy Pandas column with 4 or more columns, I want an otherwise very straightforward plot: two of the columns should be the x-y axes of a single figure, and one of the columns should index an Overlay of N Curve objects based on the x-y columns, and N Spread objects, using the final column as error. So if N=4 there should be 4 curves and four spreads. The curves and spreads with same index should be the same color, and the legend should attest to this.

Using table.to(hv.Curve,'col1','col2') I can get a Holomap for the curves, and with some effort I can do the same for the spread. If I then call .overlay() I get a nice figure for the curves including a legend, but when I do the same for the spread the legend vanishes. If I overlay the two, the legend likewise vanishes and the color cycle stops working, making all curves and spreads the same color. If I create a Holomap of curve*spread objects, then the colors match but the legend is still gone.

This seems like a very standard plot, but I can find very little in the Holoviews docs about pairing different Elements or controlling the legend.


Solution

  • This is a bit difficult to answer without any concrete code, for example I can't reproduce some of the issues you are describing. However the first issue is simply that show_legend is not enabled by default for the Spread elemen. In the case of plotting a Curve and Spread using .to and .overlay, here is what I can confirm works:

    %%opts Spread [show_legend=True width=600] Overlay [legend_position='right']
    df = pd.DataFrame({
        'index': np.arange(100), 'y': np.random.randn(100).cumsum(),
        'err': np.random.rand(100)+0.1, 'z': np.repeat(np.arange(10), 10)
    })
    ds = hv.Dataset(df)
    ds.to(hv.Curve, 'index', 'y', 'z').overlay() * ds.to(hv.Spread, 'index', ['y', 'err']).overlay()
    

    enter image description here

    If I create a Holomap of curve*spread objects, then the colors match but the legend is still gone.

    This is indeed a current limitation since we recommended against nesting objects in this way in the past, however I have just opened this PR which will allow this approach as well.