-
Notifications
You must be signed in to change notification settings - Fork 794
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
b7cf627
commit 18d8094
Showing
2 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters