{ "cells": [ { "cell_type": "markdown", "id": "f03a934b-ae4a-4a90-9f51-c3e31318ef0b", "metadata": {}, "source": [ "# `geom_band()`" ] }, { "cell_type": "code", "execution_count": 1, "id": "833d8096-b8e9-4629-a607-36ab8dd19858", "metadata": {}, "outputs": [], "source": [ "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "e384c6dc-9f36-4739-b8e7-074fa29e3759", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "id": "a4ff11ce-204c-4c6a-98aa-c3d3a036c375", "metadata": {}, "source": [ "## Default" ] }, { "cell_type": "code", "execution_count": 3, "id": "686d7988-172f-40e5-8d67-9aef835ae0ac", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_band(xmin=-1, xmax=1)" ] }, { "cell_type": "markdown", "id": "8a6c2c0f-ac51-443f-9296-65c9982fc225", "metadata": {}, "source": [ "## Both vertical and horizontal" ] }, { "cell_type": "code", "execution_count": 4, "id": "c9a87d76-36c8-4452-90c7-ba07cbaee0b7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + \\\n", " geom_band(aes(\n", " xmin=[-1.5, .5],\n", " xmax=[-.5, 1.5],\n", " )) + \\\n", " geom_band(aes(\n", " ymin=[-1.5, .5],\n", " ymax=[-.5, 1.5],\n", " ))" ] }, { "cell_type": "markdown", "id": "30ebbe1c-af00-4178-b70b-671670e5a8bb", "metadata": {}, "source": [ "## Additional parameters" ] }, { "cell_type": "code", "execution_count": 5, "id": "b3c48799-f6d9-44c0-8122-187deddecd45", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + \\\n", " geom_band(aes(\n", " xmin=[-1, 0],\n", " xmax=[0, 1],\n", " fill=[\"a\", \"b\"],\n", " ), size=3, alpha=.5, linetype='dashed', show_legend=False)" ] }, { "cell_type": "code", "execution_count": 6, "id": "5974b2e5-ef31-4c68-b8fd-22b07916827b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# stat\n", "\n", "data = {\n", " 'x': ['a', 'a', 'a', 'b', 'b', 'b'],\n", " 'y': [1, 2, 3, 4, 5, 6],\n", "}\n", "\n", "ggplot(data, aes('x', 'y')) + geom_band(aes(fill='x'), stat='boxplot')" ] }, { "cell_type": "code", "execution_count": 7, "id": "4ce86dbe-a0ce-4570-a94e-6ae4dc4664d8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# position\n", "\n", "ggplot() + geom_band(aes(xmin=[-1], xmax=[1]), position=position_nudge(x=1)) + xlim(-1, 3)" ] }, { "cell_type": "code", "execution_count": 8, "id": "6cc1bbc8-5beb-42c7-b81e-1e76f2ae3b8c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# sampling\n", "\n", "n = 20\n", "ggplot() + geom_band(aes(xmin=list(range(n)), xmax=list(range(1, 1+n)), fill=list(range(n))), size=0, sampling=sampling_random(n-1, seed=42))" ] }, { "cell_type": "code", "execution_count": 9, "id": "e367e11f-e931-4b97-a235-acc4d0ae91c0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + \\\n", " geom_band(aes(xmin=[-3, 1], xmax=[-1, 3], paint_a=['a', 'b'], paint_b=['A', 'B']), \\\n", " color_by='paint_a', fill_by='paint_b', size=1.5) + \\\n", " scale_brewer('paint_a', palette='Set2') + \\\n", " scale_brewer('paint_b', palette='Pastel2')" ] }, { "cell_type": "markdown", "id": "6472f358-6e3a-418f-84d3-b9d0531c1602", "metadata": {}, "source": [ "## Tooltips" ] }, { "cell_type": "code", "execution_count": 10, "id": "bec0ee9f-6a70-4976-bf50-d7507c689a45", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\n", " 'xstart': [ 0, 2, 3, 6, None],\n", " 'xend': [ 1, 4, 5, 7, None],\n", " 'y_from': [-1, -4, -5, None, -9],\n", " 'y_to': [ 0, -2, -3, None, -8],\n", " 'color': [\"black\", \"red\", \"blue\", \"green\", \"yellow\"],\n", "}\n", "\n", "p1 = ggplot(data, aes(xmin='xstart', xmax='xend'))\n", "p2 = ggplot(data, aes(ymin='y_from', ymax='y_to'))\n", "\n", "gggrid([\n", " p1 + geom_band(aes(fill='color'), alpha=.2) + scale_fill_identity(),\n", " p2 + geom_band(aes(fill='color'), alpha=.2) + scale_fill_identity(),\n", " p1 + geom_band(tooltips='none'),\n", " p1 + geom_band(tooltips=layer_tooltips().line(\"min|^xmin\").line(\"max|^xmax\").line(\"color|@color\")),\n", "], ncol=2)" ] }, { "cell_type": "markdown", "id": "3acb00b9-318c-4ef5-9139-35da16236602", "metadata": {}, "source": [ "## Different coordinate systems" ] }, { "cell_type": "code", "execution_count": 11, "id": "e50bb8f4-6dba-406f-92a5-9a30788dc21c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "limits = [-4.25, 4.25]\n", "\n", "p = ggplot({'xmin': [-4, -2.5, -1, 1.5, 3], \\\n", " 'xmax': [-3, -1.5, 1, 2.5, 4], \\\n", " 'ymin': [-4, -1, 3, None, None], \\\n", " 'ymax': [-3, 1, 4, None, None]}) + \\\n", " geom_band(aes(xmin='xmin', xmax='xmax'), alpha=.2, size=0, fill=\"red\") + \\\n", " geom_band(aes(ymin='ymin', ymax='ymax'), alpha=.2, size=0, fill=\"blue\")\n", "\n", "gggrid([\n", " p + ggtitle(\"Default coord\"),\n", " p + coord_flip(xlim=limits, ylim=limits) + ggtitle(\"coord_flip()\"),\n", " p + coord_fixed(xlim=limits, ylim=limits) + ggtitle(\"coord_fixed()\"),\n", " p + coord_polar(xlim=limits, ylim=limits) + ggtitle(\"coord_polar()\"),\n", "], ncol=2)" ] }, { "cell_type": "code", "execution_count": 12, "id": "db355aea-13f2-44ac-a242-4859d23d2807", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# See: https://gadflyjl.org/stable/gallery/geometries/#[Geom.band](@ref),-[Geom.hband](@ref),-[Geom.vband](@ref)\n", "\n", "import pandas as pd\n", "\n", "presidential_df = pd.read_csv(\"../../f-24e/data/presidential.csv\", parse_dates=[\"start\", \"end\"])\n", "economics_df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/economics.csv\", parse_dates=[\"date\"])\n", "\n", "ggplot() + \\\n", " geom_band(aes(xmin=\"start\", xmax=\"end\", fill=\"party\"), data=presidential_df, size=0, alpha=.4) + \\\n", " geom_line(aes(\"date\", \"unemploy\"), data=economics_df) + \\\n", " scale_y_continuous(trans='log10') + \\\n", " ggsize(1000, 200)" ] }, { "cell_type": "markdown", "id": "0cdd5a43-d7ba-47bd-bb4d-1d24f641ad0f", "metadata": {}, "source": [ "## Tests" ] }, { "cell_type": "markdown", "id": "c6ca8ed3-ea0a-4afd-bb59-301329235f65", "metadata": {}, "source": [ "### Boundary Value Analysis" ] }, { "cell_type": "code", "execution_count": 13, "id": "1cd04ae3-9a65-40ad-8969-8f593c45d36e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "class BVATest:\n", " def __init__(self, data, title, orientation='x', show=True):\n", " self.data = data\n", " self.title = title\n", " self.orientation = orientation\n", " self.show = show\n", "\n", " def to_plot(self):\n", " min_aes, max_aes = '{0}min'.format(self.orientation), '{0}max'.format(self.orientation)\n", " return ggplot(self.data) + geom_band(aes(**{min_aes: min_aes, max_aes: max_aes})) + \\\n", " ggtitle(self.title)\n", "\n", "show_all = True\n", "bva_tests = [\n", " BVATest(\n", " data=dict(\n", " xmin=[],\n", " xmax=[],\n", " ),\n", " title=\"Empty dataset\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " xmin=[-1],\n", " xmax=[1],\n", " ),\n", " title=\"One value\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " xmin=[0],\n", " xmax=[0],\n", " ),\n", " title=\"min=max\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " xmin=[1],\n", " xmax=[-1],\n", " ),\n", " title=\"min>max\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " xmin=[-1, 0],\n", " xmax=[0, 1],\n", " ),\n", " title=\"Joint bands\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " xmin=[-2, -1],\n", " xmax=[1, 2],\n", " ),\n", " title=\"Intersected bands\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " ymin=[-1],\n", " ymax=[1],\n", " ),\n", " orientation='y',\n", " title=\"y-oriented\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " xmin=[-1, float('inf'), float('-inf'), -2, -3],\n", " xmax=[1, 3, 2, float('-inf'), float('inf')],\n", " ),\n", " title=\"Inf value\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " xmin=[-1, None, -2],\n", " xmax=[1, 2, None],\n", " ),\n", " title=\"None value\",\n", " ),\n", " BVATest(\n", " data=dict(\n", " xmin=[-1, np.nan, -2],\n", " xmax=[1, 2, np.nan],\n", " ),\n", " title=\"np.nan value\",\n", " ),\n", "]\n", "\n", "gggrid([\n", " t.to_plot()\n", " for t in filter(lambda t: show_all or t.show, bva_tests)\n", "], ncol=3)" ] }, { "cell_type": "markdown", "id": "08118cba-1642-43f5-8f75-6bf11b8b9625", "metadata": {}, "source": [ "### Regression Testing" ] }, { "cell_type": "code", "execution_count": 14, "id": "284bbd6e-a863-499f-a743-57dad0a2e36a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Only zoom without clipping saves the band, if it's half-visible\n", "\n", "data = dict(xmin=[0], xmax=[2])\n", "lims = [-1, 1]\n", "\n", "gggrid([\n", " ggplot(data) + geom_band(aes(xmin='xmin', xmax='xmax')) + xlim(*lims),\n", " ggplot(data) + geom_band(aes(xmin='xmin', xmax='xmax')) + coord_cartesian(xlim=lims),\n", "])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.14" } }, "nbformat": 4, "nbformat_minor": 5 }