{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:30:50.503849Z", "iopub.status.busy": "2024-04-17T07:30:50.503529Z", "iopub.status.idle": "2024-04-17T07:30:50.826138Z", "shell.execute_reply": "2024-04-17T07:30:50.825727Z" } }, "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": "2024-04-17T07:30:50.839291Z", "iopub.status.busy": "2024-04-17T07:30:50.839171Z", "iopub.status.idle": "2024-04-17T07:30:50.844120Z", "shell.execute_reply": "2024-04-17T07:30:50.843947Z" } }, "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": "2024-04-17T07:30:50.845248Z", "iopub.status.busy": "2024-04-17T07:30:50.845091Z", "iopub.status.idle": "2024-04-17T07:30:50.876676Z", "shell.execute_reply": "2024-04-17T07:30:50.876410Z" } }, "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": "2024-04-17T07:30:50.877816Z", "iopub.status.busy": "2024-04-17T07:30:50.877731Z", "iopub.status.idle": "2024-04-17T07:30:50.881005Z", "shell.execute_reply": "2024-04-17T07:30:50.880782Z" } }, "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": "2024-04-17T07:30:50.882074Z", "iopub.status.busy": "2024-04-17T07:30:50.881999Z", "iopub.status.idle": "2024-04-17T07:30:50.883980Z", "shell.execute_reply": "2024-04-17T07:30:50.883661Z" } }, "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": "2024-04-17T07:30:50.884999Z", "iopub.status.busy": "2024-04-17T07:30:50.884856Z", "iopub.status.idle": "2024-04-17T07:30:50.887447Z", "shell.execute_reply": "2024-04-17T07:30:50.887278Z" } }, "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": "2024-04-17T07:30:50.888351Z", "iopub.status.busy": "2024-04-17T07:30:50.888275Z", "iopub.status.idle": "2024-04-17T07:30:50.890589Z", "shell.execute_reply": "2024-04-17T07:30:50.890414Z" } }, "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 }