Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added argmax example #2653

Merged
merged 4 commits into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -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
Expand Down