{ "cells": [ { "cell_type": "markdown", "id": "8787ef20", "metadata": {}, "source": [ "# Facets, Guides, and Strips\n", "\n", "This page covers the ggh4x-style facet tools exposed through plotnine extension\n", "points: rendered support for inner axes, per-panel scales, manual panel\n", "designs, and axis-guide descriptors, plus strip descriptor classes where direct\n", "drawing depends on plotnine hooks.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "873e1c14", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "from plotnine_extra import *\n", "from plotnine_extra.data import ToothGrowth, flights, iris, penguins\n", "\n", "tooth = ToothGrowth.assign(dose=ToothGrowth[\"dose\"].astype(str))\n", "penguin_data = penguins.dropna(subset=[\n", " \"bill_length_mm\",\n", " \"bill_depth_mm\",\n", " \"body_mass_g\",\n", " \"species\",\n", "]).copy()\n" ] }, { "cell_type": "markdown", "id": "332f683f", "metadata": {}, "source": [ "## Inner axes and per-panel scales\n", "\n", "Use `facet_wrap2()` or `facet_grid2()` when a plot needs inner axes or\n", "per-panel position scales. Selector expressions in `scale_x_facet()` and\n", "`scale_y_facet()` are evaluated against facet-layout rows.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "8b6c2cba", "metadata": {}, "outputs": [], "source": [ "facet_data = pd.DataFrame({\n", " \"x\": list(range(6)) + list(range(8, 14)),\n", " \"y\": [1, 2, 1.5, 3, 2.7, 3.2, 2, 3, 3.5, 4, 4.5, 5],\n", " \"panel\": [\"baseline\"] * 6 + [\"zoom\"] * 6,\n", " \"group\": [\"A\", \"A\", \"B\", \"B\", \"A\", \"B\"] * 2,\n", "})\n", "\n", "(\n", " ggplot(facet_data, aes(\"x\", \"y\", color=\"group\"))\n", " + geom_point(size=2)\n", " + facet_wrap2(\"panel\", scales=\"free_x\", axes=\"all\")\n", " + scale_x_continuous(\n", " guide=guide_axis_manual(\n", " breaks=[0, 5, 10],\n", " labels=[\"low\", \"mid\", \"high\"],\n", " label_colour=[\"#1b9e77\", \"#7570b3\", \"#d95f02\"],\n", " )\n", " )\n", " + scale_x_facet('panel == \"zoom\"', limits=(7, 14), breaks=[8, 10, 12])\n", " + guide_stringlegend(title=\"Group\")\n", " + theme_clean()\n", ")\n" ] }, { "cell_type": "markdown", "id": "a11de7af", "metadata": {}, "source": [ "## Manual panel designs\n", "\n", "`facet_manual()` maps panels to a text design. Repeated labels span cells, and\n", "`#` marks an empty cell.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "6c52931d", "metadata": {}, "outputs": [], "source": [ "manual_data = pd.DataFrame({\n", " \"x\": np.tile([1, 2, 3, 4], 3),\n", " \"y\": [1, 2, 2.2, 3, 2, 2.5, 3.2, 3.8, 1.2, 1.4, 1.8, 2.6],\n", " \"panel\": np.repeat([\"wide\", \"left\", \"right\"], 4),\n", "})\n", "\n", "(\n", " ggplot(manual_data, aes(\"x\", \"y\"))\n", " + geom_point(size=2)\n", " + geom_line()\n", " + facet_manual(\"panel\", design=\"\"\"AA\n", "BC\"\"\", axes=\"all\")\n", " + theme_pubclean()\n", ")\n" ] }, { "cell_type": "markdown", "id": "ba70beaa", "metadata": {}, "source": [ "## Nested labels and strip descriptors\n", "\n", "`guide_axis_nested()` splits axis labels by a delimiter. Nested strip rendering\n", "is available through `facet_nested()` and `facet_nested_wrap()`. The exported\n", "strip descriptor classes store ggh4x-style options; direct strip drawing is\n", "limited by the strip hooks available in the selected plotnine facet.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "93a5c9da", "metadata": {}, "outputs": [], "source": [ "nested_data = pd.DataFrame({\n", " \"x\": [\"A_low\", \"A_high\", \"B_low\", \"B_high\"],\n", " \"y\": [1.2, 2.4, 1.7, 3.1],\n", " \"family\": [\"A\", \"A\", \"B\", \"B\"],\n", " \"panel\": [\"one\", \"one\", \"two\", \"two\"],\n", "})\n", "\n", "(\n", " ggplot(nested_data, aes(\"x\", \"y\"))\n", " + geom_col(fill=\"#4E79A7\")\n", " + facet_nested_wrap([\"family\", \"panel\"], ncol=2, nest_line=True)\n", " + guide_axis_nested(delim=\"_\")\n", " + theme_few()\n", " + labs(x=None, y=\"Value\")\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "426f9354", "metadata": {}, "outputs": [], "source": [ "[\n", " strip_vanilla(clip=\"off\").clip,\n", " strip_nested(nest_line=True).nest_line,\n", " strip_themed(text_x=[\"theme override\"]).text_x,\n", " strip_tag(prefix=\"(\", suffix=\")\").prefix,\n", "]\n" ] }, { "cell_type": "markdown", "id": "4150b152", "metadata": {}, "source": [ "## Panel-specific layers\n", "\n", "`at_panel()` and `ggsubset()` restrict a layer to panels that match a layout\n", "selector.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "803cd6ff", "metadata": {}, "outputs": [], "source": [ "(\n", " ggplot(facet_data, aes(\"x\", \"y\"))\n", " + geom_point(color=\"#b3b3b3\")\n", " + at_panel(geom_point(color=\"#d95f02\", size=3), 'panel == \"zoom\"')\n", " + facet_wrap2(\"panel\", scales=\"free_x\")\n", " + force_panelsizes(cols=[1.2, 1])\n", " + theme_clean()\n", ")\n" ] }, { "cell_type": "markdown", "id": "e368f362", "metadata": {}, "source": [ "## Dendrogram scales\n", "\n", "Dendrogram position scales set a categorical order and attach a dendrogram axis\n", "guide. The guide is drawn only when an extended facet renderer can consume it.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "7d373b0a", "metadata": {}, "outputs": [], "source": [ "dendro_data = pd.DataFrame({\n", " \"sample\": [\"C\", \"A\", \"B\"],\n", " \"value\": [3.0, 1.5, 2.4],\n", "})\n", "\n", "(\n", " ggplot(dendro_data, aes(\"sample\", \"value\"))\n", " + geom_col(fill=\"#59A14F\")\n", " + facet_wrap2(\"sample\", scales=\"free_x\")\n", " + scale_x_dendrogram(hclust={\"ivl\": [\"C\", \"A\", \"B\"]})\n", " + theme_tufte()\n", ")\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 5 }