Is there a way to add a single annotation on an altair chart?

Viewed 517

At the bottom of the Altair chart, I want to put my domain name. This is to watermark the chart. Is there a way to do this? Here's some sample code. I would like to put some text below the 100 on the x-axis. Is that possible?

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

x = np.arange(100)
source = pd.DataFrame({
  'x': x,
  'f(x)': np.sin(x / 5)
})

alt.Chart(source).mark_line().encode(
    x='x',
    y='f(x)'
)
1 Answers

There's no way to add a specific watermark, but you can abuse the chart title to do this. For example:

alt.Chart(source).mark_line().encode(
    x='x',
    y='f(x)'
).properties(
    title=alt.TitleParams(
        'http://mywebsite.com',
        color='lightgray',
        baseline='bottom',
        orient='bottom',
        anchor='end'
    )
)

enter image description here

If you still want an actual title on your chart, you can accomplish this by wrapping your chart in a concatenation, which has its own title:

chart = alt.Chart(source).mark_line().encode(
    x='x',
    y='f(x)'
).properties(
    title="The Title"
)

alt.concat(chart,
    title=alt.TitleParams(
        'http://mywebsite.com',
        color='lightgray',
        baseline='bottom',
        orient='bottom',
        anchor='end'
    )
)

enter image description here

Related