{ "cells": [ { "cell_type": "markdown", "id": "9a1e235f-7006-4656-8e0f-f57da158c04d", "metadata": {}, "source": [ "# Expanding Plot Limits with `expand_limits()`\n", "\n", "When creating visualizations, you might occasionally need to adjust your plot boundaries to encompass specific data points or values. This is where the `expand_limits()` function comes in handy. It allows you to extend the plot's scales to include particular values, ensuring they're visible in your visualization.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "984c7118-bef0-4362-bee5-b4b500e62d65", "metadata": {}, "outputs": [], "source": [ "from lets_plot import *\n", "import pandas as pd\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "aaf39f46-4153-4ad4-98ea-15a5e8c1262c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "4678d72d-fc0d-4d51-b6c6-3dead0bd5ad1", "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": [ "mpg = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg2.csv')\n", "mpg.head(3)" ] }, { "cell_type": "code", "execution_count": 4, "id": "8891c0dd-1f00-4291-86fc-82c015a0bb33", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(mpg, aes(\"miles per gallon\", \"vehicle weight (lbs.)\")) + geom_point() \n", "p" ] }, { "cell_type": "markdown", "id": "5bc1e328-338c-4423-a1fa-9298c062489b", "metadata": {}, "source": [ "#### Expand Lower Limit Along the x-axis" ] }, { "cell_type": "code", "execution_count": 5, "id": "2cf4112a-aa7b-4c58-bafa-cfb520de3667", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + expand_limits(x=0)" ] }, { "cell_type": "markdown", "id": "69516526-82fc-4e4c-bb47-c60ec9a692da", "metadata": {}, "source": [ "#### Expand Limits Along the y-axis " ] }, { "cell_type": "code", "execution_count": 6, "id": "0c32bee6-2b08-462f-9600-2340f5e4a3e6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + expand_limits(y=[1000, 9000])" ] }, { "cell_type": "markdown", "id": "d97e894e-eea4-4d9c-a271-e86465acb8e7", "metadata": {}, "source": [ "#### Expand Lower Limits Along Both Axes" ] }, { "cell_type": "code", "execution_count": 7, "id": "5a3ba04a-5ccc-402c-a03b-91910674291e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + expand_limits(x = 0, y = 0)" ] }, { "cell_type": "markdown", "id": "ff4fff16-66d0-449f-b7cf-78211dc65bd8", "metadata": {}, "source": [ "#### Expanding Color-scale Limits" ] }, { "cell_type": "code", "execution_count": 8, "id": "6719668e-0753-45a1-8e18-abc912656d0c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add color mapping\n", "p1 = p + aes(color=\"number of cylinders\")\n", "\n", "gggrid([\n", " p1, \n", " # Expand the color-scale limits.\n", " p1 + expand_limits(color=range(2, 11, 2))\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.20" } }, "nbformat": 4, "nbformat_minor": 5 }