Skip to content Skip to sidebar Skip to footer

How Can I Position Text Relative To Axes In Altair?

I'm trying to make a graph much like the Multi-Line Tooltip Example. However I want to make two modifications. First of all, I want to be able to zoom in to a portion of the data.

Solution 1:

You can control the text position with the x and y encodings. Here is an example of placing text at the top of the axis:

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

data = pd.DataFrame({
    'x': np.arange(1, 21),
    'y': np.random.randint(0, 20, 20),
})

points = alt.Chart(data).mark_point().encode(
    x='x',
    y='y'
)

text = points.mark_text(baseline='top').encode(
    y=alt.value(0),  # pixels from top
    text='y'
)

points + text

enter image description here

Post a Comment for "How Can I Position Text Relative To Axes In Altair?"