{ "cells": [ { "cell_type": "markdown", "id": "872680a6", "metadata": {}, "source": [ "# Lines in Lets-Plot" ] }, { "cell_type": "code", "execution_count": 1, "id": "9c24c1d2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "// %use krangl\n", "%use kotlin-statistics\n", "%use lets-plot" ] }, { "cell_type": "code", "execution_count": 2, "id": "encouraging-limitation", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.1.1. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.2.5.1." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo() // This prevents Krangl from loading an obsolete version of Lets-Plot classes." ] }, { "cell_type": "code", "execution_count": 3, "id": "random-foster", "metadata": {}, "outputs": [], "source": [ "%use krangl" ] }, { "cell_type": "markdown", "id": "91a3bca2", "metadata": {}, "source": [ "## Vertical, horizontal and oblique lines" ] }, { "cell_type": "code", "execution_count": 4, "id": "b62278c4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
manufacturermodeldisplyearcyltransdrvctyhwyflclass
1audia41.819994auto(l5)f1829pcompact
2audia41.819994manual(m5)f2129pcompact
3audia42.020084manual(m6)f2031pcompact
4audia42.020084auto(av)f2130pcompact
5audia42.819996auto(l5)f1626pcompact

Shape: 5 x 12. \n", "

" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val mpgDf = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv\")\n", "mpgDf.head()\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "f395c360", "metadata": {}, "outputs": [], "source": [ "val mpgMap = mpgDf.toMap()" ] }, { "cell_type": "code", "execution_count": 6, "id": "781fdc93", "metadata": {}, "outputs": [], "source": [ "val regModel = (mpgMap[\"cty\"]!! zip mpgMap[\"hwy\"]!!).simpleRegression(\n", " xSelector = { it.first as Number },\n", " ySelector = { it.second as Number }\n", ")\n", "val ctyMedian = mpgDf.get(\"cty\").median()\n", "val hwyMedian = mpgDf.get(\"hwy\").median()" ] }, { "cell_type": "code", "execution_count": 7, "id": "ac17c9ed", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpgMap) { x = \"cty\"; y = \"hwy\" } +\n", " geomPoint() +\n", " geomVLine(xintercept = ctyMedian, color = \"#756bb1\", linetype = \"dashed\") +\n", " geomHLine(yintercept = hwyMedian, color = \"#756bb1\", linetype = \"dashed\") +\n", " geomABLine(slope = regModel.slope, intercept = regModel.intercept, color = \"#de2d26\") +\n", " themeMinimal()" ] }, { "cell_type": "markdown", "id": "4211165f", "metadata": {}, "source": [ "## Broken lines" ] }, { "cell_type": "code", "execution_count": 8, "id": "d1c840c9", "metadata": {}, "outputs": [], "source": [ "fun generateParabolicDataMap(n: Int = 25, a: Double = 1.0): Map> {\n", " val rand = java.util.Random(42)\n", " val x = List(2 * n + 1) { i -> a * (i - n).toDouble() / n }\n", " val y = x.map { i -> i * i + rand.nextGaussian() }\n", " return mapOf(\"x\" to x, \"y\" to y)\n", "}\n", "\n", "val pDataMap = generateParabolicDataMap(a = 3.0)" ] }, { "cell_type": "code", "execution_count": 9, "id": "e89d3e81", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(pDataMap) + geomLine() { x = \"x\"; y = \"y\" }" ] }, { "cell_type": "code", "execution_count": 10, "id": "be4d7ac2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(pDataMap) + geomPath() { x = \"x\"; y = \"y\" }" ] }, { "cell_type": "code", "execution_count": 11, "id": "de850faa", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(pDataMap) + geomStep() { x = \"x\"; y = \"y\" }" ] }, { "cell_type": "markdown", "id": "5ebb12bf", "metadata": {}, "source": [ "And what is the difference between `geomLine()` and `geomPath()`?\n", "\n", "Let's have a look at the following example:" ] }, { "cell_type": "code", "execution_count": 12, "id": "ec5263cf", "metadata": {}, "outputs": [], "source": [ "fun generateArchimedeanDataMap(n: Int = 25, k: Double = 1.0, a: Double = 1.0): Map> {\n", " val phi = List(n) { i -> 2.0 * PI * k * i.toDouble() / (n - 1) }\n", " val r = phi.map { angle -> (a * angle) / (2.0 * PI) }\n", " val x = (r zip phi).map { p -> p.first * cos(p.second) }\n", " val y = (r zip phi).map { p -> p.first * sin(p.second) }\n", " return mapOf(\"x\" to x, \"y\" to y)\n", "}\n", "\n", "val aDataMap = generateArchimedeanDataMap(n = 200, k = 2.0)" ] }, { "cell_type": "code", "execution_count": 13, "id": "b3bb2266", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(aDataMap) +\n", " geomLine() { x = \"x\"; y = \"y\" } +\n", " ggtitle(\"Plotting with geomLine()\")" ] }, { "cell_type": "code", "execution_count": 14, "id": "2b5db95c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(aDataMap) +\n", " geomPath() { x = \"x\"; y = \"y\" } +\n", " ggtitle(\"Plotting with geomPath()\")" ] }, { "cell_type": "markdown", "id": "96c52474", "metadata": {}, "source": [ "Also an interesting plot could be drawed with `geomSegment()`:" ] }, { "cell_type": "code", "execution_count": 15, "id": "a02fac5f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(generateArchimedeanDataMap(n = 50)) + geomSegment(xend = 0.0, yend = 0.0) { x = \"x\"; y = \"y\" }" ] } ], "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.7.20-dev-1299" } }, "nbformat": 4, "nbformat_minor": 5 }