Search code examples
pythondataframeplotholoviews

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 displayed is in the exact reverse order to what is expected.

df = pd.DataFrame({
    "set": list("ABABCCAD"),
    "flag": list("YYNNNYNY"),
    "id": list("DEFGHIJK"),
})
df["set"] = df["set"].map(
    {"A": "APPLE",
     "B": "BALL",
     "C": "CAT",
     "D": "DOG"
    }
)

This is the resulting df :-

    set   flag  id
0   APPLE   N   2
1   APPLE   Y   1
2   BALL    N   1
3   BALL    Y   1
4   CAT     N   1
5   CAT     Y   1
6   DOG     Y   1

I want my image in the same order as this df

def hook1(plot, element):
    plot.state.y_range.range_padding = 0.1

   
plot.state.text(
    y="xoffsets",
    x="id",
    text="id",
    source=plot.handles["source"],
    text_align="left",
    y_offset=10,
    x_offset=5    
)

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))
bokeh_obj = hv.render(plot, backend="bokeh")
show(bokeh_obj)

I get the resulting image :- enter image description here

I don't want it this way, I want the order as the same as that of my dataframe in this order :-APPLE-N,Y, then BALL N,Y..(from top to bottom).and so on.I also don't want to code the variables to order by, because I have a large df with many sets , and it would be tough to give the variable names to order in the code and also it will not make sense if im going to create a function


Solution

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