{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "structural-artist", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "from lets_plot.mapping import as_discrete\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "id": "closed-interpretation", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "from pandas import DataFrame\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "funded-spending", "metadata": {}, "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", " \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 = pd.read_csv (\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "mpg.head(3)" ] }, { "cell_type": "code", "execution_count": 4, "id": "further-headquarters", "metadata": {}, "outputs": [], "source": [ "h = 420\n", "def bunch(p):\n", " bunch = GGBunch()\n", " bunch.add_plot(p, 0, 0)\n", " bunch.add_plot(p + coord_flip(), 0, h)\n", " bunch.show()" ] }, { "cell_type": "code", "execution_count": 5, "id": "technical-democracy", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "box_plot = (ggplot(mpg) + theme(axis_title_x='blank') + \n", " geom_boxplot(aes(as_discrete('class',order=1, order_by='..middle..'), 'hwy'),\n", " color=\"#579673\", fill=\"#9AC0B3\", size=1.5\n", " #tooltips=layer_tooltips().format('^Y', 'valus is\\n {}')\n", " ))\n", "bunch(box_plot)" ] }, { "cell_type": "code", "execution_count": null, "id": "located-tactics", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 6, "id": "distinct-faith", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "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", "\n", "bunch(ggplot(data, aes(x='cond', y='rating', color='cond')) + geom_boxplot())\n" ] }, { "cell_type": "code", "execution_count": null, "id": "inappropriate-island", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 7, "id": "painted-ending", "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", ")" ] }, { "cell_type": "code", "execution_count": 8, "id": "pending-linux", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# geom_errorbar\n", "errorbar = (ggplot(tdata, aes(x='dose', color='supp')) \n", " + geom_errorbar(aes(ymin='len_min', ymax='len_max'), width=.1) \n", " + geom_line(aes(y='length')) \n", " + geom_point(aes(y='length'))\n", ")\n", "bunch(errorbar)" ] }, { "cell_type": "code", "execution_count": 9, "id": "super-librarian", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(ggplot(tdata, aes(x='dose', color='supp'))\n", " + geom_errorbar(aes(ymin='len_min', ymax='len_max', group='supp'), color='black', width=.1, position= position_dodge(0.1))\n", " + geom_line(aes(y='length'), position= position_dodge(0.1))\n", " + geom_point(aes(y='length'), position= position_dodge(0.1), size=5)\n", " + coord_flip()\n", ")" ] }, { "cell_type": "code", "execution_count": 10, "id": "institutional-engagement", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pointrange = ggplot(tdata, aes(x='dose', color='supp')) \\\n", "+ xlab(\"Dose (mg)\") \\\n", "+ ylab(\"Tooth length (mm)\") \\\n", "+ scale_color_manual(['orange', 'dark_green'], na_value='gray') \\\n", "+ geom_pointrange(aes(y='length', ymin='len_min', ymax='len_max', color='supp'), position=position_dodge(0.1)) \\\n", "+ geom_line(aes(y='length'), position=position_dodge(0.1))\n", "\n", "bunch(pointrange)" ] }, { "cell_type": "code", "execution_count": null, "id": "homeless-measurement", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 11, "id": "interracial-consultation", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "crossbar = ( \n", " ggplot(tdata, aes(x='dose', color='supp')) \n", "+ xlab(\"Dose (mg)\") \n", "+ ylab(\"Tooth length (mm)\") \n", "+ scale_color_manual(['orange', 'dark_green'], na_value='gray') \n", "+ geom_crossbar(aes(ymin='len_min', ymax='len_max', middle='length', color='supp'), fatten=5)\n", ")\n", "bunch(crossbar)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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": 5 }