From 13d2cc65e3628939e69aa505fcd801a49a1259ce Mon Sep 17 00:00:00 2001 From: Mattijn van Hoek Date: Mon, 27 Nov 2023 22:59:30 +0100 Subject: [PATCH] doc: add example, interval selection on a map (#3275) * add example * single ticks to quotes * move geopandas from doc to dev inc mypy settings --- pyproject.toml | 5 +- .../interval_selection_map_quakes.py | 62 +++++++++++++++++++ .../interval_selection_map_quakes.py | 62 +++++++++++++++++++ 3 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 tests/examples_arguments_syntax/interval_selection_map_quakes.py create mode 100644 tests/examples_methods_syntax/interval_selection_map_quakes.py diff --git a/pyproject.toml b/pyproject.toml index 343e9f1e9..0b7c46de2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -72,7 +72,8 @@ dev = [ "types-setuptools", "pyarrow>=11", "vegafusion[embed]>=1.4.0", - "anywidget" + "anywidget", + "geopandas", ] doc = [ "sphinx", @@ -82,7 +83,6 @@ doc = [ "numpydoc", "pillow>=9,<10", "pydata-sphinx-theme>=0.14.1", - "geopandas", "myst-parser", "sphinx_copybutton", "sphinx-design", @@ -216,6 +216,7 @@ module = [ "yaml.*", "vl_convert.*", "pandas.lib.*", + "geopandas.*", "nbformat.*", "ipykernel.*", "m2r.*", diff --git a/tests/examples_arguments_syntax/interval_selection_map_quakes.py b/tests/examples_arguments_syntax/interval_selection_map_quakes.py new file mode 100644 index 000000000..6ab6bed8d --- /dev/null +++ b/tests/examples_arguments_syntax/interval_selection_map_quakes.py @@ -0,0 +1,62 @@ +""" +Interval Selection on a Map +=========================== + +This is an example of a binned bar chart on the right where the filtered overlay +is adjusted by interacting with the map on the left. +""" +# category: interactive charts +import altair as alt +from vega_datasets import data +import geopandas as gpd + +# load data +gdf_quakies = gpd.read_file(data.earthquakes.url, driver="GeoJSON") +gdf_world = gpd.read_file(data.world_110m.url, driver="TopoJSON") + +# defintion for interactive brush +brush = alt.selection_interval( + encodings=["longitude"], + empty=False, + value={"longitude": [-50, -110]} +) + +# world disk +sphere = alt.Chart(alt.sphere()).mark_geoshape( + fill="transparent", stroke="lightgray", strokeWidth=1 +) + +# countries as shapes +world = alt.Chart(gdf_world).mark_geoshape( + fill="lightgray", stroke="white", strokeWidth=0.1 +) + +# earthquakes as dots on map +quakes = alt.Chart(gdf_quakies).transform_calculate( + lon="datum.geometry.coordinates[0]", + lat="datum.geometry.coordinates[1]", +).mark_circle(opacity=0.35, tooltip=True).encode( + longitude="lon:Q", + latitude="lat:Q", + color=alt.condition(brush, alt.value("goldenrod"), alt.value("steelblue")), + size=alt.Size("mag:Q", scale=alt.Scale(type="pow", range=[1, 1000], domain=[0, 7], exponent=4)), +).add_params(brush) + +# combine layers for the map +left_map = alt.layer(sphere, world, quakes).project(type="mercator") + +# histogram of binned earthquakes +bars = alt.Chart(gdf_quakies).mark_bar().encode( + x=alt.X("mag:Q").bin(extent=[0,7]), + y="count(mag):Q", + color=alt.value("steelblue") +) + +# filtered earthquakes +bars_overlay = bars.encode(color=alt.value("goldenrod")).transform_filter(brush) + +# combine layers for histogram +right_bars = alt.layer(bars, bars_overlay) + +# vertical concatenate map and bars +left_map | right_bars \ No newline at end of file diff --git a/tests/examples_methods_syntax/interval_selection_map_quakes.py b/tests/examples_methods_syntax/interval_selection_map_quakes.py new file mode 100644 index 000000000..b45afe8da --- /dev/null +++ b/tests/examples_methods_syntax/interval_selection_map_quakes.py @@ -0,0 +1,62 @@ +""" +Interval Selection on a Map +=========================== + +This is an example of a binned bar chart on the right where the filtered overlay +is adjusted by interacting with the map on the left. +""" +# category: interactive charts +import altair as alt +from vega_datasets import data +import geopandas as gpd + +# load data +gdf_quakies = gpd.read_file(data.earthquakes.url, driver="GeoJSON") +gdf_world = gpd.read_file(data.world_110m.url, driver="TopoJSON") + +# defintion for interactive brush +brush = alt.selection_interval( + encodings=["longitude"], + empty=False, + value={"longitude": [-50, -110]} +) + +# world disk +sphere = alt.Chart(alt.sphere()).mark_geoshape( + fill="transparent", stroke="lightgray", strokeWidth=1 +) + +# countries as shapes +world = alt.Chart(gdf_world).mark_geoshape( + fill="lightgray", stroke="white", strokeWidth=0.1 +) + +# earthquakes as dots on map +quakes = alt.Chart(gdf_quakies).transform_calculate( + lon="datum.geometry.coordinates[0]", + lat="datum.geometry.coordinates[1]", +).mark_circle(opacity=0.35, tooltip=True).encode( + longitude="lon:Q", + latitude="lat:Q", + color=alt.condition(brush, alt.value("goldenrod"), alt.value("steelblue")), + size=alt.Size("mag:Q").scale(type="pow", range=[1, 1000], domain=[0, 7], exponent=4), +).add_params(brush) + +# combine layers for the map +left_map = alt.layer(sphere, world, quakes).project(type="mercator") + +# histogram of binned earthquakes +bars = alt.Chart(gdf_quakies).mark_bar().encode( + x=alt.X("mag:Q").bin(extent=[0,7]), + y="count(mag):Q", + color=alt.value("steelblue") +) + +# filtered earthquakes +bars_overlay = bars.encode(color=alt.value("goldenrod")).transform_filter(brush) + +# combine layers for histogram +right_bars = alt.layer(bars, bars_overlay) + +# vertical concatenate map and bars +left_map | right_bars \ No newline at end of file