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

docs: Adds example Calculate Residuals #3625

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 35 additions & 0 deletions tests/examples_arguments_syntax/calculate_residuals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Calculate Residuals
-------------------
A dot plot showing each movie in the database, and the difference from the average movie rating.
The display is sorted by year to visualize everything in sequential order.
The graph is for all Movies before 2019.

Adapted from `Calculate Residuals <https://vega.github.io/vega-lite/examples/joinaggregate_residual_graph.html>`_.
"""
# category: advanced calculations

import altair as alt

imdb_rating = alt.datum["IMDB Rating"]

chart = (
alt.Chart("https://vega.github.io/vega-lite/data/movies.json")
.mark_point()
.transform_filter(imdb_rating != None)
.transform_filter(
alt.FieldRangePredicate("Release Date", [None, 2019], timeUnit="year")
)
.transform_joinaggregate(AverageRating="mean(IMDB Rating)")
.transform_calculate(RatingDelta=imdb_rating - alt.datum.AverageRating)
.encode(
x="Release Date:T",
y=alt.Y("RatingDelta:Q", title="Rating Delta"),
color=alt.Color(
"RatingDelta:Q",
title="Rating Delta",
scale=alt.Scale(domainMid=0),
),
)
)
chart
31 changes: 31 additions & 0 deletions tests/examples_methods_syntax/calculate_residuals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Calculate Residuals
-------------------
A dot plot showing each movie in the database, and the difference from the average movie rating.
The display is sorted by year to visualize everything in sequential order.
The graph is for all Movies before 2019.

Adapted from `Calculate Residuals <https://vega.github.io/vega-lite/examples/joinaggregate_residual_graph.html>`_.
"""
# category: advanced calculations

import altair as alt

imdb_rating = alt.datum["IMDB Rating"]

chart = (
alt.Chart("https://vega.github.io/vega-lite/data/movies.json")
.mark_point()
.transform_filter(imdb_rating != None)
.transform_filter(
alt.FieldRangePredicate("Release Date", [None, 2019], timeUnit="year")
)
.transform_joinaggregate(AverageRating="mean(IMDB Rating)")
.transform_calculate(RatingDelta=imdb_rating - alt.datum.AverageRating)
.encode(
x="Release Date:T",
y=alt.Y("RatingDelta:Q").title("Rating Delta"),
color=alt.Color("RatingDelta:Q").title("Rating Delta").scale(domainMid=0),
)
)
chart