带箭头的折线图#

本示例展示了一个带有两种箭头标注的简单折线图。Unicode 字符方法更简单,而线条加三角形的 点标记 允许更大的灵活性,例如自定义箭头形状。

import altair as alt
import numpy as np
import pandas as pd


x = np.linspace(1,7)
data = pd.DataFrame({
    "x": x,
    "y": np.sin(x),
})

unicode_arrow_with_text = alt.layer(
    # Arrow
    alt.Chart().mark_text(size=60, align="left", baseline="bottom", fontWeight=100, angle=340).encode(
        x=alt.datum(2.8),
        y=alt.datum(-0.3),
        text=alt.datum("🠃")  # Any unicode symbol could be used instead
    ),
    # Text
    alt.Chart().mark_text(size=14, align="center", baseline="bottom").encode(
        x=alt.datum(1.8),
        y=alt.datum(-0.1),
        text=alt.datum("decreasing")
    )
)

mark_arrow_with_text = alt.layer(
    # Arrow line
    alt.Chart().mark_line(size=2).encode(
        x=alt.datum(5.4),
        y=alt.datum(-0.4),
        x2=alt.datum(5.9),
        y2=alt.datum(0)
    ),
    # Arrow head
    alt.Chart().mark_point(shape="triangle", filled=True, fillOpacity=1).encode(
        x=alt.datum(5.9),
        y=alt.datum(0),
        angle=alt.AngleValue(23),
        size=alt.SizeValue(100),
        color=alt.ColorValue("#000000")
    ),
    # Text
    alt.Chart().mark_text(size=14, align="center", baseline="bottom").encode(
        x=alt.datum(4.7),
        y=alt.datum(-0.3),
        text=alt.datum("increasing")
    ),
)

line_with_points = alt.Chart(data).mark_line(point=True).encode(
    x=alt.X("x"),
    y=alt.Y("y"),
)

line_with_points + unicode_arrow_with_text + mark_arrow_with_text
import altair as alt
import numpy as np
import pandas as pd


x = np.linspace(1,7)
data = pd.DataFrame({
    "x": x,
    "y": np.sin(x),
})

unicode_arrow_with_text = alt.layer(
    # Arrow
    alt.Chart().mark_text(size=60, align="left", baseline="bottom", fontWeight=100, angle=340).encode(
        x=alt.datum(2.8),
        y=alt.datum(-0.3),
        text=alt.datum("🠃")  # Any unicode symbol could be used instead
    ),
    # Text
    alt.Chart().mark_text(size=14, align="center", baseline="bottom").encode(
        x=alt.datum(1.8),
        y=alt.datum(-0.1),
        text=alt.datum("decreasing")
    )
)

mark_arrow_with_text = alt.layer(
    # Arrow line
    alt.Chart().mark_line(size=2).encode(
        x=alt.datum(5.4),
        y=alt.datum(-0.4),
        x2=alt.datum(5.9),
        y2=alt.datum(0)
    ),
    # Arrow head
    alt.Chart().mark_point(shape="triangle", filled=True, fillOpacity=1).encode(
        x=alt.datum(5.9),
        y=alt.datum(0),
        angle=alt.AngleValue(23),
        size=alt.SizeValue(100),
        color=alt.ColorValue("#000000")
    ),
    # Text
    alt.Chart().mark_text(size=14, align="center", baseline="bottom").encode(
        x=alt.datum(4.7),
        y=alt.datum(-0.3),
        text=alt.datum("increasing")
    ),
)

line_with_points = alt.Chart(data).mark_line(point=True).encode(
    x=alt.X("x"),
    y=alt.Y("y"),
)

line_with_points + unicode_arrow_with_text + mark_arrow_with_text
# No channel encoding options are specified in this chart
# so the code is the same as for the method-based syntax.