通过组合列值在Altair中绘制网格堆积条形图

时间:2020-10-18 21:09:30

标签: pandas altair

我想像示例Trellis Stacked Bar Chart那样绘制网格堆积条形图图。

我有这个数据集:

pd.DataFrame({
    
    'storage': ['dev01', 'dev01', 'dev01', 'dev02', 'dev02', 'dev03'],
    'project': ['omega', 'alpha', 'beta', 'omega', 'beta', 'alpha'],
    'read': [3, 0, 0, 114, 27, 82],
    'write': [70, 0, 0, 45, 655, 203],
    'read-write': [313, 322, 45, 89, 90, 12]
    
})

  storage project  read  write  read-write
0   dev01   omega     3     70         313
1   dev01   alpha     0      0         322
2   dev01    beta     0      0          45
3   dev02   omega   114     45          89
4   dev02    beta    27    655          90
5   dev03   alpha    82    203          12

我不知道如何指定readwriteread-write列作为Altair的颜色/值。

2 个答案:

答案 0 :(得分:3)

您的数据为宽格式,必须转换为长格式才能在Altair编码中使用。有关更多信息,请参见Altair文档中的Long-Form vs. Wide-Form Data

这可以通过使用pd.melt修改Pandas中的输入数据来解决,但是使用Altair的Fold Transform在图表规范中进行此重塑通常更方便。例如:

import pandas as pd
import altair as alt

df = pd.DataFrame({
    'storage': ['dev01', 'dev01', 'dev01', 'dev02', 'dev02', 'dev03'],
    'project': ['omega', 'alpha', 'beta', 'omega', 'beta', 'alpha'],
    'read': [3, 0, 0, 114, 27, 82],
    'write': [70, 0, 0, 45, 655, 203],
    'read-write': [313, 322, 45, 89, 90, 12]
})

alt.Chart(df).transform_fold(
    ['read', 'write', 'read-write'],
    as_=['mode', 'value']
).mark_bar().encode(
    x='value:Q',
    y='project:N',
    column='storage:N',
    color='mode:N'
).properties(
    width=200
)

enter image description here

答案 1 :(得分:2)

您需要将所需的列melt插入新列:

# assuming your DataFrame is assigned to `df`

cols_to_melt = ['read', 'write', 'read-write']
cols_to_keep = df.columns.difference(cols_to_melt)

df = df.melt(cols_to_keep, cols_to_melt, 'mode')

因此您将获得以下信息:

   project storage        mode  value
0    omega   dev01        read      3
1    alpha   dev01        read      0
2     beta   dev01        read      0
3    omega   dev02        read    114
4     beta   dev02        read     27
5    alpha   dev03        read     82
6    omega   dev01       write     70
7    alpha   dev01       write      0
8     beta   dev01       write      0
9    omega   dev02       write     45
10    beta   dev02       write    655
11   alpha   dev03       write    203
12   omega   dev01  read-write    313
13   alpha   dev01  read-write    322
14    beta   dev01  read-write     45
15   omega   dev02  read-write     89
16    beta   dev02  read-write     90
17   alpha   dev03  read-write     12

然后在替代代码段中,使用color='site'代替color='mode'