{ "cells": [ { "cell_type": "markdown", "id": "e0594942", "metadata": {}, "source": [ "# Quickstart\n", "\n", "`plotnine_extra` re-exports plotnine and adds geoms, stats, facets, guides,\n", "themes, plot constructors, composition helpers, and animation utilities. A\n", "single import is enough for examples that combine plotnine primitives with\n", "plotnine-extra layers.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "89889b4a", "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": "8e2909c1", "metadata": {}, "source": [ "## Start with plotnine syntax\n", "\n", "The package keeps the plotnine grammar. Extra layers and helpers can be mixed\n", "with `ggplot`, `aes`, geoms, scales, and themes.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "2fa8f12a", "metadata": {}, "outputs": [], "source": [ "penguin_data = penguins.dropna(subset=[\n", " \"bill_length_mm\",\n", " \"bill_depth_mm\",\n", " \"species\",\n", "]).copy()\n", "\n", "(\n", " ggplot(penguin_data, aes(\"bill_length_mm\", \"bill_depth_mm\", color=\"species\"))\n", " + geom_point(alpha=0.75)\n", " + geom_smooth(method=\"lm\", se=False)\n", " + scale_color_tableau(k=3)\n", " + theme_few()\n", " + labs(x=\"Bill length (mm)\", y=\"Bill depth (mm)\", color=\"Species\")\n", ")\n" ] }, { "cell_type": "markdown", "id": "aa420016", "metadata": {}, "source": [ "## Use constructors for common plots\n", "\n", "Constructor helpers return `ggplot` objects, so the result can be edited with\n", "standard plotnine layers.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "9554978e", "metadata": {}, "outputs": [], "source": [ "(\n", " ggboxplot(\n", " ToothGrowth.assign(dose=ToothGrowth[\"dose\"].astype(str)),\n", " x=\"dose\",\n", " y=\"len\",\n", " fill=\"dose\",\n", " add=\"quasirandom\",\n", " )\n", " + scale_fill_few(k=3)\n", " + theme_pubr()\n", " + labs(x=\"Dose\", y=\"Tooth length\", fill=\"Dose\")\n", ")\n" ] }, { "cell_type": "markdown", "id": "86603291", "metadata": {}, "source": [ "## Combine plots after they are defined\n", "\n", "Composition operators keep plots separate until layout time. This matters when\n", "plots use different scales or themes. `plot_layout()` and `plot_annotation()`\n", "control the arrangement, and are available on every supported plotnine —\n", "`plotnine-extra` requires plotnine 0.16.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "c1beafc4", "metadata": {}, "outputs": [], "source": [ "p1 = ggscatter(penguin_data, \"bill_length_mm\", \"bill_depth_mm\", color=\"species\")\n", "p2 = ggdensity(penguin_data, \"body_mass_g\", color=\"species\")\n", "\n", "combined = p1 | p2\n", "combined = combined + plot_layout(widths=[2, 1]) + plot_annotation(\n", " title=\"Penguin measurements\",\n", " caption=\"Palmer penguins data bundled with plotnine-extra\",\n", ")\n", "combined\n" ] }, { "cell_type": "markdown", "id": "0795ad24", "metadata": {}, "source": [ "## Choose the next vignette\n", "\n", "The remaining pages cover statistical annotations, geoms and positions, facets\n", "and guides, themes and palettes, constructors, and composition.\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 5 }