{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Plotting Example with [EvilPlot](https://cibotech.github.io/evilplot/)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "interp.repositories() ++= Seq(\n", " coursierapi.MavenRepository.of(\"https://dl.bintray.com/cibotech/public\")\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import $ivy.`com.cibo::evilplot-repl:0.6.3`\n", "\n", "def showPlot(plot: com.cibo.evilplot.geometry.Drawable) =\n", " Image.fromRenderedImage(plot.asBufferedImage, format=Image.PNG)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Our first plot\n", "\n", "EvilPlot is all about building larger graphical capabilities out of smaller ones. What this means for you is that making\n", "simple plots is easy, and we don't have to spend a lot of time going over all the features just to get started. So let's make\n", "our first plot, a simple scatter plot with sequential x-values and random y-values. Then we'll open that plot up in a new window\n", "and take a look at it." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import com.cibo.evilplot._\n", "import com.cibo.evilplot.plot._\n", "import com.cibo.evilplot.plot.aesthetics.DefaultTheme._\n", "import com.cibo.evilplot.numeric.Point\n", "\n", "val data = Seq.tabulate(100) { i =>\n", " Point(i.toDouble, scala.util.Random.nextDouble())\n", "}\n", "\n", "showPlot(ScatterPlot(data).render())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There's a version of this example which renders to an HTML canvas using ScalaJS [here](render-context.html#canvasrendercontext)\n", "\n", "To break it down:\n", "+ EvilPlot uses an implicit theming system, which lets you control the appearance of plots wholesale. In this case,\n", "we're just using the built-in one, so we import it.\n", "+ `ScatterPlot` returns a `Plot` object, which is a description of how data should be plotted (plot these points as\n", "little circles on the screen), with what components (like axes, a background etc.). In this case, we've used _no_\n", "components. All we get is points on the screen!\n", "+ `render()` on `Plot` returns a `Drawable` object. Think of a `Drawable` as a fully specified description of a\n", "scene. `render` itself does not perform any side-effects, it simply constructs a scene given a plot and a size.\n", "+ Finally, `displayPlot` is an additional utility the opens the plot in a new window. You might not use this inside\n", "an application, but it's useful for exploring in the REPL!\n", "\n", "This plot is not very interesting, of course. We should probably add some axes, so we know the range of the data, and\n", "some labels, so our audience knows what we're talking about. That's easy as well:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import com.cibo.evilplot.plot._\n", "import com.cibo.evilplot.plot.aesthetics.DefaultTheme._\n", "import com.cibo.evilplot.numeric.Point\n", "\n", "val data = Seq.tabulate(100) { i =>\n", " Point(i.toDouble, scala.util.Random.nextDouble())\n", "}\n", "\n", "val plot = ScatterPlot(data)\n", " .xAxis()\n", " .yAxis()\n", " .frame()\n", " .xLabel(\"x\")\n", " .yLabel(\"y\")\n", " .render()\n", "\n", "showPlot(plot)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adding these things is simply a matter of calling an additional method on your plot that specifies _exactly_ what type\n", "of component you want to add." ] } ], "metadata": { "_draft": { "nbviewer_url": "https://gist.github.com/021a0a089e31e6dbbc66f25ef8a86ce1" }, "gist": { "data": { "description": "Plotting example with EvilPlot ", "public": true }, "id": "021a0a089e31e6dbbc66f25ef8a86ce1" }, "kernelspec": { "display_name": "Scala (2.12)", "language": "scala", "name": "scala212" }, "language_info": { "codemirror_mode": "text/x-scala", "file_extension": ".scala", "mimetype": "text/x-scala", "name": "scala", "nbconvert_exporter": "script", "version": "2.12.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }