英文:
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
英文:
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
答案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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论