{ "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" }, { "data": { "text/html": [ " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use dataframe\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.4.2. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.0.0." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo()" ] }, { "cell_type": "markdown", "id": "91a3bca2", "metadata": {}, "source": [ "## Vertical, horizontal and oblique lines" ] }, { "cell_type": "code", "execution_count": 3, "id": "b62278c4", "metadata": {}, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":5,\"ncol\":12,\"columns\":[\"untitled\",\"manufacturer\",\"model\",\"displ\",\"year\",\"cyl\",\"trans\",\"drv\",\"cty\",\"hwy\",\"fl\",\"class\"],\"kotlin_dataframe\":[{\"untitled\":1,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"auto(l5)\",\"drv\":\"f\",\"cty\":18,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":2,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"manual(m5)\",\"drv\":\"f\",\"cty\":21,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":3,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.0,\"year\":2008,\"cyl\":4,\"trans\":\"manual(m6)\",\"drv\":\"f\",\"cty\":20,\"hwy\":31,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":4,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.0,\"year\":2008,\"cyl\":4,\"trans\":\"auto(av)\",\"drv\":\"f\",\"cty\":21,\"hwy\":30,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":5,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.8,\"year\":1999,\"cyl\":6,\"trans\":\"auto(l5)\",\"drv\":\"f\",\"cty\":16,\"hwy\":26,\"fl\":\"p\",\"class\":\"compact\"}]}", "text/html": [ " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "

DataFrame: rowsCount = 5, columnsCount = 12

\n", " \n", " \n", " " ] }, "execution_count": 3, "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": 4, "id": "f395c360", "metadata": {}, "outputs": [], "source": [ "val mpgMap = mpgDf.toMap()" ] }, { "cell_type": "code", "execution_count": 5, "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.cty.median()\n", "val hwyMedian = mpgDf.hwy.median()" ] }, { "cell_type": "code", "execution_count": 6, "id": "ac17c9ed", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "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": 7, "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": 8, "id": "e89d3e81", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(pDataMap) + geomLine() { x = \"x\"; y = \"y\" }" ] }, { "cell_type": "code", "execution_count": 9, "id": "be4d7ac2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(pDataMap) + geomPath() { x = \"x\"; y = \"y\" }" ] }, { "cell_type": "code", "execution_count": 10, "id": "de850faa", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "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": 11, "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": 12, "id": "b3bb2266", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(aDataMap) +\n", " geomLine() { x = \"x\"; y = \"y\" } +\n", " ggtitle(\"Plotting with geomLine()\")" ] }, { "cell_type": "code", "execution_count": 13, "id": "2b5db95c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 13, "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": 14, "id": "a02fac5f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 14, "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.8.20" } }, "nbformat": 4, "nbformat_minor": 5 }