简体   繁体   English

如何使用 hvplot 绘制堆积条形图?

[英]How to plot a stacked bar chart using hvplot?

I am trying to plot a stacked bar chart with 3 categorical variables and 1 numerical variable using hvplot.我正在尝试使用 hvplot 绘制具有 3 个分类变量和 1 个数值变量的堆积条形图。

Does anyone know how to properly plot the stacked bar chart?有谁知道如何正确绘制堆积条形图?
Request Type 'D' & 'S' are not shown in different colors.请求类型“D”和“S”未以不同颜色显示。

Data:数据:
具有 3 个类别和 1 个数值的数据图像

My code:我的代码:

by_type = test_df.groupby(['Type', 'Fiscal Period', 'Request Type']).agg({'Count': np.sum})
plot_by_type = by_type.hvplot.bar('Type','Count', by=['Fiscal Period', 'Request Type'], stacked=False, 
                                    flip_xaxis=False, ylabel='Total Count', invert=False,
                                                     cmap=['silver', 'goldenrod'])
plot_by_type

Below is the plot I get:下面是我得到的情节: 具有 3 个类别和 1 个数值的数据图像

Currently it is not possible in HoloViews (1.13) to have more than 2 categorical variables for a barchart.目前,在 HoloViews (1.13) 中,条形图不可能有 2 个以上的分类变量。

See also this github issue:另请参阅此 github 问题:
https://github.com/holoviz/holoviews/issues/2878 https://github.com/holoviz/holoviews/issues/2878

However, you could do a workaround like this:但是,您可以执行以下解决方法:
The trick is to put one categorical as x , one categorical variable in the by keyword, and other categorical variables in the groupby keyword.诀窍是将一个分类变量作为x ,一个分类变量放在by关键字中,并将其他分类变量放在groupby关键字中。

import pandas as pd
import hvplot.pandas

# create sample data
df = 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 Type
layout = 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 barchart
plotA = 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')



Resulting plot:结果图:

具有 3 个分类变量的条形图的解决方法

As a bonus, this code will give you the same result as above:作为奖励,此代码将为您提供与上述相同的结果:

def 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 plot

plotA = 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM