From 2d07630248ab1e41e951f989a469ce56f1d7dbf0 Mon Sep 17 00:00:00 2001 From: dangotbanned <125183946+dangotbanned@users.noreply.github.com> Date: Wed, 2 Oct 2024 22:31:32 +0100 Subject: [PATCH 1/2] docs: Adds example Calculate Residuals Recreation of `vega-lite` example https://vega.github.io/vega-lite/examples/joinaggregate_residual_graph.html --- .../calculate_residuals.py | 35 +++++++++++++++++++ .../calculate_residuals.py | 31 ++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/examples_arguments_syntax/calculate_residuals.py create mode 100644 tests/examples_methods_syntax/calculate_residuals.py diff --git a/tests/examples_arguments_syntax/calculate_residuals.py b/tests/examples_arguments_syntax/calculate_residuals.py new file mode 100644 index 000000000..992d3ee10 --- /dev/null +++ b/tests/examples_arguments_syntax/calculate_residuals.py @@ -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 `_. +""" +# 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 \ No newline at end of file diff --git a/tests/examples_methods_syntax/calculate_residuals.py b/tests/examples_methods_syntax/calculate_residuals.py new file mode 100644 index 000000000..5d1628663 --- /dev/null +++ b/tests/examples_methods_syntax/calculate_residuals.py @@ -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 `_. +""" +# 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 \ No newline at end of file From 5052f4c146e985483e932c7d98d3c6f81b26c49d Mon Sep 17 00:00:00 2001 From: dangotbanned <125183946+dangotbanned@users.noreply.github.com> Date: Thu, 3 Oct 2024 14:56:36 +0100 Subject: [PATCH 2/2] docs: Use `vega_datasets` instead of url https://github.com/vega/altair/pull/3625#issuecomment-2389762200 --- .../calculate_residuals.py | 18 ++++++++++-------- .../calculate_residuals.py | 18 ++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/tests/examples_arguments_syntax/calculate_residuals.py b/tests/examples_arguments_syntax/calculate_residuals.py index 992d3ee10..b8e8fe227 100644 --- a/tests/examples_arguments_syntax/calculate_residuals.py +++ b/tests/examples_arguments_syntax/calculate_residuals.py @@ -10,23 +10,25 @@ # category: advanced calculations import altair as alt +from vega_datasets import data -imdb_rating = alt.datum["IMDB Rating"] +imdb_rating = alt.datum["IMDB_Rating"] +source = data.movies.url chart = ( - alt.Chart("https://vega.github.io/vega-lite/data/movies.json") + alt.Chart(source) .mark_point() .transform_filter(imdb_rating != None) .transform_filter( - alt.FieldRangePredicate("Release Date", [None, 2019], timeUnit="year") + alt.FieldRangePredicate("Release_Date", [None, 2019], timeUnit="year") ) - .transform_joinaggregate(AverageRating="mean(IMDB Rating)") - .transform_calculate(RatingDelta=imdb_rating - alt.datum.AverageRating) + .transform_joinaggregate(Average_Rating="mean(IMDB_Rating)") + .transform_calculate(Rating_Delta=imdb_rating - alt.datum.Average_Rating) .encode( - x="Release Date:T", - y=alt.Y("RatingDelta:Q", title="Rating Delta"), + x=alt.X("Release_Date:T", title="Release Date"), + y=alt.Y("Rating_Delta:Q", title="Rating Delta"), color=alt.Color( - "RatingDelta:Q", + "Rating_Delta:Q", title="Rating Delta", scale=alt.Scale(domainMid=0), ), diff --git a/tests/examples_methods_syntax/calculate_residuals.py b/tests/examples_methods_syntax/calculate_residuals.py index 5d1628663..bb53fb788 100644 --- a/tests/examples_methods_syntax/calculate_residuals.py +++ b/tests/examples_methods_syntax/calculate_residuals.py @@ -10,22 +10,24 @@ # category: advanced calculations import altair as alt +from vega_datasets import data -imdb_rating = alt.datum["IMDB Rating"] +imdb_rating = alt.datum["IMDB_Rating"] +source = data.movies.url chart = ( - alt.Chart("https://vega.github.io/vega-lite/data/movies.json") + alt.Chart(source) .mark_point() .transform_filter(imdb_rating != None) .transform_filter( - alt.FieldRangePredicate("Release Date", [None, 2019], timeUnit="year") + alt.FieldRangePredicate("Release_Date", [None, 2019], timeUnit="year") ) - .transform_joinaggregate(AverageRating="mean(IMDB Rating)") - .transform_calculate(RatingDelta=imdb_rating - alt.datum.AverageRating) + .transform_joinaggregate(Average_Rating="mean(IMDB_Rating)") + .transform_calculate(Rating_Delta=imdb_rating - alt.datum.Average_Rating) .encode( - x="Release Date:T", - y=alt.Y("RatingDelta:Q").title("Rating Delta"), - color=alt.Color("RatingDelta:Q").title("Rating Delta").scale(domainMid=0), + x=alt.X("Release_Date:T").title("Release Date"), + y=alt.Y("Rating_Delta:Q").title("Rating Delta"), + color=alt.Color("Rating_Delta:Q").title("Rating Delta").scale(domainMid=0), ) ) chart \ No newline at end of file