{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pandas import DataFrame\n", "import numpy as np\n", "import math\n", "from lets_plot import *\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Load datasets\n", "import pandas as pd\n", "\n", "mpg_df = pd.read_csv (\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "iris_df = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/iris.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Crosshair examples" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_smooth\n", "(ggplot(mpg_df, aes(x='displ', y='hwy')) + ggtitle('geom_smooth')\n", " + geom_point() \n", " + geom_smooth(method='loess', size=1, \n", " tooltips=layer_tooltips()\n", " .line('min|^ymin')\n", " .line('|^y')\n", " .line('max|^ymax'))\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_smooth with acnhor\n", "(ggplot(mpg_df, aes(x='displ', y='hwy')) + ggtitle('geom_smooth')\n", " + geom_point() \n", " + geom_smooth(method='loess', size=1, \n", " tooltips=layer_tooltips()\n", " .line('min|^ymin')\n", " .line('|^y')\n", " .line('max|^ymax')\n", " .anchor('top_right'))\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_ribbon\n", "id = [\"A\", \"A\", \"A\", \"B\", \"B\", \"B\"]\n", "x = [1, 2, 4, 1, 3, 4]\n", "ymin = [-1, 0, 0, 3, 3, 4]\n", "ymax = [0, 1, 1, 4, 5, 5]\n", "r_dat = {}\n", "r_dat = dict(id=id, x=x, ymin=ymin, ymax=ymax)\n", "\n", "# geom_ribbon\n", "ggplot(r_dat) + ggtitle('geom_ribbon') \\\n", " + geom_ribbon(aes(x='x', ymin='ymin', ymax='ymax', group='id', fill='id'), color='black', alpha=0.5)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_ribbon with anchor\n", "ggplot(r_dat) + ggtitle('geom_ribbon') \\\n", " + geom_ribbon(aes(x='x', ymin='ymin', ymax='ymax', group='id', fill='id'), color='black', alpha=0.5,\n", " tooltips=layer_tooltips().line('@|^ymax').line('@|^ymin').anchor('top_right'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_point\n", "(ggplot(mpg_df, aes(x='displ', y='cty', fill='drv', size='hwy')) + ggtitle('geom_point')\n", " + scale_size(range=[5, 15], breaks=[15, 40])\n", " + ggsize(600, 350)\n", " + geom_point(shape=21, color='white',\n", " tooltips=layer_tooltips()\n", " .anchor('top_right')\n", " .min_width(180)\n", " .format('cty', '.0f')\n", " .format('hwy', '.0f')\n", " .format('drv', '{}wd')\n", " .line('@manufacturer @model')\n", " .line('cty/hwy [mpg]|@cty/@hwy')\n", " .line('@|@class')\n", " .line('drive train|@drv')\n", " .line('@|@year')) \n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_area\n", "(ggplot(iris_df) + ggtitle('geom_area')\n", " + geom_area(aes(x='sepal_length', fill='species'), \n", " stat='density',\n", " color='white',\n", " tooltips=layer_tooltips()\n", " .anchor('top_right')\n", " .line('^fill')\n", " .line('length|^x')\n", " .line('density|^y'))\n", " + ggsize(650, 300) \n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_line\n", "random.seed(42)\n", "T = 1\n", "N = 1000\n", "t = np.linspace(0, T, N)\n", "dt = T / N\n", "# brownian motions\n", "W1 = np.random.standard_normal(size=N)\n", "Wt1 = np.cumsum(W1) * np.sqrt(dt)\n", "W2 = np.random.standard_normal(size=N)\n", "Wt2 = np.cumsum(W2) * np.sqrt(dt)\n", "l_dat = {}\n", "l_dat['W1'] = Wt1\n", "l_dat['W2'] = Wt2\n", "l_dat['t'] = t\n", "# transform data via melt function\n", "# to produce two trajectories\n", "l_dat = pd.DataFrame(l_dat)\n", "l_dat = pd.melt(l_dat, id_vars=['t'], value_vars=['W1', 'W2'])\n", "\n", "ggplot(l_dat, aes(x='t', y='value', group='variable')) + ggtitle('geom_line')\\\n", "+ geom_line(aes(color='variable'), size=1, alpha=0.7, tooltips=layer_tooltips().anchor('top_left'))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_freqpoly\n", "ggplot(l_dat, aes(x='value')) + ggtitle('geom_freqpoly') \\\n", "+ geom_freqpoly(size=2, tooltips=layer_tooltips().anchor('top_right'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_path\n", "path_dat={}\n", "path_dat['x']=[1e-3,2e-3,3e-3,4e-3,5e-3,5e-3,4e-3,3e-3,2e-3,1e-3]\n", "path_dat['y']=[1e-3,2e-3,3e-3,4e-3,5e-3,1e-3,2e-3,3e-3,4e-3,5e-3]\n", "path_dat['g']=[1,1,1,1,1,2,2,2,2,2]\n", "\n", "ggplot(path_dat, aes(x='x',y='y',group='g')) + ggtitle('geom_path')\\\n", " + geom_path(aes(color='g'), stat='density2d',bins=3, tooltips=layer_tooltips().anchor('middle_center'))\\\n", " + scale_color_gradient(low='dark_green',high='red')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_contour\n", "X_max = 50\n", "Y_max = 50\n", "def z_fun(x, y):\n", " z = math.sin(x * 3 * math.pi / X_max)\n", " z += math.sin(y * 3 * math.pi / Y_max)\n", " z += x * 3 / X_max\n", " z += y * 5 / Y_max\n", " return z\n", "\n", "x = []\n", "y = []\n", "z = []\n", "for row in range(0, Y_max - 1):\n", " for col in range(0, X_max - 1):\n", " x.append(col)\n", " y.append(row)\n", " z.append(z_fun(col, row))\n", "\n", "c_dat = dict(x=x, y=y, z=z)\n", "\n", "(ggplot(c_dat, aes('x', 'y')) + ggtitle('geom_contour')\n", " + scale_color_gradient('green', 'red')\n", " + geom_contour(aes(z='z', color='..level..'), bins=30, tooltips=layer_tooltips().anchor('top_right'))\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_density\n", "np.random.seed(43)\n", "dat={}\n", "dat['x'] = np.append(np.random.normal(0,1,1000), np.random.normal(3,1,500))\n", "dat['y'] = np.append(np.random.normal(0,1,1000), np.random.normal(3,1,500))\n", "\n", "\n", "ggplot(dat,aes('x')) + ggtitle('geom_density') + geom_density(tooltips=layer_tooltips().anchor('top_right'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_density2d\n", "ggplot(dat, aes('x', 'y')) + ggtitle('geom_density2d')\\\n", " + geom_density2d(aes(color='..level..'), tooltips=layer_tooltips().anchor('top_right'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_tile\n", "d={\n", " 'x': [1,2,3,4,5],\n", " 'y': [0,0,0,0,0],\n", " 'z': [-1,-0.5,0,0.5,1]\n", "}\n", "ggplot(d, aes('x', fill='z')) + ggtitle('geom_tile')\\\n", " + geom_tile(tooltips=layer_tooltips().anchor('top_center')) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_bin2d\n", "ggplot(dat, aes('x', 'y')) + ggtitle('geom_bin2d')\\\n", " + geom_bin2d(tooltips=layer_tooltips().anchor('top_right')) + ggtitle('geom_bin2d')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# polygon with points\n", "d1 = {\n", " 'x': [0.75, 1.75, 0.75, 1.75, 0.75, 1.75],\n", " 'y': [2.75, 2.75, 1.75, 1.75, 0.75, 0.35], \n", " 'group': [1, 1, 2, 2, 3, 3],\n", "}\n", "\n", "id = [\"1.1\", \"2.1\", \"1.2\", \"2.2\", \"1.3\", \"2.3\"]\n", "val = [3, 3.1, 3.1, 3.2, 3.15, 3.5]\n", "x = [2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3, 0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3]\n", "y = [-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5, 2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2]\n", "id4 = [v for v in id for _ in range(4)]\n", "val4 = [v for v in val for _ in range(4)]\n", "d2 = dict(id=id4, val=val4, x=x, y=y)\n", "\n", "ggplot(d2, aes(x, y)) + ggtitle('polygon with points') \\\n", " + geom_polygon(aes(fill='val', group='id'), tooltips=layer_tooltips().anchor('top_right'))\\\n", " + geom_point(data=d1, mapping=aes(x='x', y='y', color='group'), tooltips=layer_tooltips().anchor('top_right'))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### No crosshair" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_contourf\n", "(ggplot(c_dat, aes('x', 'y')) + ggtitle('geom_contourf')\n", " + scale_fill_gradient('green', 'red')\n", " + geom_contourf(aes(z='z', fill='..level..',alpha='..level..'),\n", " tooltips=layer_tooltips().anchor('top_right'))\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_density2df\n", "ggplot(dat, aes('x', 'y')) + ggtitle('geom_density2df')\\\n", " + geom_density2df(aes(fill = '..level..'), tooltips=layer_tooltips().anchor('top_right'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_boxplot\n", "(ggplot(mpg_df, aes('class', 'hwy')) \n", " + ggtitle('geom_boxplot')\n", " + geom_boxplot(tooltips=layer_tooltips()\n", " .anchor('top_center')\n", " .format('^Y', '.0f')\n", " .format('^middle', '.2f')\n", " .line('@|^middle')\n", " .line('lower/upper|^lower/^upper')\n", " .line('min/max|^ymin/^ymax'))\n", " )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_histogram + geom_vline\n", "np.random.seed(123)\n", "data = DataFrame(dict(\n", " cond=np.repeat(['A','B'], 200),\n", " rating=np.concatenate((np.random.normal(0, 1, 200), np.random.normal(.8, 1, 200)))\n", "))\n", "cdat = data.groupby(['cond'], as_index=False).mean()\n", "\n", "\n", "(ggplot(data, aes(x='rating', fill='cond')) + ggtitle('geom_histogram + geom_vline') \n", " + geom_histogram(binwidth=.5, alpha=.8, tooltips=layer_tooltips().anchor('top_right'))\n", " + geom_vline(data=cdat, \n", " mapping=aes(xintercept='rating'), \n", " linetype=\"longdash\", size=1, color=\"red\",\n", " tooltips=layer_tooltips().anchor('top_left'))\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tdata = dict(\n", " supp = ['OJ', 'OJ', 'OJ', 'VC', 'VC', 'VC'],\n", " dose = [0.5, 1.0, 2.0, 0.5, 1.0, 2.0],\n", " length = [13.23, 22.70, 26.06, 7.98, 16.77, 26.14],\n", " len_min = [11.83, 21.2, 24.50, 4.24, 15.26, 23.35],\n", " len_max = [15.63, 24.9, 27.11, 10.72, 19.28, 28.93]\n", ")\n", "\n", "# geom_errorbar\n", "(ggplot(tdata, aes(x='dose', color='supp')) \n", " + ggtitle('geom_errorbar')\n", " + geom_errorbar(aes(ymin='len_min', ymax='len_max'), width=.1,\n", " tooltips=layer_tooltips().line('len_min|^ymin').line('len_max|^ymax').anchor('top_left')) \n", " + geom_line(aes(y='length')) \n", " + geom_point(aes(y='length'))\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_crossbar\n", "(ggplot(tdata, aes(x='dose', color='supp'))\n", " + ggtitle('geom_crossbar')\n", " + geom_crossbar(aes(ymin='len_min', ymax='len_max', middle='length', color='supp'), fatten=5,\n", " tooltips=layer_tooltips()\n", " .line('len_min|^ymin')\n", " .line('|^middle')\n", " .line('len_max|^ymax')\n", " .anchor('middle_right'))\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#geom_bar\n", "(ggplot(tdata, aes(x='dose', color='supp')) \n", " + ggtitle('geom_bar')\n", " + geom_bar(aes(y='length', fill='supp'), stat='identity', position='dodge', color='black',\n", " tooltips=layer_tooltips().anchor('top_center'))\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_linerange\n", "(ggplot(tdata, aes(x='dose', color='supp'))\n", " + ggtitle('geom_linerange')\n", " + geom_linerange(aes(ymin='len_min', ymax='len_max', color='supp'), position=position_dodge(0.1), size=3,\n", " tooltips=layer_tooltips()\n", " .line('len_min|^ymin')\n", " .line('len_max|^ymax')\n", " .anchor('top_left'))\n", " + geom_line(aes(y='length'), position=position_dodge(0.1))\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geom_pointrange\n", "(ggplot(tdata, aes(x='dose', color='supp'))\n", " + ggtitle('geom_pointrange')\n", " + geom_pointrange(aes(y='length', ymin='len_min', ymax='len_max', color='supp'), position=position_dodge(0.1), size=3, shape=23, fatten=1,\n", " tooltips=layer_tooltips()\n", " .line('len_min|^ymin')\n", " .line('|^y')\n", " .line('len_max|^ymax')\n", " .anchor('top_left'))\n", " + geom_line(aes(y='length'), position=position_dodge(0.1))\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.7.10" } }, "nbformat": 4, "nbformat_minor": 4 }