{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot\n", "\n", "import kotlin.math.*" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "// This example was found at: \n", "// https://jakevdp.github.io/PythonDataScienceHandbook/04.04-density-and-contour-plots.html\n", "\n", "fun meshgridPoints(x:List, y:List):List> {\n", " val xSer = y.flatMap { x }\n", " var yInd = -1\n", " val ySer = y.flatMap { \n", " yInd++\n", " List(x.size) {y[yInd]} \n", " }\n", " return xSer.zip(ySer)\n", "}\n", "\n", "val gridPoints = meshgridPoints(\n", " generateSequence(0.0, {it + 6.0/50} ).takeWhile { it <= 5.0 }.toList(),\n", " generateSequence(0.0, {it + 6.0/40} ).takeWhile { it <= 5.0 }.toList())\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "fun f(x:Double, y:Double): Double {\n", " return sin(x).pow(10) + cos(10 + y * x) * cos(x)\n", "}\n", "\n", "val X = gridPoints.map { it.first }\n", "val Y = gridPoints.map { it.second }\n", "val Z = gridPoints.map { f(it.first, it.second) }" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot {x=X; y=Y; fill=Z}\n", "p + geomTile() + scaleFillHue()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Lets use greyscale and also remove colorbar.\n", "val p1 = p + geomTile(alpha=.5) + scaleFillGrey(1, .2, guide=\"none\")\n", "p1" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Add contours\n", "p1 + geomContour(color=\"red\") {z=Z}" ] }, { "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", "p1 + geomContour {z=Z; color=\"..level..\"} + scaleColorGradient(low=\"dark_green\", high=\"yellow\")" ] }, { "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 p2 = letsPlot {x=X; y=Y}\n", "\n", "// Filled polygons are not working very well in this case\n", "p2 + geomPolygon(stat=Stat.contour {z=Z}) {fill=\"..level..\"}" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// 'contourf' geom works a lot better:\n", "p2 + geomContourFilled {z=Z; fill=\"..level..\"}" ] } ], "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 }