简体   繁体   English

将样本编号附加到 altair 中的 X-Labels

[英]Appending sample number to X-Labels in altair

I would like to automatically append the sample # (in parentheses) corresponding to the x-labels of an altair figure.我想自动 append 样本#(在括号中)对应于 altair 图形的 x-labels。 I am open to doing this outside of altair, but I thought there may be a way to do it at the figure level using altair/vega-lite.我愿意在 altair 之外执行此操作,但我认为可能有一种方法可以使用 altair/vega-lite 在图形级别执行此操作。 I am pasting the code using an example from the altair/vega website (part of the vega_dataset), but with a hackneyed, manual method in which I rename the labels explicitly for one of the labels.我正在使用来自 altair/vega 网站(vega_dataset 的一部分)的示例粘贴代码,但使用了一种陈旧的手动方法,在该方法中,我为其中一个标签显式地重命名了标签。 In this case, I have added the sample number of 73 to Europe.在这种情况下,我已将样本编号 73 添加到欧洲。

Link to data 链接到数据

import altair as alt
from vega_datasets import data

df = data.cars()
df['Origin'] = df['Origin'].replace({'Europe':'Europe (n=73)'})

alt.Chart(df).transform_density(
    'Miles_per_Gallon',
    as_=['Miles_per_Gallon', 'density'],
    extent=[5, 50],
    groupby=['Origin']
).mark_area(orient='horizontal').encode(
    y='Miles_per_Gallon:Q',
    color='Origin:N',
    x=alt.X(
        'density:Q',
        stack='center',
        impute=None,
        title=None,
        axis=alt.Axis(labels=False, values=[0],grid=False, ticks=True),
    ),
    column=alt.Column(
        'Origin:N',
        header=alt.Header(
            titleOrient='bottom',
            labelOrient='bottom',
            labelPadding=0,
        ),
    )
).properties(
    width=100
).configure_facet(
    spacing=0
).configure_view(
    stroke=None
)

在此处输入图像描述

You could use pandas to generate the replacement dictionary and assign it to a new dataframe column:您可以使用 pandas 生成替换字典并将其分配给新的 dataframe 列:

import altair as alt
from vega_datasets import data

df = data.cars()
group_sizes = df.groupby('Origin').size()
replace_dict = group_sizes.index + ' (n=' + group_sizes.astype(str) + ')'
df['Origin_with_count'] = df['Origin'].replace(replace_dict)

alt.Chart(df).transform_density(
    'Miles_per_Gallon',
    as_=['Miles_per_Gallon', 'density'],
    extent=[5, 50],
    groupby=['Origin_with_count', 'Origin']
).mark_area(orient='horizontal').encode(
    y='Miles_per_Gallon:Q',
    color='Origin:N',
    x=alt.X(
        'density:Q',
        stack='center',
        impute=None,
        title=None,
        axis=alt.Axis(labels=False, values=[0],grid=False, ticks=True),
    ),
    column=alt.Column(
        'Origin_with_count:N',
        header=alt.Header(
            title=None,
            labelOrient='bottom',
            labelPadding=0,
        ),
    )
).properties(
    width=100
).configure_facet(
    spacing=0
).configure_view(
    stroke=None
)

You might be able to do something more elegant with labelExpr , not sure.不确定,您也许可以使用labelExpr做一些更优雅的事情。

在此处输入图像描述

You could overlay a text mark with the count instead.您可以用计数覆盖文本标记。
I was able to do this with the following code.我能够使用以下代码做到这一点。 I was not able to manage the y position of the text (see commented-out line) or use the n datum in the header labelExpr for some reason.由于某种原因,我无法管理文本的 y position(参见注释掉的行)或使用 header labelExpr 中的 n 数据。

df = data.cars()

violin = alt.Chart(df).transform_density(
    'Miles_per_Gallon',
    as_=['Miles_per_Gallon', 'density'],
    extent=[5, 50],
    groupby=['Origin']
).mark_area(orient='horizontal').encode(
    y='Miles_per_Gallon:Q',
    color='Origin:N',
    x=alt.X(
        'density:Q',
        stack='center',
        impute=None,
        title=None,
        axis=alt.Axis(labels=False, values=[0],grid=False, ticks=True),
    ),
).properties(width=100)

text = alt.Chart(df).mark_text().transform_aggregate(
    cnt='count()',
    groupby=["Origin"]
).transform_calculate(
    n = "'n=' + datum.cnt",
).encode(
#     y=alt.Y('mean(Miles_per_Gallon):Q'),
    text=alt.Text('n:N'), 
)

(violin + text).facet(
    column=alt.Column('Origin:N'),
).configure_header(
    labelExpr="[datum.value, datum.n]",
)

在此处输入图像描述

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

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