{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:28:59.747542Z", "iopub.status.busy": "2024-04-17T07:28:59.747461Z", "iopub.status.idle": "2024-04-17T07:29:00.067332Z", "shell.execute_reply": "2024-04-17T07:29:00.067114Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "from lets_plot import *\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:00.081010Z", "iopub.status.busy": "2024-04-17T07:29:00.080830Z", "iopub.status.idle": "2024-04-17T07:29:00.082544Z", "shell.execute_reply": "2024-04-17T07:29:00.082357Z" } }, "outputs": [], "source": [ "np.random.seed(42)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:00.083747Z", "iopub.status.busy": "2024-04-17T07:29:00.083592Z", "iopub.status.idle": "2024-04-17T07:29:00.086160Z", "shell.execute_reply": "2024-04-17T07:29:00.085957Z" } }, "outputs": [], "source": [ "cov0=[[1, -.8], \n", " [-.8, 1]] \n", "cov1=[[1, .8], \n", " [.8, 1]] \n", "cov2=[[ 10, .1],\n", " [.1, .1]]\n", "\n", "x0, y0 = np.random.multivariate_normal(mean=[-2,0], cov=cov0, size=400).T\n", "x1, y1 = np.random.multivariate_normal(mean=[2,0], cov=cov1, size=400).T\n", "x2, y2 = np.random.multivariate_normal(mean=[0,1], cov=cov2, size=400).T\n", "\n", "data = dict(\n", " x = np.concatenate((x0,x1,x2)),\n", " y = np.concatenate((y0,y1,y2))\n", ")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:00.087193Z", "iopub.status.busy": "2024-04-17T07:29:00.087077Z", "iopub.status.idle": "2024-04-17T07:29:00.123521Z", "shell.execute_reply": "2024-04-17T07:29:00.123325Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(data, aes('x', 'y')) + ggsize(600,300) \\\n", "+ geom_point(color='black', alpha=.1)\n", "p" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:00.124443Z", "iopub.status.busy": "2024-04-17T07:29:00.124353Z", "iopub.status.idle": "2024-04-17T07:29:00.289401Z", "shell.execute_reply": "2024-04-17T07:29:00.289185Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Basic density \n", "p + geom_density2d(color='red')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:00.290694Z", "iopub.status.busy": "2024-04-17T07:29:00.290611Z", "iopub.status.idle": "2024-04-17T07:29:00.460602Z", "shell.execute_reply": "2024-04-17T07:29:00.460365Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Set contour color by \"level\".\n", "# - change defailt position and size of colorbar.\n", "p + geom_density2d(aes(color='..level..')) \\\n", "+ scale_color_gradient(low='dark_green', high='yellow', guide=guide_colorbar(barheight=10, barwidth=300)) \\\n", "+ theme(legend_position='bottom')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Filling contours by level" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:00.462269Z", "iopub.status.busy": "2024-04-17T07:29:00.462109Z", "iopub.status.idle": "2024-04-17T07:29:00.631975Z", "shell.execute_reply": "2024-04-17T07:29:00.631766Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 = ggplot(data, aes('x', 'y')) + ggsize(600,300)\n", "\n", "# Filled polygons are not always working well - see the missing polygon in the middle. \n", "p1 + geom_polygon(aes(fill='..level..'), stat='density2d') + coord_fixed()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:00.633503Z", "iopub.status.busy": "2024-04-17T07:29:00.633401Z", "iopub.status.idle": "2024-04-17T07:29:00.824968Z", "shell.execute_reply": "2024-04-17T07:29:00.824739Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 'density2df' is not dependent on the poligons order and works a lot better.\n", "p1 + geom_density2df(aes(fill='..level..'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### geom_bin2d is another way to plot density" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:00.827450Z", "iopub.status.busy": "2024-04-17T07:29:00.827366Z", "iopub.status.idle": "2024-04-17T07:29:00.832220Z", "shell.execute_reply": "2024-04-17T07:29:00.832034Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 + geom_bin2d()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:00.833351Z", "iopub.status.busy": "2024-04-17T07:29:00.833228Z", "iopub.status.idle": "2024-04-17T07:29:00.836985Z", "shell.execute_reply": "2024-04-17T07:29:00.836814Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Adjust the tile size - make them square and bigger.\n", "# Show density instead of count.\n", "p1 + geom_bin2d(aes(fill='..density..'), binwidth=[1, 1])" ] } ], "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": 2 }