{ "cells": [ { "cell_type": "markdown", "id": "ac7098ad", "metadata": {}, "source": [ "# Themes, Palettes, and Scales\n", "\n", "The styling helpers provide ggpubr-, ggsci-, and ggthemes-inspired themes,\n", "palettes, and scales as ordinary plotnine components.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "f5914364", "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": "7d2185cb", "metadata": {}, "source": [ "## ggthemes-style palettes\n", "\n", "`ggthemes_palette()` returns deterministic hex colors. The named scales use the\n", "same palette definitions for color and fill aesthetics.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "7068bc34", "metadata": {}, "outputs": [], "source": [ "{\n", " \"colorblind\": ggthemes_palette(\"colorblind\", n=4),\n", " \"tableau\": ggthemes_palette(\"tableau\", n=4),\n", " \"wsj\": ggthemes_palette(\"wsj\", n=4),\n", "}\n" ] }, { "cell_type": "code", "execution_count": null, "id": "7d86bfb7", "metadata": {}, "outputs": [], "source": [ "(\n", " ggplot(penguin_data, aes(\"bill_length_mm\", \"bill_depth_mm\", color=\"species\"))\n", " + geom_point(alpha=0.75)\n", " + scale_color_colorblind(k=3)\n", " + theme_few()\n", " + labs(x=\"Bill length (mm)\", y=\"Bill depth (mm)\", color=\"Species\")\n", ")\n" ] }, { "cell_type": "markdown", "id": "b1c30919", "metadata": {}, "source": [ "## ggthemes-inspired themes\n", "\n", "Theme helpers are plain plotnine themes. They can be combined with labels,\n", "scales, and guides.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "e5949dd2", "metadata": {}, "outputs": [], "source": [ "theme_data = flights[flights[\"year\"].isin([1956, 1957, 1958])].copy()\n", "theme_data[\"month_index\"] = theme_data.groupby(\"year\").cumcount() + 1\n", "\n", "(\n", " ggplot(theme_data, aes(\"month_index\", \"passengers\", color=\"factor(year)\"))\n", " + geom_line(size=1)\n", " + scale_color_wsj(k=3)\n", " + theme_wsj()\n", " + labs(x=\"Month\", y=\"Passengers\", color=\"Year\", title=\"Air passengers\")\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "13452217", "metadata": {}, "outputs": [], "source": [ "(\n", " ggplot(theme_data, aes(\"month_index\", \"passengers\", color=\"factor(year)\"))\n", " + geom_line(size=1)\n", " + scale_color_economist(k=3)\n", " + theme_economist()\n", " + labs(x=\"Month\", y=\"Passengers\", color=\"Year\", title=\"Economist-style line plot\")\n", ")\n" ] }, { "cell_type": "markdown", "id": "132fa552", "metadata": {}, "source": [ "## Publication themes and styling helpers\n", "\n", "The ggpubr-style helpers adjust labels, grids, font settings, rotations, and\n", "axis scales without requiring a new theme class.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "0351db06", "metadata": {}, "outputs": [], "source": [ "(\n", " ggplot(tooth, aes(\"dose\", \"len\", fill=\"supp\"))\n", " + geom_boxplot()\n", " + scale_fill_tableau(k=2)\n", " + theme_pubr()\n", " + labs(title=\"Tooth growth\", x=\"Dose\", y=\"Length\")\n", " + labs_pubr()\n", " + rotate_x_text(0)\n", " + grids(axis=\"y\")\n", ")\n" ] }, { "cell_type": "markdown", "id": "76ccac87", "metadata": {}, "source": [ "## Palette utilities\n", "\n", "Palette utilities are useful outside a plot when preparing style guides or\n", "manual scales.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "e430ec35", "metadata": {}, "outputs": [], "source": [ "{\n", " \"palette_names\": all_palette_names()[:8],\n", " \"npg_colors\": get_palette(\"npg\", 3),\n", " \"point_shapes\": type(show_point_shapes()).__name__,\n", " \"line_types\": type(show_line_types()).__name__,\n", "}\n" ] }, { "cell_type": "markdown", "id": "536e92cb", "metadata": {}, "source": [ "## Scale utilities\n", "\n", "`extended_range_breaks()`, `smart_digits()`, and `bank_slopes()` support axis\n", "labels and aspect-ratio choices.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "f4d6685b", "metadata": {}, "outputs": [], "source": [ "breaks = extended_range_breaks(n=5)((0.2, 9.7))\n", "{\n", " \"breaks\": breaks,\n", " \"labels\": smart_digits(breaks),\n", " \"bank\": bank_slopes([0, 1, 2], [0, 2, 4]),\n", "}\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 5 }