{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This page is available as an executable or viewable Jupyter Notebook:\n", "

\n", " \n", " \n", "\n", " \n", " \n", "\n", "
\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%use lets-plot\n", "%use numpy" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "import org.jetbrains.numkt.core.*\n", "import org.jetbrains.numkt.math.*\n", "import org.jetbrains.numkt.random.Random\n", "import org.jetbrains.numkt.*" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "val cov0 = array(listOf(listOf(1, -.8),\n", " listOf(-.8, 1)))\n", "val cov1 = array(listOf(listOf(1, .8),\n", " listOf(.8, 1)))\n", "val cov2 = array(listOf(listOf(10, .1),\n", " listOf(.1, .1)))\n", "\n", "val n = intArrayOf(400)\n", "val xy0 = Random.multivariateNormal(mean=array(listOf(-2,0)), \n", " cov=cov0, \n", " size=n,\n", " checkValid=\"raise\",\n", " tol=1e-8\n", " ).transpose()\n", "val xy1 = Random.multivariateNormal(mean=array(listOf(2,0)), \n", " cov=cov1, \n", " size=n,\n", " checkValid=\"raise\",\n", " tol=1e-8\n", " ).transpose()\n", "val xy2 = Random.multivariateNormal(mean=array(listOf(0,1)), \n", " cov=cov2, \n", " size=n,\n", " checkValid=\"raise\",\n", " tol=1e-8\n", " ).transpose()\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "val data = mapOf(\n", " \"x\" to concatenate(xy0[0], xy1[0], xy2[0]).toList(),\n", " \"y\" to concatenate(xy0[1], xy1[1], xy2[1]).toList()\n", ")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = ggplot(data) {x=\"x\"; y=\"y\"} + ggsize(600,300) +\n", " geom_point(color=\"black\", alpha=.1)\n", "p" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Basic density \n", "p + geom_density2d(color=\"red\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Set contour color by level\n", "// - change defailt position and size of colorbar\n", "p + geom_density2d {color=\"..level..\"} +\n", " scale_color_gradient(low=\"dark_green\", high=\"yellow\", guide=guide_colorbar(barHeight=10, barWidth=300)) +\n", " theme().legendPosition_bottom()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Filling contours by level" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p1 = ggplot(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 + geom_polygon(stat=Stat.density2d()) {fill=\"..level..\"} + coord_fixed()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// 'density2df' is not dependent on poligons order and works a lot better\n", "p1 + geom_density2df {fill=\"..level..\"}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### geom_bin2d is another way to plot density" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 + geom_bin2d()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "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(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", "pygments_lexer": "kotlin", "version": "1.4.20-dev-2342" }, "pycharm": { "stem_cell": { "cell_type": "raw", "metadata": { "collapsed": false }, "source": [] } } }, "nbformat": 4, "nbformat_minor": 4 }