{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:23.158406Z", "iopub.status.busy": "2025-11-05T13:44:23.158291Z", "iopub.status.idle": "2025-11-05T13:44:23.162299Z", "shell.execute_reply": "2025-11-05T13:44:23.162104Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "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": "2025-11-05T13:44:23.176272Z", "iopub.status.busy": "2025-11-05T13:44:23.176173Z", "iopub.status.idle": "2025-11-05T13:44:23.180855Z", "shell.execute_reply": "2025-11-05T13:44:23.180639Z" } }, "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", "
xy
019.3524660.481977
147.585001-0.444944
236.867703-0.738881
330.334266-0.882739
48.6449130.703183
\n", "
" ], "text/plain": [ " x y\n", "0 19.352466 0.481977\n", "1 47.585001 -0.444944\n", "2 36.867703 -0.738881\n", "3 30.334266 -0.882739\n", "4 8.644913 0.703183" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.seed(42)\n", "\n", "x = np.random.uniform(1, 50, size=40)\n", "\n", "data = pd.DataFrame({'x': x, 'y': np.sin(x)})\n", "data.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**geom_line()** connects points in order of the variable on the x-axis." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:23.181867Z", "iopub.status.busy": "2025-11-05T13:44:23.181789Z", "iopub.status.idle": "2025-11-05T13:44:23.211125Z", "shell.execute_reply": "2025-11-05T13:44:23.210780Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(data) + geom_point(aes(x='x', y='y', color='x'), alpha=0.7, size=4) + \\\n", " scale_color_discrete() + \\\n", " theme(legend_position='none') + \\\n", " geom_line(aes(x='x', y='y'), linetype=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**geom_path()** connects observations in the order how they appear in data." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:23.212154Z", "iopub.status.busy": "2025-11-05T13:44:23.212077Z", "iopub.status.idle": "2025-11-05T13:44:23.215446Z", "shell.execute_reply": "2025-11-05T13:44:23.215275Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(data) + geom_point(aes(x='x', y='y', color='x'), alpha=0.7, size=4) + \\\n", " scale_color_discrete() + \\\n", " theme(legend_position='none') + \\\n", " geom_path(aes(x='x', y='y'), size=0.7, linetype='dotted')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another example to demonstrate the difference between geom_path() and geom_line()." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:23.216337Z", "iopub.status.busy": "2025-11-05T13:44:23.216265Z", "iopub.status.idle": "2025-11-05T13:44:23.217878Z", "shell.execute_reply": "2025-11-05T13:44:23.217693Z" } }, "outputs": [], "source": [ "a = [5, 1, 1, 5, 5, 2, 2, 4, 4, 3]\n", "b = [5, 5, 1, 1, 4, 4, 2, 2, 3, 3]\n", "\n", "snail = pd.DataFrame({'x': a, 'y': b})" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:23.218621Z", "iopub.status.busy": "2025-11-05T13:44:23.218549Z", "iopub.status.idle": "2025-11-05T13:44:23.221230Z", "shell.execute_reply": "2025-11-05T13:44:23.221062Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_path(data=snail, mapping=aes(x='x', y='y'), size=2, alpha=0.7)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:23.221981Z", "iopub.status.busy": "2025-11-05T13:44:23.221909Z", "iopub.status.idle": "2025-11-05T13:44:23.224551Z", "shell.execute_reply": "2025-11-05T13:44:23.224386Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_line(data=snail, mapping=aes(x='x', y='y'), size=2, alpha=0.7)" ] } ], "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 }