使用hvplot/holoviews更改分组条形图中的条形顺序 [英] Change the order of bars in a grouped barplot with hvplot/holoviews

查看:97
本文介绍了使用hvplot/holoviews更改分组条形图中的条形顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个分组的条形图,但无法弄清楚如何影响该条形图的顺序.

I try to create a grouped bar plot but can't figure out how to influence the order of the barplot.

给出以下示例数据:

import pandas as pd
import hvplot.pandas

df = pd.DataFrame({
    "lu": [200, 100, 10], 
    "le": [220, 80, 130], 
    "la": [60, 20, 15], 
    "group": [1, 2, 2],
})
df = df.groupby("group").sum()

我想创建一个水平分组的条形图,以显示第1组和第2组以及所有三列.列应按"le","la"和"lu"的顺序显示.

I'd like to create a horizontal grouped bar plot showing the two groups 1 and 2 with all three columns. The columns should appear in the order of "le", "la" and "lu".

自然,我会尝试使用Hvplot:

Naturally I'd try this with Hvplot:

df.hvplot.barh(x = "group", y = ["le", "la", "lu"])

这样我得到以下结果:

With that I get the result below:

Hvplot似乎并不关心我添加列的顺序(调用 df.hvplot.barh(x ="group",y = ["lu","le","la"])不会更改任何内容,Hvplot似乎也不关心数据帧中的原始顺序.

Hvplot does not seem to care about the order I add the columns (calling df.hvplot.barh(x = "group", y = ["lu", "le", "la"]) doesn't change anything. Nor does Hvplot seem to care about the original order in the dataframe.

是否有其他选项可以影响条形图的顺序?

Are there any options to influence the order of the bars?

推荐答案

此问题刚刚在HoloViews 1.13中进行了修复.

This has just been fixed in HoloViews 1.13.

您可以按照自己的意愿对条形图进行排序:

You can sort your barplot just like you wanted:

df.hvplot.barh(x="group", y=["lu", "la", "le"])


在我撰写本文时,尚未正式提供HoloViews 1.13,但您可以通过以下方式安装它:


As I write this, HoloViews 1.13 is not officially available yet, but you can install it through:

pip安装git + https://github.com/holoviz/holoviews.git


如果您想进一步控制订单,可以在grouped_barplot上使用.redim.values():

group_specific_order = [2, 1]
variable_specific_order = ['lu', 'la', 'le']

# Note that group and Variable are the variable names of your dimensions here
# when you use this on a different grouped barchart, then please change to the 
# names of your own dimensions.
your_grouped_barplot.redim.values(
    group=group_specific_order, 
    Variable=variable_specific_order,
)

这篇关于使用hvplot/holoviews更改分组条形图中的条形顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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