{ "cells": [ { "cell_type": "markdown", "id": "240bbb2d", "metadata": {}, "source": [ "# Composition and Export Workflows\n", "\n", "Use this page after the individual plots are defined. It covers plot layout and\n", "figure annotations first, then the related final-stage tasks of plot inspection\n", "and animation setup.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "dedd5f00", "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": "dbebbcc5", "metadata": {}, "source": [ "## Compose plots with operators\n", "\n", "`|` places plots beside each other and `/` stacks them. `plot_layout()`\n", "sets their relative sizes.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "0b9c2916", "metadata": {}, "outputs": [], "source": [ "base = penguin_data.dropna(subset=[\"body_mass_g\"]).copy()\n", "left = (\n", " ggplot(base, aes(\"bill_length_mm\", \"bill_depth_mm\", color=\"species\"))\n", " + geom_point(alpha=0.65)\n", " + scale_color_tableau(k=3)\n", " + theme_few()\n", ")\n", "right = (\n", " ggplot(base, aes(\"body_mass_g\", fill=\"species\"))\n", " + geom_density(alpha=0.35)\n", " + scale_fill_tableau(k=3)\n", " + theme_few()\n", ")\n", "\n", "composition = left | right\n", "composition = composition + plot_layout(widths=[1.6, 1])\n", "composition\n" ] }, { "cell_type": "markdown", "id": "25a5025e", "metadata": {}, "source": [ "## Add figure-level annotations\n", "\n", "`plot_annotation()` annotates operator compositions, and\n", "`ggarrange()` / `annotate_figure()` provide ggpubr-style wrappers for\n", "arranging and annotating composed plots. All of it is available on every\n", "supported plotnine: `plotnine-extra` requires plotnine 0.16, so there is no\n", "version gating to work around.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "34e71417", "metadata": {}, "outputs": [], "source": [ "annotated = composition + plot_annotation(\n", " title=\"Penguin morphology\",\n", " caption=\"Two finished plots arranged with plotnine-extra\",\n", ")\n", "annotated\n" ] }, { "cell_type": "code", "execution_count": null, "id": "0e438ed1", "metadata": {}, "outputs": [], "source": [ "arranged = ggarrange(left, right, ncol=2)\n", "arranged = annotate_figure(\n", " arranged, top=\"Penguin morphology\", bottom=\"Palmer penguins data\"\n", ")\n", "arranged\n" ] }, { "cell_type": "markdown", "id": "d8b28ead", "metadata": {}, "source": [ "## Query rendered plots\n", "\n", "`get_breaks()` and `get_legend()` inspect a built plot. `get_legend()` raises\n", "an error when no legend is drawn.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "eee88c15", "metadata": {}, "outputs": [], "source": [ "axis_plot = (\n", " ggplot(pd.DataFrame({\"x\": [1, 2, 3], \"y\": [2, 3, 5]}), aes(\"x\", \"y\"))\n", " + geom_point()\n", " + scale_x_continuous(breaks=[1, 2, 3])\n", ")\n", "get_breaks(axis_plot, \"x\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "7af17bdf", "metadata": {}, "outputs": [], "source": [ "legend_plot = left + guide_stringlegend(title=\"Species\")\n", "legend = get_legend(legend_plot)\n", "type(legend).__name__\n" ] }, { "cell_type": "markdown", "id": "df708222", "metadata": {}, "source": [ "## Animate plot sequences\n", "\n", "`PlotnineAnimation` takes a sequence of ggplot objects. Save the animation when\n", "the runtime has an animation writer such as Pillow or ImageMagick.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "4a96b781", "metadata": {}, "outputs": [], "source": [ "frames = []\n", "for year in [1950, 1951, 1952]:\n", " frame_data = flights[flights[\"year\"] == year].copy()\n", " frame_data[\"month_index\"] = range(1, len(frame_data) + 1)\n", " frames.append(\n", " ggplot(frame_data, aes(\"month_index\", \"passengers\"))\n", " + geom_line(size=1)\n", " + geom_point()\n", " + theme_clean()\n", " + labs(title=f\"Air passengers in {year}\", x=\"Month\", y=\"Passengers\")\n", " )\n", "\n", "animation = PlotnineAnimation(frames, interval=300)\n", "type(animation).__name__\n" ] }, { "cell_type": "markdown", "id": "01141e17", "metadata": {}, "source": [ "For static export, use plotnine's normal save method on an individual plot or\n", "on the composed figure.\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 5 }