{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "928abbdd-acba-4101-b536-37c5bd39a8a3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "from lets_plot.bistro import *\n", "from scipy.stats import multivariate_normal\n", "from lets_plot.geo_data import *\n", "import pandas as pd\n", "import numpy as np\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "id": "7b5c79b2-ac1a-40dd-91ab-45697979b4d9", "metadata": {}, "outputs": [], "source": [ "path_df = {\n", " 'x': [None, None, 0.0, None, 1.0, 2.0, None, 4.0, 5.0, 6.0],\n", " 'y': [None, 0.0, 0.5, 0.0, 0.0, 1.0, None, None, 0.5, 1.0],\n", " 'c': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "id": "9c68cea7-a827-4417-8463-2cc208bc8027", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(path_df, aes('x','y')) + geom_line()" ] }, { "cell_type": "code", "execution_count": 4, "id": "e348f34b-a7c6-413c-9d65-32c38d30d788", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(path_df, aes('x','y')) + geom_line() + coord_polar()" ] }, { "cell_type": "code", "execution_count": 5, "id": "c7169538-6ab3-464f-ae1e-501333631e9f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(path_df, aes(\"x\", \"y\", color=\"c\")) + geom_path(size=3) + geom_point(color=\"red\")" ] }, { "cell_type": "code", "execution_count": 6, "id": "43e73257-3ce7-4a80-bbfe-328edf4abbbd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame(path_df)\n", "df['xend'] = df['x'].shift(-1)\n", "df['yend'] = df['y'].shift(-1)\n", "\n", "ggplot(df, aes(x='x', y='y', xend='xend', yend='yend', color='c')) + geom_segment(size=2)" ] }, { "cell_type": "code", "execution_count": 7, "id": "6c9927be-fcb2-417c-ac3e-6e7e31abc496", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(path_df, aes('x', 'y')) + geom_line()" ] }, { "cell_type": "code", "execution_count": 8, "id": "f67a7e6b-c5bc-4c4a-b952-01e8c9e0ea7a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(path_df, aes(\"x\", y=\"c\")) + geom_area(stat=\"identity\")" ] }, { "cell_type": "code", "execution_count": 9, "id": "0d6b9091-8566-4aab-9e1b-9d6d7dcac503", "metadata": {}, "outputs": [], "source": [ "ribbon_df = {\n", " 'x': [None, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0],\n", " 'ymin': [None, 0.0, 0.3, None, 0.2, None, 0.4, 0.6],\n", " 'ymax': [None, 0.5, 0.8, None, 0.7, 0.9, None, 1.0],\n", "}" ] }, { "cell_type": "code", "execution_count": 10, "id": "7297689b-15b2-4b52-b096-952e3eb5456d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(ribbon_df) + geom_ribbon(aes(x='x', ymin='ymin', ymax='ymax'))" ] }, { "cell_type": "code", "execution_count": 11, "id": "a797d535-6d2d-4d65-b604-40d2834034d1", "metadata": {}, "outputs": [], "source": [ "ridges_df = {\n", " 'x': [None, -1, -0.5, 0.0, 0.5, 1.0, None, -1, -0.5, 0.0, None, 0.5, 1.0, 2.0],\n", " 'g': ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B', 'B'],\n", " 'h': [None, 0.0, 0.6, 1.0, 0.6, 0.0, None, 0.0, 0.5, 0.8, None, 0.4, 0.0, 0.0]\n", "}" ] }, { "cell_type": "code", "execution_count": 12, "id": "68f646a8-d76c-4352-9c69-00b853c32e64", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(ridges_df, aes('x', 'g', height='h', group='g')) + geom_area_ridges(stat='identity')" ] }, { "cell_type": "code", "execution_count": 13, "id": "a98dcebc-81b8-4348-b666-f1383b14a120", "metadata": {}, "outputs": [], "source": [ "smooth_df = {\n", " 'x': [None, -2, -1, 0, 1, 2, None, 3, 4, 5, None, 6],\n", " 'y': [None, 1.5, 0.5, 0.0, 0.6, 1.0, None, 1.2, 1.0, 0.8, None, 0.7]\n", "}" ] }, { "cell_type": "code", "execution_count": 14, "id": "f6e1d4f3-3644-48d4-aeee-6deafb29759a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(smooth_df, aes('x', 'y')) + geom_smooth(method='loess') + geom_point()" ] }, { "cell_type": "code", "execution_count": 15, "id": "ee036ce7-bee8-4aaf-8cf6-7d5e819e2aae", "metadata": {}, "outputs": [], "source": [ "violin_df = {\n", " 'y': [0.3, None,0.2,0.5,0.8, None,0.4,0.6, 0.1, None,0.9, 0.7]\n", "}" ] }, { "cell_type": "code", "execution_count": 16, "id": "69ed0293-9139-4860-92a4-c67774ddeabd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(violin_df, aes(y = 'y')) + geom_violin()" ] }, { "cell_type": "code", "execution_count": 17, "id": "893b53ab-a1a2-4712-ab27-0d220ff7b51f", "metadata": {}, "outputs": [], "source": [ "step_df = {\n", " 'x': [None, 0, 1, None, 2, 3, 4, None, 5, 6],\n", " 'y': [None, 0, 1, None, None, 0.6, None, 0.9, 0.9, 1.0],\n", " 'c': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "}" ] }, { "cell_type": "code", "execution_count": 18, "id": "2f0e943f-fc18-4375-b594-fcb03599efa5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(step_df, aes('x', 'y', color='c')) + geom_step(size=3)" ] }, { "cell_type": "code", "execution_count": 19, "id": "a070e5fa-5a24-499a-abd6-39ed8aaf9f65", "metadata": {}, "outputs": [], "source": [ "polygon_df = { \n", " 'x': [None, 0, 1, 1, 0, 2, 3, None, None, 4, 5, None, 5, 4],\n", " 'y': [None, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0.5, None, 1, 1],\n", " 'id': ['A','A','A','A','A', 'B','B','B','B', 'C','C','C','C','C']\n", "}" ] }, { "cell_type": "code", "execution_count": 20, "id": "5a251e98-0def-49bf-bfc1-3099ef4465b1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(polygon_df, aes('x','y', group='id', color='id', fill='id')) + geom_polygon(size=5) + scale_fill_hue()" ] }, { "cell_type": "code", "execution_count": 21, "id": "553b9509-9683-4c0c-b014-49819169b311", "metadata": {}, "outputs": [], "source": [ "contour_df = {\n", " 'x': [0,1,2,3,4, 0,1,2,3,4, 0,1,2,3,4, 0,1,2,3,4, 0,1,2,3,4],\n", " 'y': [0,0,0,0,0, 1,1,1,1,1, 2,2,2,2,2, 3,3,3,3,3, 4,4,4,4,4],\n", " 'z': [0,1,4,9,16, 1,2,None,10,17, 4,None,None,None,20, 9,10,None,18,25, 16,17,20,25,32]\n", "}\n", "contour_df2 = {\n", " 'x': [0,1,2,3,4, 0,1,2,3,4, 0,1,2,3,4, 0,1,2,3,4, 0,1,2,3,4],\n", " 'y': [0,0,0,0,0, 1,1,1,1,1, 2,2,2,2,2, 3,3,3,3,3, 4,4,4,4,4],\n", " 'z': [0,1,4,9,16, 1,2,5,10,17,4,5,8,13,20, 9,10,13,18,25, 16,17,20,25,32]\n", "}" ] }, { "cell_type": "code", "execution_count": 22, "id": "5b9c92e5-8eb3-4455-b4d2-45d00b6e2e50", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(contour_df2, aes('x', 'y', z='z')) + geom_contour(bins=6)" ] }, { "cell_type": "code", "execution_count": 23, "id": "495560ac-2e59-4685-bb24-f7281bc45553", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = 25\n", "x = np.linspace(-1, 1, n)\n", "y = np.linspace(-1, 1, n)\n", "X, Y = np.meshgrid(x, y)\n", "mean = np.zeros(2)\n", "cov = [[1, .5],\n", " [.5, 1]]\n", "rv = multivariate_normal(mean, cov)\n", "Z = rv.pdf(np.dstack((X, Y)))\n", "data = {'x': X.flatten(), 'y': Y.flatten(), 'z': Z.flatten()}\n", "\n", "ggplot(data, aes(x='x', y='y', z='z')) + geom_contour()" ] }, { "cell_type": "code", "execution_count": 24, "id": "8b43d4be-306a-4d05-b534-1d08bafd228a", "metadata": {}, "outputs": [], "source": [ "density_df = {\n", " 'x': [None, -3.0, -2.9, -2.8, None, -1.0, -1.0, -0.8, None, 0.5, 0.6, 0.7, None, 3.0, 10.0, None],\n", " 'y': [None, 2, 3, 2, None, 3, None, 4, 5, 0, 1, 2, 3, 4, 1, 5]\n", "}" ] }, { "cell_type": "code", "execution_count": 25, "id": "af41cdab-d2ef-44ac-8bc9-4eb89bc7b0d6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(density_df, aes('x')) + geom_density()" ] }, { "cell_type": "code", "execution_count": 26, "id": "947394ef-663d-40b8-9607-81c0bc94b656", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(density_df, aes('x', 'y')) + geom_density(stat='identity')" ] }, { "cell_type": "code", "execution_count": 27, "id": "1842ef8d-6ffc-49f9-805a-f560d41e037f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(polygon_df, aes('x','y', group='id', color='id', fill='id')) + geom_map() + scale_fill_hue()" ] }, { "cell_type": "code", "execution_count": 28, "id": "ed91638b-013a-4095-a100-dc429b10e3ca", "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame({\n", " 'x': [0.0, 4.0, 1.0, 5.0, 2.0, 6.0, 3.0, 7.0],\n", " 'y': [4.0, 4.0, 3.0, 3.0, 2.0, 2.0, 1.0, 1.0],\n", " 'x_na': [None, 4.0, 1.0, 5.0, 2.0, None, 3.0, 7.0],\n", " 'y_na': [None, 4.0, 3.0, 3.0, 2.0, None, 1.0, 1.0],\n", " 'c': ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'b']\n", "})\n", "line = ggplot(df, aes(x='x', y='y', color='c')) + geom_line(linewidth = 1) + ggtitle('geom_line')\n", "line_x_na = ggplot(df, aes(x='x_na', y='y', color='c')) + geom_line(linewidth = 1) + ggtitle('geom_line NA in x')\n", "line_y_na = ggplot(df, aes(x='x', y='y_na', color='c')) + geom_line(linewidth = 1) + ggtitle('geom_line NA in y')\n", "path = ggplot(df, aes(x='x', y='y', color='c')) + geom_path(linewidth = 1) + ggtitle('geom_path')\n", "path_x_na = ggplot(df, aes(x='x_na', y='y', color='c')) + geom_path(linewidth = 1) + ggtitle('geom_path NA in x')\n", "path_y_na = ggplot(df, aes(x='x', y='y_na', color='c')) + geom_path(linewidth = 1) + ggtitle('geom_path NA in y')\n", "step = ggplot(df, aes(x='x', y='y', color='c')) + geom_step(linewidth = 1) + ggtitle('geom_step')\n", "step_x_na = ggplot(df, aes(x='x_na', y='y', color='c')) + geom_step(linewidth = 1) + ggtitle('geom_step NA in x')\n", "step_y_na = ggplot(df, aes(x='x', y='y_na', color='c')) + geom_step(linewidth = 1) + ggtitle('geom_step NA in y')" ] }, { "cell_type": "code", "execution_count": 29, "id": "eb4e969f-bdd5-4a58-8561-e07509f22a7b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([line, line_x_na, line_y_na])" ] }, { "cell_type": "code", "execution_count": 30, "id": "87cb3cc5-b8c5-4c21-8206-57eb8fb4dcba", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([path, path_x_na, path_y_na])" ] }, { "cell_type": "code", "execution_count": 31, "id": "7383b3b9-e28a-4f7c-870f-8002c48014dc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([step, step_x_na, step_y_na])" ] }, { "cell_type": "code", "execution_count": null, "id": "8b7a405c-b923-4853-9ad5-89c236e289d0", "metadata": {}, "outputs": [], "source": [] } ], "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 }