{ "cells": [ { "cell_type": "markdown", "id": "2ac0e74d", "metadata": {}, "source": [ "# Axis Position\n", "\n", "The `position` parameter in `scale_x_(), scale_y_()` functions controls position of the axis:\n", "\n", "- 'left', 'right' or 'both' for y-axis;\n", "- 'top', 'bottom' or 'both' for x-axis.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "8ab5b1ed", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:42:43.927227Z", "iopub.status.busy": "2025-11-05T13:42:43.927049Z", "iopub.status.idle": "2025-11-05T13:42:43.931863Z", "shell.execute_reply": "2025-11-05T13:42:43.931586Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "b0b9825c", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:42:43.932700Z", "iopub.status.busy": "2025-11-05T13:42:43.932587Z", "iopub.status.idle": "2025-11-05T13:42:43.934774Z", "shell.execute_reply": "2025-11-05T13:42:43.934552Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()\n", "LetsPlot.set_theme(theme_grey())" ] }, { "cell_type": "code", "execution_count": 3, "id": "5e6d519e", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:42:43.935634Z", "iopub.status.busy": "2025-11-05T13:42:43.935523Z", "iopub.status.idle": "2025-11-05T13:42:44.024999Z", "shell.execute_reply": "2025-11-05T13:42:44.024635Z" } }, "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", "
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
\n", "
" ], "text/plain": [ " sepal_length sepal_width petal_length petal_width species\n", "0 5.1 3.5 1.4 0.2 setosa\n", "1 4.9 3.0 1.4 0.2 setosa\n", "2 4.7 3.2 1.3 0.2 setosa" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/iris.csv')\n", "df.head(3)" ] }, { "cell_type": "markdown", "id": "b8877904", "metadata": {}, "source": [ "#### 1. Default Axis Position\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "e0859183", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:42:44.026120Z", "iopub.status.busy": "2025-11-05T13:42:44.025999Z", "iopub.status.idle": "2025-11-05T13:42:44.056262Z", "shell.execute_reply": "2025-11-05T13:42:44.055994Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = (ggplot(df) + geom_point(aes(\"sepal_width\", \"sepal_length\", color=\"species\"), size=5)\n", " + scale_color_brewer(palette=\"Set1\")\n", " )\n", "p" ] }, { "cell_type": "markdown", "id": "8e816608", "metadata": {}, "source": [ "#### 2. `position=\"right\"`" ] }, { "cell_type": "code", "execution_count": 5, "id": "0202aa5c", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:42:44.057234Z", "iopub.status.busy": "2025-11-05T13:42:44.057151Z", "iopub.status.idle": "2025-11-05T13:42:44.060358Z", "shell.execute_reply": "2025-11-05T13:42:44.060181Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + scale_y_continuous(\"Sepal Length\", position=\"right\")" ] }, { "cell_type": "markdown", "id": "9e01a07b", "metadata": {}, "source": [ "#### 3. `position=\"top\"`" ] }, { "cell_type": "code", "execution_count": 6, "id": "4cc9ecd1", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:42:44.061132Z", "iopub.status.busy": "2025-11-05T13:42:44.061062Z", "iopub.status.idle": "2025-11-05T13:42:44.064386Z", "shell.execute_reply": "2025-11-05T13:42:44.064125Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + scale_x_continuous(position=\"top\")" ] }, { "cell_type": "markdown", "id": "11909c79", "metadata": {}, "source": [ "#### 4. `position=\"both\"`" ] }, { "cell_type": "code", "execution_count": 7, "id": "b6dfc0a2", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:42:44.065086Z", "iopub.status.busy": "2025-11-05T13:42:44.065010Z", "iopub.status.idle": "2025-11-05T13:42:44.068075Z", "shell.execute_reply": "2025-11-05T13:42:44.067905Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(p\n", " + scale_x_continuous(\"Sepal Width\", position=\"both\")\n", " + scale_y_continuous(\"Sepal Length\", position=\"both\")\n", ")" ] }, { "cell_type": "markdown", "id": "346bad10", "metadata": {}, "source": [ "##### 4.1 Lets use equal units on the X-axis and on the Y-axis." ] }, { "cell_type": "code", "execution_count": 8, "id": "a360a62d", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:42:44.068698Z", "iopub.status.busy": "2025-11-05T13:42:44.068623Z", "iopub.status.idle": "2025-11-05T13:42:44.071788Z", "shell.execute_reply": "2025-11-05T13:42:44.071617Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(p\n", " + scale_x_continuous(\"Sepal Width\", position=\"both\")\n", " + scale_y_continuous(\"Sepal Length\", position=\"both\")\n", " + coord_fixed()\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.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }