Skip to content Skip to sidebar Skip to footer

How To Set Order Of Bars According To Order/index Of Dataframe In Holoviews

I am facing this issue in HOLOVIEWS where in Im unable to get the order of bars on image the way it is on my df. I also tried many ways to order and sort but failed . The image dis

Solution 1:

If you want to sort the outer index, you can use .sort(dimension, reverse=True) on your hv.Bars object. However sorting the inner index will require you to set the dimension values explicitly:

from bokeh.io import show
import holoviews as hv
hv.extension("bokeh")


df = df.groupby(["set", "flag"])["id"].count().reset_index()
count_bars = hv.Bars(df, kdims=["set","flag"], vdims="id")

plot = (count_bars
        .opts(hooks=[hook1], title="IDs",invert_axes=True, width=500, padding=2)
        .redim.values(flag=["Y", "N"]) # Inverting the axes flips this order. This produces N, Y vertically
        .sort("set", reverse=True)
       )

bokeh_obj = hv.render(plot, backend="bokeh")
show(bokeh_obj)

Produces: enter image description here

Solution 2:

As a quickfix/workaround I have reversed the index to appear in the order of df , but I think this should be fixed in holoviews in the future

    df['indexn'] = df.index
    df.indexn = df.indexn.values[::-1]
    order = df.sort_values(by='indexn').set
    order1 = df.sort_values(by='indexn').flag
    count_bars = hv.Bars(df, kdims=["set","flag"], vdims="id").redim.values(set=order,flag=order1)

Post a Comment for "How To Set Order Of Bars According To Order/index Of Dataframe In Holoviews"