Skip to content

Commit

Permalink
Add argmax example (#2653)
Browse files Browse the repository at this point in the history
* added argmax example

* Update line_chart_with_custom_legend.py

Fixed y axis labeling

* Apply suggestions from code review

Co-authored-by: Joel Ostblom <joelostblom@users.noreply.github.com>

* Update code formatting

Co-authored-by: Joel Ostblom <joelostblom@users.noreply.github.com>
  • Loading branch information
tempdata73 and joelostblom authored Jul 10, 2022
1 parent b7cf627 commit 18d8094
Showing 2 changed files with 41 additions and 1 deletion.
40 changes: 40 additions & 0 deletions altair/examples/line_chart_with_custom_legend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Line Chart with Custom Legend
-----------------------------
This example uses the argmax aggregation function in order to create a custom
legend for a line chart.
"""
# category: line charts
import altair as alt
from vega_datasets import data


source = data.stocks()

base = alt.Chart(source).encode(
color=alt.Color("symbol", legend=None)
).transform_filter(
"datum.symbol !== 'IBM'"
).properties(
width=500
)

line = base.mark_line().encode(x="date", y="price")


last_price = base.mark_circle().encode(
x=alt.X("last_date['date']:T"),
y=alt.Y("last_date['price']:Q")
).transform_aggregate(
last_date="argmax(date)",
groupby=["symbol"]
)

company_name = last_price.mark_text(align="left", dx=4).encode(text="symbol")

chart = (line + last_price + company_name).encode(
x=alt.X(title="date"),
y=alt.Y(title="price")
)

chart
2 changes: 1 addition & 1 deletion doc/user_guide/encoding.rst
Original file line number Diff line number Diff line change
@@ -426,7 +426,7 @@ aggregation functions built into Altair; they are listed in the following table:
Aggregate Description Example
========= =========================================================================== =====================================
argmin An input data object containing the minimum field value. N/A
argmax An input data object containing the maximum field value. N/A
argmax An input data object containing the maximum field value. :ref:`gallery_line_chart_with_custom_legend`
average The mean (average) field value. Identical to mean. :ref:`gallery_layer_line_color_rule`
count The total count of data objects in the group. :ref:`gallery_simple_heatmap`
distinct The count of distinct field values. N/A

0 comments on commit 18d8094

Please sign in to comment.