繁华开满天机
目前,在 HoloViews (1.13) 中,条形图不可能有超过 2 个分类变量。另请参阅此 github 问题:https ://github.com/holoviz/holoviews/issues/2878但是,您可以执行以下解决方法:诀窍是在关键字中放置一个x分类变量,一个分类变量,以及其他分类变量关键字by中的变量。groupbyimport pandas as pdimport hvplot.pandas# create sample datadf = pd.DataFrame({ 'Type': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'], 'Fiscal Period': ['2019-01', '2019-01', '2019-02', '2019-02', '2019-01', '2019-01', '2019-02', '2019-02'], 'Request Type': ['S', 'D', 'S', 'D', 'S', 'D', 'S', 'D'], 'values': range(1, 9),})# create a separate barchart per Typelayout = df.hvplot.bar( x='Fiscal Period', y='values', by='Request Type', groupby='Type', stacked=True, cmap='Category20', legend='top_left', width=400, xlabel='',).layout()# make plots nicer so they look more like a clustered barchartplotA = layout['A'].opts(title='Type: A')plotB = layout['B'].opts(show_legend=False, yaxis=None, ylabel='', title='Type: B')# add separate plots together again(plotA + plotB).opts(title='Showing the counts per Fiscal Period, Request Type and Type')结果图:作为奖励,此代码将为您提供与上述相同的结果:https://i.stack.imgur.com/fcl7z.pngdef create_subplot(type_selected): plot = df[df['Type'] == type_selected].hvplot.bar( x='Fiscal Period', y='values', by='Request Type', stacked=True, cmap='Category20', label='Type: ' + type_selected, legend='top_left', width=400, xlabel='', ylabel='', ) return plotplotA = create_subplot('A')plotB = create_subplot('B').opts(show_legend=False, yaxis=None)(plotA + plotB).opts(title='Showing the counts per Fiscal Period, Request Type and Type')