I'm trying to create a scatter plot in Altair and I would like to add a text label or annotation to the point with the maximum y.
I've been able to add text_marks to all points, but I can't figure out how to label a specific point based on max(y)
Here's some example data:
df = pd.DataFrame(columns=['date', 'daily', 'total'],
data=[['2019-08-01', 29, 102370],
['2019-08-02', 18, 102388],
['2019-08-03', 19, 102407],
['2019-08-04', 13, 102420],
['2019-08-05', 29, 102449],
['2019-08-06', 49, 102498],
['2019-08-07', 31, 102529],
['2019-08-08', 39, 102568],
['2019-08-09', 23, 102591],
['2019-08-10', 17, 102608],
['2019-08-11', 18, 102626],
['2019-08-12', 38, 102664],
['2019-08-13', 22, 102686]])
This is what I came up with so far, but it doesn't do what I want and I think I may have over-complicated it
chart = alt.Chart(
data=df,
).mark_line(
color='red'
).encode(
alt.X('date:T', title=''),
alt.Y('daily:Q', title='')
)
text = alt.Chart(df).mark_text().encode(
x=alt.X('max(date):T'),
y=alt.Y('max(daily):Q'),
text=alt.Text('max(daily):Q')
)
(chart + text)
There are 2 ways of doing what you want:
pandas (for me it is the easiest but not always simple or available,transform_window to rank the data and a transform_filter to keep only the maximum valuePandas version:
chart = (
alt.Chart(data=df)
.mark_line(color="red")
.encode(alt.X("date:T", title=""), alt.Y("daily:Q", title=""))
)
text = (
alt.Chart(df.query("daily == daily.max()"))
.mark_text(dy=-15, color="red")
.encode(x=alt.X("date:T"), y=alt.Y("daily:Q"), text=alt.Text("daily:Q"))
)
(chart + text)
Vega-lite version:
chart = (
alt.Chart(data=df)
.mark_line(color="red")
.encode(alt.X("date:T", title=""), alt.Y("daily:Q", title=""))
)
text = (
alt.Chart(df)
.mark_text(dy=-15, color="red")
.transform_window(
sort=[alt.SortField("daily", order="descending")],
rank="rank(daily)"
)
.transform_filter(alt.datum.rank == 1)
.encode(x=alt.X("date:T"),
y=alt.Y("daily:Q"),
text=alt.Text("daily:Q"))
)
(chart + text)
Both code produce the chart below:

A concise way to do this in the Vega-Lite grammar is to use the argmax aggregate. For example:
chart = alt.Chart(
data=df,
).mark_line(
color='red'
).encode(
alt.X('date:T', title=''),
alt.Y('daily:Q', title='')
)
text = alt.Chart(df).mark_text(dy=-15, color="red").encode(
x=alt.X('date:T', aggregate={'argmax': 'daily'}),
y=alt.Y('max(daily):Q'),
text=alt.Text('max(daily):Q')
)
chart + text

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With