{ "cells": [ { "cell_type": "markdown", "id": "b0cb8f8e-adbd-4122-99ed-08ffcf793d12", "metadata": {}, "source": [ "# Combining Discrete and Continuous Layers\n", "\n", "This notebook demonstrates how to position continuous elements like background bands (`geom_band`) and annotations (`geom_text`) relative to discrete elements like bars.\n", "\n", "When positioning continuous elements, keep in mind numeric equivalents of the discrete positions: in this example `0.0` for 'Outback' through `7.0` for 'Pacer'." ] }, { "cell_type": "code", "execution_count": 1, "id": "2c9f4f70-9667-42ae-93a3-013dafab2ea9", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "010e706b-afcd-4e03-8c80-383747b69353", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "7ad25b3a-0b53-4e6c-8b2b-ce46169ec5bb", "metadata": {}, "outputs": [], "source": [ "np.random.seed(69)\n", "\n", "cars = pd.DataFrame({\n", " 'Models': ['Outback', 'Impresa', 'BRZ', 'Jetta', 'Passat', 'Matador', 'Rambler', 'Pacer'],\n", " 'Val': np.random.uniform(0,100, size=8),\n", "})\n", "\n", "# Data to use in `geom_band` and `geom_text` layers\n", "cars_band = pd.DataFrame({\n", " 'Brand': ['Subaru', 'Volkswagen', 'AMC'],\n", " 'pos_minx': [-0.5, 2.5, 4.5],\n", " 'pos_maxx':[2.5, 4.5, 7.5],\n", " 'M':['#41DC8E', '#E0FFFF','#90D5FF']\n", "})" ] }, { "cell_type": "code", "execution_count": 4, "id": "189eb2df-56d8-4cae-ab2f-19c2c60e7719", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(cars, aes(x='Models', weight='Val')) + \\\n", " geom_band(aes(xmin='pos_minx', xmax='pos_maxx', fill='Brand', color='Brand'),\n", " data=cars_band,\n", " tooltips='none',\n", " alpha=0.5) + \\\n", " geom_text(aes(x='pos_minx', label='Brand'), y=100,\n", " data=cars_band, \n", " size=8, fontface='bold', \n", " hjust='left', \n", " nudge_x=0.1) + \\\n", " geom_bar() + \\\n", " scale_fill_manual(values=cars_band['M']) + \\\n", " scale_color_manual(values=cars_band['M']) + \\\n", " theme(legend_position='none', axis_title_x='blank') + \\\n", " ggsize(700, 400)" ] } ], "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.9.23" } }, "nbformat": 4, "nbformat_minor": 5 }