{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## `Free` scales on a faceted plot" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:11.047879Z", "iopub.status.busy": "2024-04-17T07:29:11.047801Z", "iopub.status.idle": "2024-04-17T07:29:11.369966Z", "shell.execute_reply": "2024-04-17T07:29:11.369677Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:11.383020Z", "iopub.status.busy": "2024-04-17T07:29:11.382863Z", "iopub.status.idle": "2024-04-17T07:29:11.519516Z", "shell.execute_reply": "2024-04-17T07:29:11.519254Z" } }, "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": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg2.csv')\n", "data.head(3)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:11.520607Z", "iopub.status.busy": "2024-04-17T07:29:11.520509Z", "iopub.status.idle": "2024-04-17T07:29:11.554578Z", "shell.execute_reply": "2024-04-17T07:29:11.554217Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = (ggplot(data, aes(x=\"engine horsepower\", y=\"engine displacement (cu. inches)\")) + \n", " geom_point(aes(color=\"origin of car\")) + theme_grey())\n", "p + ggsize(800, 350)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Faceted plot" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:11.555935Z", "iopub.status.busy": "2024-04-17T07:29:11.555790Z", "iopub.status.idle": "2024-04-17T07:29:11.557749Z", "shell.execute_reply": "2024-04-17T07:29:11.557482Z" } }, "outputs": [], "source": [ "fp = p + ggsize(800, 500)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `facet_grid()` with `fixed` scales (the default)\n", "\n", "Scales are constant across all panels." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:11.558882Z", "iopub.status.busy": "2024-04-17T07:29:11.558714Z", "iopub.status.idle": "2024-04-17T07:29:11.566369Z", "shell.execute_reply": "2024-04-17T07:29:11.566177Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fp + facet_grid(y='origin of car')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `facet_grid()` with `free` Y-scales" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:11.567418Z", "iopub.status.busy": "2024-04-17T07:29:11.567262Z", "iopub.status.idle": "2024-04-17T07:29:11.572163Z", "shell.execute_reply": "2024-04-17T07:29:11.571993Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fp + facet_grid(y='origin of car', scales='free_y')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `facet_wrap()` with `fixed` scales (the default)\n", "\n", "Scales are constant across all panels." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:11.573158Z", "iopub.status.busy": "2024-04-17T07:29:11.573042Z", "iopub.status.idle": "2024-04-17T07:29:11.578373Z", "shell.execute_reply": "2024-04-17T07:29:11.578199Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fp + facet_wrap(facets=\"number of cylinders\", order=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `facet_wrap()` with `free` scales along both axis" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:11.579292Z", "iopub.status.busy": "2024-04-17T07:29:11.579214Z", "iopub.status.idle": "2024-04-17T07:29:11.584496Z", "shell.execute_reply": "2024-04-17T07:29:11.584321Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fp + facet_wrap(facets=\"number of cylinders\", order=1, scales='free')" ] } ], "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.10.13" } }, "nbformat": 4, "nbformat_minor": 4 }