最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Altair line chart with last values labeled - how to stop overlapping labels? - Stack Overflow

programmeradmin3浏览0评论

How can I stop line labels from overlapping when the last values that the labels are pinned to are close together? I am using Altair 5.5.

import altair as alt
from vega_datasets import data

# Import example data
source = data.stocks()

# Create a common chart object
chart = alt.Chart(source).transform_filter(
    alt.datum.symbol != "IBM"  # A reducation of the dataset to clarify our example. Not required.
).encode(
    alt.Color("symbol").legend(None)
)

# Draw the line
line = chart.mark_line().encode(
    x="date:T",
    y="price:Q"
)

# Use the `argmax` aggregate to limit the dataset to the final value
label = chart.encode(
    x='max(date):T',
    y=alt.Y('price:Q').aggregate(argmax='date'),
    text='symbol'
)

# Create a text label
text = label.mark_text(align='left', dx=4)

# Create a circle annotation
circle = label.mark_circle()

# Draw the chart with all the layers combined
line + circle + text

How can I stop line labels from overlapping when the last values that the labels are pinned to are close together? I am using Altair 5.5.

import altair as alt
from vega_datasets import data

# Import example data
source = data.stocks()

# Create a common chart object
chart = alt.Chart(source).transform_filter(
    alt.datum.symbol != "IBM"  # A reducation of the dataset to clarify our example. Not required.
).encode(
    alt.Color("symbol").legend(None)
)

# Draw the line
line = chart.mark_line().encode(
    x="date:T",
    y="price:Q"
)

# Use the `argmax` aggregate to limit the dataset to the final value
label = chart.encode(
    x='max(date):T',
    y=alt.Y('price:Q').aggregate(argmax='date'),
    text='symbol'
)

# Create a text label
text = label.mark_text(align='left', dx=4)

# Create a circle annotation
circle = label.mark_circle()

# Draw the chart with all the layers combined
line + circle + text
Share Improve this question asked Feb 17 at 4:44 footfalconfootfalcon 6197 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It's currently not possible for altair to auto position labels so they are not overlapping. Their is a vega-lite issue here.

If you're willing to manually specify the shifts you can do it like this

import altair as alt
from vega_datasets import data

# Import example data
source = data.stocks()


# Create a common chart object
chart = alt.Chart(
    source
    # ).transform_filter(
    # alt.datum.symbol != "IBM"  # A reducation of the dataset to clarify our example. Not required.
).encode(alt.Color("symbol").legend(None))

# Draw the line
line = chart.mark_line().encode(x="date:T", y="price:Q")

# Use the `argmax` aggregate to limit the dataset to the final value
label = chart.encode(
    x="max(date):T", y=alt.Y("price:Q").aggregate(argmax="date"), text="symbol"
)

offset = 8
# Create a text label
text = label.mark_text(
    align="left",
    dx=4,
    dy=alt.expr(
        alt.expr.if_(
            alt.datum.symbol == "IBM",
            offset,
            alt.expr.if_(alt.datum.symbol == "AMZN", -offset, 0),
        )
    ),
)

# Create a circle annotation
circle = label.mark_circle()

# Draw the chart with all the layers combined
line + circle + text

发布评论

评论列表(0)

  1. 暂无评论