{ "cells": [ { "cell_type": "markdown", "id": "b64186c9-ae08-4eae-8ea8-d1ae43bc8b95", "metadata": {}, "source": [ "# Handling of Empty Panels in `facet_wrap()`" ] }, { "cell_type": "code", "execution_count": 1, "id": "0ce1758f-9bdb-4213-b22e-8376d39681d6", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from lets_plot import *\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "3561f448-7760-4d26-9b01-cfb8be00f93c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "2c595e38-65ec-40d3-a16b-927879998360", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
miles per gallonnumber of cylindersengine displacement (cu. inches)engine horsepowervehicle weight (lbs.)time to accelerate (sec.)model yearorigin of carvehicle name
018.08307.0130350412.070USchevrolet chevelle malibu
115.08350.0165369311.570USbuick skylark 320
218.08318.0150343611.070USplymouth satellite
\n", "
" ], "text/plain": [ " miles per gallon number of cylinders engine displacement (cu. inches) \\\n", "0 18.0 8 307.0 \n", "1 15.0 8 350.0 \n", "2 18.0 8 318.0 \n", "\n", " engine horsepower vehicle weight (lbs.) time to accelerate (sec.) \\\n", "0 130 3504 12.0 \n", "1 165 3693 11.5 \n", "2 150 3436 11.0 \n", "\n", " model year origin of car vehicle name \n", "0 70 US chevrolet chevelle malibu \n", "1 70 US buick skylark 320 \n", "2 70 US plymouth satellite " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg2.csv')\n", "df.head(3)" ] }, { "cell_type": "code", "execution_count": 4, "id": "49e00fb9-1a05-4366-ad23-c6973a942ac1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = (\n", " ggplot(df, \n", " aes('engine horsepower', 'miles per gallon', \n", " color=as_discrete('origin of car',order=-1)))\n", " + geom_point()\n", " + theme_grey() + ggtitle('Efficiency vs Engine Horsepower')\n", ")\n", "p" ] }, { "cell_type": "markdown", "id": "467498b5-95ac-4cce-9ce0-9024af0120b2", "metadata": {}, "source": [ "### Empty Facet Panels are Dropped by Default\n", "\n", "For example, there are no 3-cylinder engine cars originated in the US. Accordingly, this facet panel is dropped." ] }, { "cell_type": "code", "execution_count": 5, "id": "ebda0228-9500-4e48-ba05-64f98b645218", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + facet_wrap(['origin of car', 'number of cylinders'], order=[-1, 1], ncol=3)" ] }, { "cell_type": "markdown", "id": "28a91a2c-38dd-4f75-b9aa-2d7daa624070", "metadata": {}, "source": [ "### Using the `drop` Parameter to Control Empty Panels\n", "\n", "Use `drop=False` to show all factor levels regardless of whether they occur in the data." ] }, { "cell_type": "code", "execution_count": 6, "id": "adfcc480-582e-41b9-b6d9-1f2c2c687a0b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + facet_wrap(['origin of car', 'number of cylinders'], order=[-1,1], ncol=5,\n", " drop=False\n", ")" ] } ], "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.23" } }, "nbformat": 4, "nbformat_minor": 5 }