{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# The `orientation` Parameter\n", "\n", "Some geoms treat each axis differently and, thus, can have two orientations.\n", "\n", "The `orientation` parameter specifies the axis that the layer's stat and geom should run along (x-axis by default). " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:50.886009Z", "iopub.status.busy": "2026-01-27T17:08:50.885936Z", "iopub.status.idle": "2026-01-27T17:08:50.888776Z", "shell.execute_reply": "2026-01-27T17:08:50.888523Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:50.889692Z", "iopub.status.busy": "2026-01-27T17:08:50.889617Z", "iopub.status.idle": "2026-01-27T17:08:50.891259Z", "shell.execute_reply": "2026-01-27T17:08:50.891031Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### One Axis Is Discrete, the Other Continuous" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:50.904389Z", "iopub.status.busy": "2026-01-27T17:08:50.904289Z", "iopub.status.idle": "2026-01-27T17:08:51.001951Z", "shell.execute_reply": "2026-01-27T17:08:51.001708Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(234, 12)\n" ] }, { "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
\n", "
" ], "text/plain": [ " Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy \\\n", "0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 \n", "1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 \n", "2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 \n", "\n", " fl class \n", "0 p compact \n", "1 p compact \n", "2 p compact " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mpg_df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "print(mpg_df.shape)\n", "mpg_df.head(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### `geom_bar()`" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.003104Z", "iopub.status.busy": "2026-01-27T17:08:51.002990Z", "iopub.status.idle": "2026-01-27T17:08:51.005232Z", "shell.execute_reply": "2026-01-27T17:08:51.005004Z" } }, "outputs": [], "source": [ "base = ggplot(mpg_df) + ggsize(800, 300)\n", "manufacturer_mapping = as_discrete('manufacturer', order_by='..count..')\n", "hide_x_label = theme(axis_title_x='blank')\n", "hide_y_label = theme(axis_title_y='blank')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.006006Z", "iopub.status.busy": "2026-01-27T17:08:51.005931Z", "iopub.status.idle": "2026-01-27T17:08:51.036105Z", "shell.execute_reply": "2026-01-27T17:08:51.035841Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Orientation \"x\" (default) : \"manufacturer\" is mapped to the x-axis.\n", "orientation_x = base + geom_bar(aes(x=manufacturer_mapping, fill='class'), color='white')\n", "orientation_x + hide_x_label" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.037119Z", "iopub.status.busy": "2026-01-27T17:08:51.037035Z", "iopub.status.idle": "2026-01-27T17:08:51.041933Z", "shell.execute_reply": "2026-01-27T17:08:51.041687Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Flip coordinates.\n", "orientation_x + coord_flip() + hide_x_label" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.042663Z", "iopub.status.busy": "2026-01-27T17:08:51.042583Z", "iopub.status.idle": "2026-01-27T17:08:51.047678Z", "shell.execute_reply": "2026-01-27T17:08:51.047452Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Orientation \"y\" : \"manufacturer\" is mapped to the y-axis.\n", "base_y = base + hide_y_label\n", "base_y + geom_bar(aes(y=manufacturer_mapping, fill='class'), color='white', orientation=\"y\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.048451Z", "iopub.status.busy": "2026-01-27T17:08:51.048374Z", "iopub.status.idle": "2026-01-27T17:08:51.053373Z", "shell.execute_reply": "2026-01-27T17:08:51.053121Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The \"y\" orientation is automatically selected if x is a continuous axis and y is a discrete axis.\n", "base_y + geom_bar(aes(y=manufacturer_mapping, fill='class'), color='white')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### `geom_boxplot()`" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.054042Z", "iopub.status.busy": "2026-01-27T17:08:51.053966Z", "iopub.status.idle": "2026-01-27T17:08:51.058974Z", "shell.execute_reply": "2026-01-27T17:08:51.058743Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Implicit change of orientation.\n", "base_y + geom_boxplot(aes(y='manufacturer', x='cty'), width=0.7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### `geom_violin()`" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.059690Z", "iopub.status.busy": "2026-01-27T17:08:51.059615Z", "iopub.status.idle": "2026-01-27T17:08:51.083634Z", "shell.execute_reply": "2026-01-27T17:08:51.083350Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Implicit change of orientation.\n", "ggplot(mpg_df) + \\\n", " geom_violin(aes(y=\"drv\", x='cty', fill=\"drv\"), trim=False) + \\\n", " geom_boxplot(aes(y=\"drv\", x='cty'), fill=\"white\", alpha=0.5, width=0.3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### `geom_lollipop()`" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.084654Z", "iopub.status.busy": "2026-01-27T17:08:51.084562Z", "iopub.status.idle": "2026-01-27T17:08:51.089636Z", "shell.execute_reply": "2026-01-27T17:08:51.089405Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Implicit change of orientation.\n", "base_y + geom_lollipop(aes(y='manufacturer'), stat='count') + scale_y_continuous(expand=[.05, 0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### `stat_summary()`" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.090679Z", "iopub.status.busy": "2026-01-27T17:08:51.090599Z", "iopub.status.idle": "2026-01-27T17:08:51.094809Z", "shell.execute_reply": "2026-01-27T17:08:51.094598Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Implicit change of orientation.\n", "base_y + stat_summary(aes(y='manufacturer', x='cty'), width=0.7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### `as_discrete()`\n", "\n", "Sometimes an explicit use of `as_discrete()` is required - when the discrete data on the y-axis is represented by numbers." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.095767Z", "iopub.status.busy": "2026-01-27T17:08:51.095693Z", "iopub.status.idle": "2026-01-27T17:08:51.103934Z", "shell.execute_reply": "2026-01-27T17:08:51.103707Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Explicit change of orientation through as_discrete().\n", "gggrid([\n", " ggplot(mpg_df) + geom_bar(aes('cty', 'cyl', fill='cyl'), stat='sum', size=0) + scale_fill_discrete(),\n", " ggplot(mpg_df) + geom_bar(aes('cty', as_discrete('cyl'), fill='cyl'), stat='sum', size=0) + scale_fill_discrete(),\n", "])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Continuous Variables\n", "\n", "In the case where both variables are continuous, the orientation should always be specified explicitly." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.104828Z", "iopub.status.busy": "2026-01-27T17:08:51.104755Z", "iopub.status.idle": "2026-01-27T17:08:51.107123Z", "shell.execute_reply": "2026-01-27T17:08:51.106881Z" } }, "outputs": [], "source": [ "#\n", "# Density estimates.\n", "#\n", "\n", "np.random.seed(0)\n", "\n", "cov0 = [[1, -.8], \n", " [-.8, 1]] \n", "cov1 = [[ 10, .1],\n", " [.1, .1]]\n", "\n", "x0, y0 = np.random.multivariate_normal(mean=[-2,0], cov=cov0, size=200).T\n", "x1, y1 = np.random.multivariate_normal(mean=[0,1], cov=cov1, size=200).T\n", "\n", "data = dict(\n", " x = np.concatenate((x0, x1)),\n", " y = np.concatenate((y0, y1)),\n", " c = [\"A\"]*200 + [\"B\"]*200\n", ")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.107962Z", "iopub.status.busy": "2026-01-27T17:08:51.107881Z", "iopub.status.idle": "2026-01-27T17:08:51.112339Z", "shell.execute_reply": "2026-01-27T17:08:51.112122Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(data, aes(\"x\", \"y\", color=\"c\")) + geom_point()\n", "p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### `geom_density()`" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.113108Z", "iopub.status.busy": "2026-01-27T17:08:51.113030Z", "iopub.status.idle": "2026-01-27T17:08:51.155416Z", "shell.execute_reply": "2026-01-27T17:08:51.155071Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_density(size=2),\n", " p + geom_density(size=2, orientation=\"y\"),\n", "])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### `geom_histogram()`" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.156292Z", "iopub.status.busy": "2026-01-27T17:08:51.156203Z", "iopub.status.idle": "2026-01-27T17:08:51.166234Z", "shell.execute_reply": "2026-01-27T17:08:51.165967Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_histogram(fill=\"rgba(0,0,0,0)\"),\n", " p + geom_histogram(alpha=0, orientation=\"y\", show_legend=False),\n", "])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### `geom_freqpoly()`" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.167038Z", "iopub.status.busy": "2026-01-27T17:08:51.166955Z", "iopub.status.idle": "2026-01-27T17:08:51.175037Z", "shell.execute_reply": "2026-01-27T17:08:51.174786Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_freqpoly(size=2),\n", " p + geom_freqpoly(size=2, orientation=\"y\"),\n", "])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### `geom_smooth()`" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.175819Z", "iopub.status.busy": "2026-01-27T17:08:51.175735Z", "iopub.status.idle": "2026-01-27T17:08:51.190740Z", "shell.execute_reply": "2026-01-27T17:08:51.190471Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_smooth(),\n", " p + geom_smooth() + coord_flip(),\n", " p + geom_smooth(orientation=\"y\"),\n", "])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### `geom_boxplot()`" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.191720Z", "iopub.status.busy": "2026-01-27T17:08:51.191629Z", "iopub.status.idle": "2026-01-27T17:08:51.204699Z", "shell.execute_reply": "2026-01-27T17:08:51.204392Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + \\\n", " geom_boxplot(x=-10, color=\"black\") + \\\n", " geom_boxplot(y=-3, color=\"black\", orientation=\"y\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### `geom_violin()`" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.205516Z", "iopub.status.busy": "2026-01-27T17:08:51.205429Z", "iopub.status.idle": "2026-01-27T17:08:51.239668Z", "shell.execute_reply": "2026-01-27T17:08:51.239412Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + \\\n", " geom_violin(x=-10, color=\"black\", alpha=0) + \\\n", " geom_violin(y=-3, color=\"black\", alpha=0, orientation=\"y\")" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:08:51.240600Z", "iopub.status.busy": "2026-01-27T17:08:51.240513Z", "iopub.status.idle": "2026-01-27T17:08:51.263661Z", "shell.execute_reply": "2026-01-27T17:08:51.263409Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_violin(aes(fill=\"c\"), y=-2, color=\"black\", \n", " alpha=0.3, position=\"identity\", width=3, show_legend=False,\n", " orientation=\"y\")" ] } ], "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 }