对于普通条形图,您可以按照您想要的方式对数据进行排序。
但是,对于分组条形图,您还不能设置顺序。
但是此功能的开发正在进行中,并且可能在下一个版本中可用:https :
//github.com/holoviz/holoviews/issues/3799 Hvplot 0.5.2 和 Holoviews 1.12 的当前解决方案:
1)如果你重新使用 Bokeh 后端,您可以使用关键字 hooks:
from itertools import product
# define hook function to set order on bokeh plot
def set_grouped_barplot_order(plot, element):
# define you categorical ordering in a list of tuples
factors = product(['2', '1'], ['le', 'la', 'lu'])
# since you're using horizontal bar set order on y_range.factors
# if you would have had a normal (vertical) barplot you would use x_range.factors
plot.state.y_range.factors = [*factors]
# create plot
group = df.groupby("group").sum()
group_plot = group.hvplot.barh(
x="group",
y=["le", "la", "lu"],
padding=0.05,
)
# apply your special ordering function
group_plot.opts(hooks=[set_grouped_barplot_order], backend='bokeh')
挂钩允许您将特定的散景设置应用于您的绘图。您不需要经常使用钩子,但在这种情况下它们非常方便。文档:
http ://holoviews.org/user_guide/Customizing_Plots.html#Plot-hooks
https://holoviews.org/FAQ.html
2) 另一种解决方案是将您的 Holoviews 图转换为实际的散景图,然后设置订购:
from itertools import product
import holoviews as hv
from bokeh.plotting import show
# create plot
group = df.groupby("group").sum()
group_plot = group.hvplot.barh(
x="group",
y=["le", "la", "lu"],
padding=0.05,
)
# render your holoviews plot as a bokeh plot
my_bokeh_plot = hv.render(group_plot, backend='bokeh')
# set the custom ordering on your bokeh plot
factors = product(['2', '1'], ['le', 'la', 'lu'])
my_bokeh_plot.y_range.factors = [*factors]
show(my_bokeh_plot)
我个人更喜欢第一个解决方案,因为它保留在 Holoviews 中。
结果图: