{ "cells": [ { "cell_type": "markdown", "id": "d69298e0", "metadata": {}, "source": [ "# `stat_summary_bin()`" ] }, { "cell_type": "code", "execution_count": 1, "id": "53ff73b0", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:06:43.679909Z", "iopub.status.busy": "2024-04-26T12:06:43.679909Z", "iopub.status.idle": "2024-04-26T12:06:44.718209Z", "shell.execute_reply": "2024-04-26T12:06:44.717206Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "49b0e7d0", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:06:44.718209Z", "iopub.status.busy": "2024-04-26T12:06:44.718209Z", "iopub.status.idle": "2024-04-26T12:06:44.733351Z", "shell.execute_reply": "2024-04-26T12:06:44.733351Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "5cac34da", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:06:44.733351Z", "iopub.status.busy": "2024-04-26T12:06:44.733351Z", "iopub.status.idle": "2024-04-26T12:06:44.859757Z", "shell.execute_reply": "2024-04-26T12:06:44.858935Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(150, 5)\n" ] }, { "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", "
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa
\n", "
" ], "text/plain": [ " sepal_length sepal_width petal_length petal_width species\n", "0 5.1 3.5 1.4 0.2 setosa\n", "1 4.9 3.0 1.4 0.2 setosa\n", "2 4.7 3.2 1.3 0.2 setosa\n", "3 4.6 3.1 1.5 0.2 setosa\n", "4 5.0 3.6 1.4 0.2 setosa" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/iris.csv\")\n", "print(df.shape)\n", "df.head()" ] }, { "cell_type": "markdown", "id": "33fc0877", "metadata": {}, "source": [ "#### 1. The `geom` Parameter" ] }, { "cell_type": "code", "execution_count": 4, "id": "170b6df8", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:06:44.859757Z", "iopub.status.busy": "2024-04-26T12:06:44.859757Z", "iopub.status.idle": "2024-04-26T12:06:44.969559Z", "shell.execute_reply": "2024-04-26T12:06:44.968463Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(\"petal_length\", \"sepal_length\")) + \\\n", " stat_summary_bin(geom='crossbar', width=1, binwidth=.5) + \\\n", " geom_point(shape=21)" ] }, { "cell_type": "markdown", "id": "f390af39", "metadata": {}, "source": [ "#### 2. `fun`, `fun_min`, `fun_max` Parameters" ] }, { "cell_type": "code", "execution_count": 5, "id": "5afddb4c", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:06:44.969559Z", "iopub.status.busy": "2024-04-26T12:06:44.969559Z", "iopub.status.idle": "2024-04-26T12:06:44.985702Z", "shell.execute_reply": "2024-04-26T12:06:44.984676Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(\"petal_length\", \"sepal_length\")) + \\\n", " stat_summary_bin(geom='crossbar', width=1, binwidth=.5, \\\n", " fun='mq', fun_min='lq', fun_max='uq') + \\\n", " geom_point(shape=21)" ] }, { "cell_type": "markdown", "id": "92de60d3", "metadata": {}, "source": [ "#### 3. The `quantiles` Parameter" ] }, { "cell_type": "code", "execution_count": 6, "id": "c05e10ec", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:06:44.985702Z", "iopub.status.busy": "2024-04-26T12:06:44.985702Z", "iopub.status.idle": "2024-04-26T12:06:45.000429Z", "shell.execute_reply": "2024-04-26T12:06:45.000429Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(\"petal_length\", \"sepal_length\")) + \\\n", " stat_summary_bin(geom='crossbar', width=1, binwidth=.5, \\\n", " fun='mq', fun_min='lq', fun_max='uq', \\\n", " quantiles=[.05, .5, .95]) + \\\n", " geom_point(shape=21)" ] }, { "cell_type": "markdown", "id": "546b0bc9", "metadata": {}, "source": [ "#### 4. `center` and `boundary` Parameters" ] }, { "cell_type": "code", "execution_count": 7, "id": "22c88593", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:06:45.000429Z", "iopub.status.busy": "2024-04-26T12:06:45.000429Z", "iopub.status.idle": "2024-04-26T12:06:45.031909Z", "shell.execute_reply": "2024-04-26T12:06:45.031909Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def get_plot(center=None, boundary=None):\n", " return ggplot(df, aes(\"petal_length\", \"sepal_length\")) + \\\n", " stat_summary_bin(geom='crossbar', width=1, binwidth=.5, \\\n", " center=center, boundary=boundary) + \\\n", " geom_point(shape=21) + \\\n", " ggtitle(\"center={0}, boundary={1}\".format(center, boundary))\n", "\n", "gggrid([\n", " get_plot(),\n", " get_plot(center=1),\n", " get_plot(boundary=1),\n", "], ncol=2)" ] } ], "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": 5 }