包装Altair中的图例文本

huangapple go评论63阅读模式
英文:

Wrap legend text in altair

问题

以下是您要翻译的内容:

"I have the following example where I'm trying to plot 4 points. When I wrap the text for the point legend labels the result is only 1 point for each category (2 points total).

Any help figuring out how to wrap legend text without losing data would be greatly appreciated."

import pandas as pd
import altair as alt
import textwrap

# Example DataFrame
df = pd.DataFrame({'point': ['a', 'b', 'c', 'd'],
                   'label': ['My super long label that is too long',
                             'Short label',
                             'My super long label that is too long',
                             'Short label'],
                    'x': [1,2,3,4],
                    'y': [1,2,3,4]})

# Wrap text in the 'label' column
df['label_wrapped'] = df['label'].apply(textwrap.wrap, args=[15])

chart = alt.Chart(df).mark_circle().encode(
    x='x',
    y='y',
    color=alt.Color('label_wrapped:N', title='Label'),
)
chart

包装Altair中的图例文本

英文:

I have the following example where I'm trying to plot 4 points. When I wrap the text for the point legend labels the result is only 1 point for each category (2 points total).

Any help figuring out how to wrap legend text without losing data would be greatly appreciated.

import pandas as pd
import altair as alt
import textwrap

# Example DataFrame
df = pd.DataFrame({'point': ['a', 'b', 'c', 'd'],
                   'label': ['My super long label that is too long',
                             'Short label',
                             'My super long label that is too long',
                             'Short label'],
                    'x': [1,2,3,4],
                    'y': [1,2,3,4]})

# Wrap text in the 'label' column
df['label_wrapped'] = df['label'].apply(textwrap.wrap, args=[15])

chart = alt.Chart(df).mark_circle().encode(
    x='x',
    y='y',
    color=alt.Color('label_wrapped:N', title='Label'),
)
chart

包装Altair中的图例文本

答案1

得分: 3

我通过使用labelExpr找到了一个解决方案。

import pandas as pd
import altair as alt
import textwrap

# 示例 DataFrame
df = pd.DataFrame({'point': ['a', 'b', 'c', 'd'],
                   'label': ['My super long label that is too long',
                             'Short label',
                             'My super long label that is too long',
                             'Short label'],
                   'x': [1, 2, 3, 4],
                   'y': [1, 2, 3, 4]})

# 在'label'列中包装文本
df["label_wrapped"] = df["label"].apply(lambda x: "|".join(textwrap.wrap(x, width=15)))

chart = alt.Chart(df).mark_circle().encode(
    x='x',
    y='y',
    color=alt.Color('label_wrapped:N',
                    title='Label',
                    legend=alt.Legend(labelExpr="split(datum.label, '|')")),
    tooltip=[
        alt.Tooltip("label", title="name"),
        alt.Tooltip("point")
    ],
)
chart

包装Altair中的图例文本

英文:

I found a solution by using the labelExpr

import pandas as pd
import altair as alt
import textwrap

# Example DataFrame
df = pd.DataFrame({'point': ['a', 'b', 'c', 'd'],
                   'label': ['My super long label that is too long',
                             'Short label',
                             'My super long label that is too long',
                             'Short label'],
                    'x': [1,2,3,4],
                    'y': [1,2,3,4]})

# Wrap text in the 'label' column
df["label_wrapped"] = df["label"].apply(lambda x: "|".join(textwrap.wrap(x, width=15)))

chart = alt.Chart(df).mark_circle().encode(
    x='x',
    y='y',
    color=alt.Color('label_wrapped:N',
                    title='Label',
                    legend=alt.Legend(labelExpr="split(datum.label, '|')")),
    tooltip=[
        alt.Tooltip("label", title="name"),
        alt.Tooltip("point")
    ],
)
chart

包装Altair中的图例文本

huangapple
  • 本文由 发表于 2023年7月11日 04:50:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定