{ "cells": [ { "cell_type": "markdown", "id": "8c6e7594", "metadata": {}, "source": [ "# Horizontal and Vertical Facet Labels\n", "\n", "\n", "\n", "New `theme()` parameters allow to set the style for horizontal and vertical facet labels separately:\n", "\n", "- `strip_background_x` and `strip_background_y` parameters set the background of horizontal and vertical facet labels respectively, specified with `element_rect()`;\n", "\n", "- `strip_text_x` and `strip_text_y` - horizontal and vertical facet labels, specified with `element_text()`.\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "1bf79612", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "9e0f34c9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "d5fed6b5", "metadata": {}, "outputs": [], "source": [ "data = pd.DataFrame.from_records([\n", " (\"pet\", \"cat\", 5, \"carnivore\"),\n", " (\"pet\", \"dog\", 10, \"carnivore\"),\n", " (\"pet\", \"rabbit\", 2, \"herbivore\"),\n", " (\"pet\", \"hamster\", 1, \"herbivore\"),\n", "\n", " (\"farm\", \"cow\", 500, \"herbivore\"),\n", " (\"farm\", \"pig\", 100, \"carnivore\"),\n", " (\"farm\", \"horse\", 700, \"herbivore\"),\n", "])\n", "data.columns = (\"animal_type\", \"animal\", \"weight\", \"diet\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "17997a9c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(data, aes(x='animal', y='weight')) + \\\n", " geom_bar(stat='identity') + \\\n", " facet_grid(x='animal_type', y='diet', scales='free') + \\\n", " theme_bw()\n", "\n", "p" ] }, { "cell_type": "markdown", "id": "6419bb58", "metadata": {}, "source": [ "Change the style for facet labels:" ] }, { "cell_type": "code", "execution_count": 5, "id": "cf99de92", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + theme(strip_text_x=element_text(face='bold'),\n", " strip_background_x=element_rect(fill='pink'),\n", " strip_text_y=element_text(face='italic'),\n", " strip_background_y=element_rect(fill='light_blue'))" ] }, { "cell_type": "markdown", "id": "59a8830d", "metadata": {}, "source": [ "Hide horizontal facet labels: " ] }, { "cell_type": "code", "execution_count": 6, "id": "270b0370", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + theme(strip_text_x=element_blank())" ] } ], "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.9.20" } }, "nbformat": 4, "nbformat_minor": 5 }