如何根据 Holoviews 中数据帧的顺序/索引设置条的顺序 [英] How to set order of bars according to order/index of dataframe in Holoviews

查看:41
本文介绍了如何根据 Holoviews 中数据帧的顺序/索引设置条的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 HOLOVIEWS 中面临这个问题,其中我无法像在我的 df 上那样获取图像上的条形顺序.我也尝试了很多方法来ordersort 但都失败了.显示的图像与预期的顺序完全相反.

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

这是结果 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

我希望我的图像与此 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)

我得到了结果图像:-

我不想这样,我希望顺序与我的数据帧的顺序相同:-APPLE-N,Y,然后BALL NY..(从上到下)等等.我也没有想要对变量进行编码以进行排序,因为我有一个很大的 df 和许多 sets ,并且很难在代码中对变量名称进行排序,并且如果我要创建一个 function

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

推荐答案

作为快速修复/解决方法,我已将索引反转为按 df 的顺序出现,但我认为这应该在holoviews 未来

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)

这篇关于如何根据 Holoviews 中数据帧的顺序/索引设置条的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆