{ "cells": [ { "cell_type": "markdown", "id": "title", "metadata": {}, "source": [ "# Strip Text - Dev Testing Notebook\n", "\n", "Development notebook for testing strip text options, including `strip_text_x`, `strip_text_y`, `angle`, `size`, `hjust`, `vjust`, margins, and facet variants." ] }, { "cell_type": "code", "execution_count": 1, "id": "setup", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "from lets_plot import *\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "id": "data", "metadata": {}, "outputs": [], "source": [ "# Base dataset: two factors with three levels each.\n", "data = {\n", " 'x': ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C',\n", " 'A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C'],\n", " 'row': ['North', 'North', 'North', 'South', 'South', 'South', 'East', 'East', 'East',\n", " 'North', 'North', 'North', 'South', 'South', 'South', 'East', 'East', 'East'],\n", " 'col': ['Alpha', 'Alpha', 'Alpha', 'Alpha', 'Alpha', 'Alpha', 'Alpha', 'Alpha', 'Alpha',\n", " 'Beta', 'Beta', 'Beta', 'Beta', 'Beta', 'Beta', 'Beta', 'Beta', 'Beta'],\n", " 'val': [12, 15, 9, 11, 7, 14, 6, 18, 10, 13, 8, 16, 5, 19, 11, 9, 14, 12]\n", "}\n", "\n", "# Long-label dataset for testing layout under stress.\n", "data_long = {\n", " 'x': list(range(6)) * 4,\n", " 'y': [i * 1.2 + (j % 2) for j in range(4) for i in range(6)],\n", " 'row': ['Very Long Row Label One'] * 12 + ['Very Long Row Label Two'] * 12,\n", " 'col': (['Short Col A'] * 6 + ['Short Col B'] * 6) * 2,\n", "}\n", "\n", "base = ggplot(data, aes('x', 'val')) + geom_bar(stat='identity', fill='steelblue') + theme_bw()" ] }, { "cell_type": "markdown", "id": "sec-x-angles", "metadata": {}, "source": [ "## 1. `strip_text_x`: angle sweep (`facet_grid` columns)" ] }, { "cell_type": "code", "execution_count": 3, "id": "x-angles", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "(!) WARN: class GGBunch is deprecated and will be removed in future releases.\n", "\n", " Please, use function ggbunch() to combine several plots into\n", " a single figure with custom layout.\n", "\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "angles = [0, 30, 45, 90, -45]\n", "plots = []\n", "for a in angles:\n", " p = base + \\\n", " facet_grid(x='col') + \\\n", " theme(strip_text_x=element_text(angle=a, size=12)) + \\\n", " ggtitle(f'strip_text_x angle={a}') + \\\n", " ggsize(420, 220)\n", " plots.append(p)\n", "bunch = GGBunch()\n", "for i, p in enumerate(plots):\n", " bunch.add_plot(p, (i % 3) * 440, (i // 3) * 240)\n", "bunch.show()" ] }, { "cell_type": "markdown", "id": "sec-y-angles", "metadata": {}, "source": [ "## 2. `strip_text_y`: angle sweep (`facet_grid` rows)" ] }, { "cell_type": "code", "execution_count": 4, "id": "y-angles", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "(!) WARN: class GGBunch is deprecated and will be removed in future releases.\n", "\n", " Please, use function ggbunch() to combine several plots into\n", " a single figure with custom layout.\n", "\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plots = []\n", "for a in angles:\n", " p = base + \\\n", " facet_grid(y='row') + \\\n", " theme(strip_text_y=element_text(angle=a, size=12)) + \\\n", " ggtitle(f'strip_text_y angle={a}') + \\\n", " ggsize(420, 280)\n", " plots.append(p)\n", "bunch = GGBunch()\n", "for i, p in enumerate(plots):\n", " bunch.add_plot(p, (i % 3) * 440, (i // 3) * 300)\n", "bunch.show()" ] }, { "cell_type": "markdown", "id": "sec-both", "metadata": {}, "source": [ "## 3. X and Y strips: independent angles (`facet_grid`)" ] }, { "cell_type": "code", "execution_count": 5, "id": "both-angles", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "(!) WARN: class GGBunch is deprecated and will be removed in future releases.\n", "\n", " Please, use function ggbunch() to combine several plots into\n", " a single figure with custom layout.\n", "\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "combos = [(0, 90), (45, 0), (45, 45), (90, 90)]\n", "plots = []\n", "for ax, ay in combos:\n", " p = base + \\\n", " facet_grid(x='col', y='row') + \\\n", " theme(\n", " strip_text_x=element_text(angle=ax, size=11),\n", " strip_text_y=element_text(angle=ay, size=11),\n", " ) + \\\n", " ggtitle(f'x_angle={ax}, y_angle={ay}') + \\\n", " ggsize(460, 340)\n", " plots.append(p)\n", "bunch = GGBunch()\n", "for i, p in enumerate(plots):\n", " bunch.add_plot(p, (i % 2) * 480, (i // 2) * 360)\n", "bunch.show()" ] }, { "cell_type": "markdown", "id": "sec-font-size", "metadata": {}, "source": [ "## 4. Font size effect at a fixed 45-degree angle" ] }, { "cell_type": "code", "execution_count": 6, "id": "font-size", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "(!) WARN: class GGBunch is deprecated and will be removed in future releases.\n", "\n", " Please, use function ggbunch() to combine several plots into\n", " a single figure with custom layout.\n", "\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sizes = [8, 12, 16, 22]\n", "plots = []\n", "for s in sizes:\n", " p = base + \\\n", " facet_grid(x='col', y='row') + \\\n", " theme(\n", " strip_text_x=element_text(angle=45, size=s),\n", " strip_text_y=element_text(angle=45, size=s),\n", " ) + \\\n", " ggtitle(f'size={s}, angle=45') + \\\n", " ggsize(420, 320)\n", " plots.append(p)\n", "bunch = GGBunch()\n", "for i, p in enumerate(plots):\n", " bunch.add_plot(p, (i % 2) * 440, (i // 2) * 340)\n", "bunch.show()" ] }, { "cell_type": "markdown", "id": "sec-hjust-vjust", "metadata": {}, "source": [ "## 5. `hjust` and `vjust` with `angle=45`" ] }, { "cell_type": "code", "execution_count": 7, "id": "hjust-vjust", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "(!) WARN: class GGBunch is deprecated and will be removed in future releases.\n", "\n", " Please, use function ggbunch() to combine several plots into\n", " a single figure with custom layout.\n", "\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "combos_jv = [(0, 0), (0.5, 0.5), (1, 1), (0, 1), (1, 0)]\n", "plots = []\n", "for h, v in combos_jv:\n", " p = base + \\\n", " facet_grid(x='col') + \\\n", " theme(\n", " strip_text_x=element_text(angle=45, size=13, hjust=h, vjust=v),\n", " strip_background_x=element_rect(fill='aliceblue', color='steelblue'),\n", " ) + \\\n", " ggtitle(f'hjust={h}, vjust={v}') + \\\n", " ggsize(420, 240)\n", " plots.append(p)\n", "bunch = GGBunch()\n", "for i, p in enumerate(plots):\n", " bunch.add_plot(p, (i % 3) * 440, (i // 3) * 260)\n", "bunch.show()" ] }, { "cell_type": "markdown", "id": "sec-facet-wrap", "metadata": {}, "source": [ "## 6. `facet_wrap`: `strip_text_x` angle sweep" ] }, { "cell_type": "code", "execution_count": 8, "id": "facet-wrap", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "(!) WARN: class GGBunch is deprecated and will be removed in future releases.\n", "\n", " Please, use function ggbunch() to combine several plots into\n", " a single figure with custom layout.\n", "\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data_wrap = {\n", " 'x': list(range(5)) * 6,\n", " 'y': [i + j * 0.7 for j in range(6) for i in range(5)],\n", " 'grp': (['Group One'] * 5 + ['Group Two'] * 5 + ['Group Three'] * 5) * 2,\n", "}\n", "\n", "plots = []\n", "for a in [0, 30, 45, 90]:\n", " p = ggplot(data_wrap, aes('x', 'y')) + \\\n", " geom_point(color='steelblue') + \\\n", " facet_wrap('grp', ncol=3) + \\\n", " theme_bw() + \\\n", " theme(strip_text_x=element_text(angle=a, size=12)) + \\\n", " ggtitle(f'facet_wrap angle={a}') + \\\n", " ggsize(480, 260)\n", " plots.append(p)\n", "bunch = GGBunch()\n", "for i, p in enumerate(plots):\n", " bunch.add_plot(p, (i % 2) * 500, (i // 2) * 280)\n", "bunch.show()" ] }, { "cell_type": "markdown", "id": "sec-long-labels", "metadata": {}, "source": [ "## 7. Long strip labels: layout stress test" ] }, { "cell_type": "code", "execution_count": 9, "id": "long-labels", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "base_long = ggplot(data_long, aes('x', 'y')) + geom_point(color='tomato') + theme_bw()\n", "\n", "for a in [0, 45, 90]:\n", " p = base_long + \\\n", " facet_grid(x='col', y='row') + \\\n", " theme(\n", " strip_text_x=element_text(angle=a, size=11),\n", " strip_text_y=element_text(angle=a, size=11),\n", " ) + \\\n", " ggtitle(f'Long labels, angle={a}') + \\\n", " ggsize(620, 380)\n", " p.show()" ] }, { "cell_type": "markdown", "id": "sec-full-theme", "metadata": {}, "source": [ "## 8. Styled strips: color, face, and background" ] }, { "cell_type": "code", "execution_count": 10, "id": "styled-strips", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(base +\n", " facet_grid(x='col', y='row') +\n", " theme(\n", " strip_text_x=element_text(angle=35, size=13, face='bold', color='darkslategray'),\n", " strip_text_y=element_text(angle=-35, size=13, face='italic', color='darkred'),\n", " strip_background_x=element_rect(fill='aliceblue', color='cadetblue'),\n", " strip_background_y=element_rect(fill='lemonchiffon', color='tan'),\n", " ) +\n", " ggtitle('Styled strips: x angle=35, y angle=-35') +\n", " ggsize(560, 420)\n", ")" ] }, { "cell_type": "markdown", "id": "sec-default", "metadata": {}, "source": [ "## 9. Default `angle=0` baseline for comparison" ] }, { "cell_type": "code", "execution_count": 11, "id": "default-baseline", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(base +\n", " facet_grid(x='col', y='row') +\n", " ggtitle('Default - no angle override') +\n", " ggsize(560, 400)\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.13.13" } }, "nbformat": 4, "nbformat_minor": 5 }