{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "private-kingston", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T11:05:06.747237Z", "iopub.status.busy": "2024-08-23T11:05:06.745319Z", "iopub.status.idle": "2024-08-23T11:05:09.372377Z", "shell.execute_reply": "2024-08-23T11:05:09.372113Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot\n", "%use dataframe" ] }, { "cell_type": "code", "execution_count": 2, "id": "aggressive-identifier", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T11:05:09.374162Z", "iopub.status.busy": "2024-08-23T11:05:09.373909Z", "iopub.status.idle": "2024-08-23T11:05:09.409819Z", "shell.execute_reply": "2024-08-23T11:05:09.409990Z" } }, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.7.2. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.3.2." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo()" ] }, { "cell_type": "code", "execution_count": 3, "id": "informational-payroll", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T11:05:09.412130Z", "iopub.status.busy": "2024-08-23T11:05:09.411858Z", "iopub.status.idle": "2024-08-23T11:05:09.578798Z", "shell.execute_reply": "2024-08-23T11:05:09.578967Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot() + geomLabel(x=0, y=0, label=\"Lorem ipsum\", size=14) + ggsize(500, 200)" ] }, { "cell_type": "code", "execution_count": 4, "id": "individual-congress", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T11:05:09.581154Z", "iopub.status.busy": "2024-08-23T11:05:09.580369Z", "iopub.status.idle": "2024-08-23T11:05:09.646736Z", "shell.execute_reply": "2024-08-23T11:05:09.646538Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot() + geomLabel( \n", " x = 0, y = 0,\n", " label = \"Lorem ipsum\",\n", " size = 14,\n", " fill = \"#edf8e9\",\n", " color = \"#238b45\",\n", " fontface = \"bold\",\n", " labelPadding = 1.0,\n", " labelR = 0.5,\n", " labelSize = 2.0\n", ") + ggsize(500, 200)" ] }, { "cell_type": "code", "execution_count": 5, "id": "super-employer", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T11:05:09.648743Z", "iopub.status.busy": "2024-08-23T11:05:09.648295Z", "iopub.status.idle": "2024-08-23T11:05:09.779887Z", "shell.execute_reply": "2024-08-23T11:05:09.779572Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val families = listOf(\n", " \"Arial\",\n", " \"Calibri\",\n", " \"Garamond\",\n", " \"Geneva\",\n", " \"Georgia\",\n", " \"Helvetica\",\n", " \"Lucida Grande\",\n", " \"Rockwell\",\n", " \"Times New Roman\",\n", " \"Verdana\",\n", " \"sans-serif\",\n", " \"serif\",\n", " \"monospace\"\n", ")\n", "\n", "letsPlot() + geomLabel(size = 10, labelPadding = 0, labelR = 0) {\n", " y = IntArray(families.size) { it };\n", " label = families;\n", " family = families\n", "}" ] }, { "cell_type": "code", "execution_count": 6, "id": "shaped-internet", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T11:05:09.781698Z", "iopub.status.busy": "2024-08-23T11:05:09.781241Z", "iopub.status.idle": "2024-08-23T11:05:10.029047Z", "shell.execute_reply": "2024-08-23T11:05:10.028800Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val justifications = listOf(0, 0.5, 1)\n", "val angles = listOf(0, 45, 90)\n", "\n", "val values = justifications.flatMap { hjust ->\n", " justifications.flatMap { vjust ->\n", " angles.map { angle ->\n", " Triple(hjust, vjust, angle)\n", " }\n", " }\n", "}\n", "\n", "val data = mapOf(\n", " \"hjust\" to values.map { it.first },\n", " \"vjust\" to values.map { it.second },\n", " \"angle\" to values.map { it.third }\n", ")\n", "\n", "letsPlot(data) { x = \"hjust\"; y = \"vjust\" } +\n", " geomPoint(size = 3) +\n", " geomLabel(label = \"Text\", size = 9) { hjust = \"hjust\"; vjust = \"vjust\"; angle = \"angle\" } +\n", " facetGrid(y = \"angle\", yFormat = \"{d}°\") +\n", " scaleXContinuous(breaks = listOf(0, 0.5, 1), expand = listOf(0.1)) +\n", " scaleYContinuous(breaks = listOf(0, 0.5, 1), expand = listOf(0.0, 0.5)) +\n", " themeClassic() + theme(panelBorder = elementRect(size = 1))" ] }, { "cell_type": "code", "execution_count": 7, "id": "under-petersburg", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T11:05:10.030616Z", "iopub.status.busy": "2024-08-23T11:05:10.030296Z", "iopub.status.idle": "2024-08-23T11:05:10.453384Z", "shell.execute_reply": "2024-08-23T11:05:10.453563Z" } }, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":3,\"ncol\":9,\"columns\":[\"miles per gallon\",\"number of cylinders\",\"engine displacement (cu. inches)\",\"engine horsepower\",\"vehicle weight (lbs.)\",\"time to accelerate (sec.)\",\"model year\",\"origin of car\",\"vehicle name\"],\"kotlin_dataframe\":[{\"miles per gallon\":18.0,\"number of cylinders\":8,\"engine displacement (cu. inches)\":307.0,\"engine horsepower\":130,\"vehicle weight (lbs.)\":3504,\"time to accelerate (sec.)\":12.0,\"model year\":70,\"origin of car\":\"US\",\"vehicle name\":\"chevrolet chevelle malibu\"},{\"miles per gallon\":15.0,\"number of cylinders\":8,\"engine displacement (cu. inches)\":350.0,\"engine horsepower\":165,\"vehicle weight (lbs.)\":3693,\"time to accelerate (sec.)\":11.5,\"model year\":70,\"origin of car\":\"US\",\"vehicle name\":\"buick skylark 320\"},{\"miles per gallon\":18.0,\"number of cylinders\":8,\"engine displacement (cu. inches)\":318.0,\"engine horsepower\":150,\"vehicle weight (lbs.)\":3436,\"time to accelerate (sec.)\":11.0,\"model year\":70,\"origin of car\":\"US\",\"vehicle name\":\"plymouth satellite\"}]}", "text/html": [ " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "

DataFrame: rowsCount = 3, columnsCount = 9

\n", "
miles per gallonnumber of cylindersengine displacement (cu. inches)engine horsepowervehicle weight (lbs.)time to accelerate (sec.)model yearorigin of carvehicle name
18.0000008307.000000130350412.00000070USchevrolet chevelle malibu
15.0000008350.000000165369311.50000070USbuick skylark 320
18.0000008318.000000150343611.00000070USplymouth satellite
\n", " \n", " \n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val mpg = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg2.csv\")\n", "mpg.head(3)" ] }, { "cell_type": "code", "execution_count": 8, "id": "extensive-democrat", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T11:05:10.455244Z", "iopub.status.busy": "2024-08-23T11:05:10.454995Z", "iopub.status.idle": "2024-08-23T11:05:10.521475Z", "shell.execute_reply": "2024-08-23T11:05:10.521648Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val p = letsPlot(mpg.head(30).toMap()) { \n", " x = \"vehicle weight (lbs.)\"\n", " y = \"miles per gallon\"\n", " label = \"vehicle name\"\n", "}\n", "\n", "p + geomLabel()" ] }, { "cell_type": "code", "execution_count": 9, "id": "intermediate-bridal", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T11:05:10.523948Z", "iopub.status.busy": "2024-08-23T11:05:10.523685Z", "iopub.status.idle": "2024-08-23T11:05:10.577702Z", "shell.execute_reply": "2024-08-23T11:05:10.577812Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geomLabel(color = \"white\", fontface = \"bold\") {\n", " fill = asDiscrete(\"number of cylinders\", order = 1)\n", "} + ggsize(800,400)" ] }, { "cell_type": "code", "execution_count": 10, "id": "continent-nudist", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T11:05:10.579783Z", "iopub.status.busy": "2024-08-23T11:05:10.579478Z", "iopub.status.idle": "2024-08-23T11:05:12.646444Z", "shell.execute_reply": "2024-08-23T11:05:12.646177Z" } }, "outputs": [], "source": [ "// Initialize Lets-Plot GeoTools extension. \n", "%use lets-plot-gt(gt=\"[23,)\")" ] }, { "cell_type": "code", "execution_count": 11, "id": "speaking-massage", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T11:05:12.648118Z", "iopub.status.busy": "2024-08-23T11:05:12.647865Z", "iopub.status.idle": "2024-08-23T11:05:17.081785Z", "shell.execute_reply": "2024-08-23T11:05:17.081441Z" } }, "outputs": [], "source": [ "@file:DependsOn(\"org.geotools:gt-shapefile:[23,)\")\n", "@file:DependsOn(\"org.geotools:gt-cql:[23,)\")\n", "\n", "import org.geotools.data.shapefile.ShapefileDataStoreFactory\n", "import org.geotools.data.simple.SimpleFeatureCollection\n", "import java.net.URL\n", "\n", "import org.geotools.filter.text.cql2.CQL\n", "\n", "\n", "val factory = ShapefileDataStoreFactory()\n", "\n", "val worldFeatures : SimpleFeatureCollection = with(\"naturalearth_lowres\") {\n", " val url = \"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/shp/${this}/${this}.shp\"\n", " factory.createDataStore(URL(url)).featureSource.features\n", "}\n", "val europe = worldFeatures.subCollection(CQL.toFilter(\"continent = 'Europe'\"))\n", "\n", "val cityFeatures : SimpleFeatureCollection = with(\"naturalearth_cities\") {\n", " val url = \"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/shp/${this}/${this}.shp\"\n", " factory.createDataStore(URL(url)).featureSource.features\n", "}\n", "val cities = cityFeatures.toSpatialDataset()\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "agricultural-deployment", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T11:05:17.083381Z", "iopub.status.busy": "2024-08-23T11:05:17.083119Z", "iopub.status.idle": "2024-08-23T11:05:17.756691Z", "shell.execute_reply": "2024-08-23T11:05:17.756473Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot() + \n", " geomMap(map = europe.toSpatialDataset(), fill=\"#e5f5e0\") +\n", " geomPoint(data = cities, color = \"#224717\", size = 3) +\n", " geomLabel(data = cities, hjust = 0, vjust = 1, color = \"#224717\") { label = \"name\" } +\n", " coordMap(xlim = -10.5 to 44.0, ylim = 36.0 to 60.5) +\n", " theme(axis=\"blank\", panelGrid=\"blank\")" ] } ], "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.9.23" } }, "nbformat": 4, "nbformat_minor": 5 }