From 02e8c3479db0bc4b5adc52bb3453eb5d5b46f06a Mon Sep 17 00:00:00 2001 From: Dan Redding <125183946+dangotbanned@users.noreply.github.com> Date: Wed, 25 Sep 2024 11:51:19 +0100 Subject: [PATCH 1/4] fix(typing): Add missing `float` to `IntoExpression` alias (#3611) --- altair/expr/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/altair/expr/core.py b/altair/expr/core.py index e3bc65a52..e7872ada6 100644 --- a/altair/expr/core.py +++ b/altair/expr/core.py @@ -237,4 +237,4 @@ def __repr__(self) -> str: return f"{self.group}[{self.name!r}]" -IntoExpression: TypeAlias = Union[bool, None, str, OperatorMixin, Dict[str, Any]] +IntoExpression: TypeAlias = Union[bool, None, str, float, OperatorMixin, Dict[str, Any]] From e466f024bf2e73208deabffc66a71abe6b955591 Mon Sep 17 00:00:00 2001 From: Dan Redding <125183946+dangotbanned@users.noreply.github.com> Date: Thu, 26 Sep 2024 13:51:18 +0100 Subject: [PATCH 2/4] fix(ruff): Resolve `SIM910` lint in `display.py` (#3613) --- altair/vegalite/v5/display.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/altair/vegalite/v5/display.py b/altair/vegalite/v5/display.py index 8e6cb39bf..5d71a2934 100644 --- a/altair/vegalite/v5/display.py +++ b/altair/vegalite/v5/display.py @@ -100,7 +100,7 @@ def jupyter_renderer(spec: dict, **metadata): JupyterChart.enable_offline(offline=offline) # type: ignore[attr-defined] # propagate embed options - embed_options = metadata.get("embed_options", None) + embed_options = metadata.get("embed_options") # Need to ignore attr-defined mypy rule because mypy doesn't see _repr_mimebundle_ # conditionally defined in AnyWidget From 38ce5b10b06d5ca7e53e3a795bf234638444a0a1 Mon Sep 17 00:00:00 2001 From: Mattijn van Hoek Date: Thu, 26 Sep 2024 20:22:42 +0200 Subject: [PATCH 3/4] example of overlapping bars (#3612) --- .../grouped_bar_chart_overlapping_bars.py | 27 +++++++++++++++++++ .../grouped_bar_chart_overlapping_bars.py | 27 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 tests/examples_arguments_syntax/grouped_bar_chart_overlapping_bars.py create mode 100644 tests/examples_methods_syntax/grouped_bar_chart_overlapping_bars.py diff --git a/tests/examples_arguments_syntax/grouped_bar_chart_overlapping_bars.py b/tests/examples_arguments_syntax/grouped_bar_chart_overlapping_bars.py new file mode 100644 index 000000000..06e4e3d65 --- /dev/null +++ b/tests/examples_arguments_syntax/grouped_bar_chart_overlapping_bars.py @@ -0,0 +1,27 @@ +""" +Grouped Bar Chart with xOffset and overlapping bars +--------------------------------------------------- +Like :ref:`gallery_grouped_bar_chart2`, this example shows a grouped bar chart using the ``xOffset`` encoding channel, but in this example the bars are partly overlapping within each group. +""" +# category: bar charts +import altair as alt +import pandas as pd + +source = pd.DataFrame( + { + "category": list("AABBCC"), + "group": list("xyxyxy"), + "value": [0.1, 0.6, 0.7, 0.2, 0.6, 0.1], + } +) + +base = alt.Chart(source, width=alt.Step(12)).encode( + x="category:N", + y="value:Q", + xOffset=alt.XOffset("group:N", scale=alt.Scale(paddingOuter=0.5)), +) + +alt.layer( + base.mark_bar(size=20, stroke="white", fillOpacity=0.9).encode(fill="group:N"), + base.mark_text(dy=-5).encode(text="value:Q"), +) \ No newline at end of file diff --git a/tests/examples_methods_syntax/grouped_bar_chart_overlapping_bars.py b/tests/examples_methods_syntax/grouped_bar_chart_overlapping_bars.py new file mode 100644 index 000000000..5f81ac104 --- /dev/null +++ b/tests/examples_methods_syntax/grouped_bar_chart_overlapping_bars.py @@ -0,0 +1,27 @@ +""" +Grouped Bar Chart with xOffset and overlapping bars +--------------------------------------------------- +Like :ref:`gallery_grouped_bar_chart2`, this example shows a grouped bar chart using the ``xOffset`` encoding channel, but in this example the bars are partly overlapping within each group. +""" +# category: bar charts +import altair as alt +import pandas as pd + +source = pd.DataFrame( + { + "category": list("AABBCC"), + "group": list("xyxyxy"), + "value": [0.1, 0.6, 0.7, 0.2, 0.6, 0.1], + } +) + +base = alt.Chart(source, width=alt.Step(12)).encode( + x="category:N", + y="value:Q", + xOffset=alt.XOffset("group:N").scale(paddingOuter=0.5), +) + +alt.layer( + base.mark_bar(size=20, stroke="white", fillOpacity=0.9).encode(fill="group:N"), + base.mark_text(dy=-5).encode(text="value:Q"), +) \ No newline at end of file From cabf1e6428bab107a26adc6c51993d3b2c5bf6f0 Mon Sep 17 00:00:00 2001 From: Mattijn van Hoek Date: Fri, 27 Sep 2024 17:13:21 +0200 Subject: [PATCH 4/4] bar chart with labels colored by luminance (#3614) --- ...ar_chart_with_labels_measured_luminance.py | 30 +++++++++++++++++++ ...ar_chart_with_labels_measured_luminance.py | 30 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 tests/examples_arguments_syntax/bar_chart_with_labels_measured_luminance.py create mode 100644 tests/examples_methods_syntax/bar_chart_with_labels_measured_luminance.py diff --git a/tests/examples_arguments_syntax/bar_chart_with_labels_measured_luminance.py b/tests/examples_arguments_syntax/bar_chart_with_labels_measured_luminance.py new file mode 100644 index 000000000..bd08223c0 --- /dev/null +++ b/tests/examples_arguments_syntax/bar_chart_with_labels_measured_luminance.py @@ -0,0 +1,30 @@ +""" +Bar Chart with Labels based on Measured Luminance +================================================= +This example shows a basic horizontal bar chart with labels where the measured luminance to decides if the text overlay is be colored ``black`` or ``white``. +""" +# category: bar charts +import altair as alt +from vega_datasets import data + +source = data.barley() + +base = alt.Chart(source).encode( + x=alt.X('sum(yield):Q', stack='zero'), + y=alt.Y('site:O', sort='-x'), + text=alt.Text('sum(yield):Q', format='.0f') +) + +bars = base.mark_bar( + tooltip=alt.expr("luminance(scale('color', datum.sum_yield))") +).encode( + color='sum(yield):Q' +) + +text = base.mark_text( + align='right', + dx=-3, + color=alt.expr("luminance(scale('color', datum.sum_yield)) > 0.5 ? 'black' : 'white'") +) + +bars + text diff --git a/tests/examples_methods_syntax/bar_chart_with_labels_measured_luminance.py b/tests/examples_methods_syntax/bar_chart_with_labels_measured_luminance.py new file mode 100644 index 000000000..3517986ca --- /dev/null +++ b/tests/examples_methods_syntax/bar_chart_with_labels_measured_luminance.py @@ -0,0 +1,30 @@ +""" +Bar Chart with Labels based on Measured Luminance +================================================= +This example shows a basic horizontal bar chart with labels where the measured luminance to decides if the text overlay is be colored ``black`` or ``white``. +""" +# category: bar charts +import altair as alt +from vega_datasets import data + +source = data.barley() + +base = alt.Chart(source).encode( + x=alt.X('sum(yield):Q').stack('zero'), + y=alt.Y('site:O').sort('-x'), + text=alt.Text('sum(yield):Q', format='.0f') +) + +bars = base.mark_bar( + tooltip=alt.expr("luminance(scale('color', datum.sum_yield))") +).encode( + color='sum(yield):Q' +) + +text = base.mark_text( + align='right', + dx=-3, + color=alt.expr("luminance(scale('color', datum.sum_yield)) > 0.5 ? 'black' : 'white'") +) + +bars + text