{ "cells": [ { "cell_type": "markdown", "id": "150dc00f", "metadata": {}, "source": [ "# `geom_function()`" ] }, { "cell_type": "code", "execution_count": 1, "id": "9b4af9d3", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from scipy.stats import norm\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "fc1b5d6d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "8c7aada0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_function(fun=norm.pdf, xlim=[-3, 3])" ] }, { "cell_type": "markdown", "id": "24bc3634", "metadata": {}, "source": [ "### 1. Comparison With Empirical Density" ] }, { "cell_type": "code", "execution_count": 4, "id": "22fd1b16", "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "\n", "xs = df[\"cty\"]\n", "\n", "loc, scale = xs.mean(), xs.std()\n", "xmin, xmax = xs.min(), xs.max()\n", "\n", "fun = lambda t: norm.pdf(t, loc, scale)" ] }, { "cell_type": "code", "execution_count": 5, "id": "759eed16", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(\"cty\")) + \\\n", " geom_density() + \\\n", " geom_function(fun=fun, xlim=[xmin, xmax], color=\"red\")" ] }, { "cell_type": "markdown", "id": "955951bf", "metadata": {}, "source": [ "### 2. Use Data to Autodetect `xlim`" ] }, { "cell_type": "code", "execution_count": 6, "id": "216f5b42", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(\"cty\")) + \\\n", " geom_density() + \\\n", " geom_function(aes(\"cty\"), data=df, fun=fun, color=\"red\")" ] }, { "cell_type": "markdown", "id": "64f599ee", "metadata": {}, "source": [ "### 3. Composition of Function and Statistic" ] }, { "cell_type": "code", "execution_count": 7, "id": "e79810ce", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(\"cty\")) + \\\n", " geom_density() + \\\n", " geom_function(fun=fun, xlim=[xmin, xmax], \\\n", " stat='smooth', method='loess', span=.3, color=\"red\")" ] }, { "cell_type": "markdown", "id": "f018193d", "metadata": {}, "source": [ "### 4. Use Different Geometry" ] }, { "cell_type": "code", "execution_count": 8, "id": "1cd6c684", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(\"cty\")) + \\\n", " geom_density() + \\\n", " geom_function(aes(fill='y'), fun=fun, xlim=[xmin, xmax], n=30, \\\n", " geom='histogram', alpha=.5) + \\\n", " scale_fill_brewer(type='seq', palette='YlOrRd', trans='reverse')" ] } ], "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.8.17" } }, "nbformat": 4, "nbformat_minor": 5 }