{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot\n", "\n", "@file:DependsOn(\"org.apache.commons:commons-math3:3.6.1\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "import org.apache.commons.math3.distribution.MultivariateNormalDistribution" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "val cov0 : Array = arrayOf(doubleArrayOf(1.0, -.8),\n", " doubleArrayOf(-.8, 1.0))\n", "\n", "val cov1 : Array = arrayOf(doubleArrayOf(1.0, .8),\n", " doubleArrayOf(.8, 1.0))\n", "\n", "val cov2 : Array = arrayOf(doubleArrayOf(10.0, .1),\n", " doubleArrayOf(.1, .1))\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "val n = 400\n", "\n", "val means0 : DoubleArray = doubleArrayOf(-2.0, 0.0)\n", "val means1 : DoubleArray = doubleArrayOf(2.0, 0.0)\n", "val means2 : DoubleArray = doubleArrayOf(0.0, 1.0)\n", "\n", "val xy0 = MultivariateNormalDistribution(means0, cov0).sample(n)\n", "val xy1 = MultivariateNormalDistribution(means1, cov1).sample(n)\n", "val xy2 = MultivariateNormalDistribution(means2, cov2).sample(n)\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "val data = mapOf(\n", " \"x\" to (xy0.map { it[0] } + xy1.map { it[0] } + xy2.map { it[0] }).toList(),\n", " \"y\" to (xy0.map { it[1] } + xy1.map { it[1] } + xy2.map { it[1] }).toList()\n", ")\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot(data) {x=\"x\"; y=\"y\"} + ggsize(600,300) +\n", " geomPoint(color=\"black\", alpha=.1)\n", "p" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Basic density \n", "p + geomDensity2D(color=\"red\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Set contour color by level\n", "// - change defailt position and size of colorbar\n", "p + geomDensity2D {color=\"..level..\"} +\n", " scaleColorGradient(low=\"dark_green\", high=\"yellow\", guide=guideColorbar(barHeight=10, barWidth=300)) +\n", " theme().legendPositionBottom()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Filling contours by level" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p1 = letsPlot(data) {x=\"x\"; y=\"y\"} + ggsize(600,300)\n", "\n", "// Filled polygons are not always working well - note missing polygons in the middle. \n", "p1 + geomPolygon(stat=Stat.density2D()) {fill=\"..level..\"} + coordFixed()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// 'geomDensity2DFilled' is not dependent on poligons order and works a lot better\n", "p1 + geomDensity2DFilled {fill=\"..level..\"}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### geomBin2D is another way to plot density" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 + geomBin2D()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Adjust the tile size - make them square and bigger.\n", "// Show density instead of count.\n", "p1 + geomBin2D(binWidth=1 to 1) {fill=\"..density..\"}" ] } ], "metadata": { "kernelspec": { "display_name": "Kotlin", "language": "kotlin", "name": "kotlin" }, "language_info": { "codemirror_mode": "text/x-kotlin", "file_extension": ".kt", "mimetype": "text/x-kotlin", "name": "kotlin", "nbconvert_exporter": "", "pygments_lexer": "kotlin", "version": "1.8.20" } }, "nbformat": 4, "nbformat_minor": 4 }