Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add a single text label to the max point in an Altair scatter plot?

Tags:

python

altair

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)
like image 475
Chris Avatar asked Nov 15 '25 18:11

Chris


2 Answers

There are 2 ways of doing what you want:

  1. Filter the data with pandas (for me it is the easiest but not always simple or available,
  2. A vega-lite version, with filters: a transform_window to rank the data and a transform_filter to keep only the maximum value

Pandas 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:

chart with only max value as text

like image 119
FlorianGD Avatar answered Nov 17 '25 10:11

FlorianGD


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

enter image description here

like image 40
jakevdp Avatar answered Nov 17 '25 08:11

jakevdp



Donate For Us

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