{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot Demo Usage for SCALA Language" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "val plot = new Plot();\n", "val y1 = List(1.5, 1, 6, 5, 2, 8)\n", "val cs = List(Color.black, Color.red, Color.gray, Color.green, Color.blue, Color.pink)\n", "val ss = List(StrokeType.SOLID, StrokeType.SOLID, StrokeType.DASH, StrokeType.DOT, StrokeType.DASHDOT, StrokeType.LONGDASH)\n", "plot.add(new Stems(y1, cs, ss, 5))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "val plot = new Plot(\"Elo\")\n", "var cs = new Color(255, 0, 0, 128)// transparent bars\n", "//cs[3] = Color.red // set color of a single bar, solid colored bar\n", "plot.add(new Bars(List(1, 2, 3, 4), List(3, 5, 2, 3, 7), cs, Color.black, 0.3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "val plot = new Plot(\"Changing Point Size, Color, Shape\")\n", "val y1 = List(6, 7, 12, 11, 8, 14)\n", "val y2 = y1.map(x => x - 1)\n", "val y3 = y1.map(x => x - 2)\n", "val y4 = y1.map(x => x - 3)\n", "plot.add(new Points(y1))\n", "plot.add(new Points(y2, ShapeType.CIRCLE))\n", "plot.add(new Points(y3, 8.0, ShapeType.DIAMOND))\n", "plot.add(new Points(y4, 12.0, Color.orange, Color.red))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "val plot = new Plot(\"Changing point properties with list\")\n", "val cs = List(Color.black, Color.red, Color.orange, Color.green, Color.blue, Color.pink)\n", "val ss = List(6.0, 9.0, 12.0, 15.0, 18.0, 21.0)\n", "val fs = List(false, false, false, true, false, false)\n", "\n", "val list = List(new Points(List(5,5,5,5,5,5), 12.0, cs), \n", " new Points(List(4,4,4,4,4,4), 12.0, Color.gray, cs),\n", " new Points(List(3,3,3,3,3,3), 12, Color.red),\n", " new Points(List(2,2,2,2,2,2), 12.0, Color.black, fs, Color.black))\n", "\n", "plot.add(list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "val plot = new Plot()\n", "val y = List(3, 5, 2, 3)\n", "val x0 = List(0, 1, 2, 3)\n", "val x1 = List(3, 4, 5, 8)\n", "plot.add(new Area(x0, y))\n", "plot.add(new Area(x1, y, new Color(128, 128, 128, 50), 0))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "val p = new Plot()\n", "p.add(new Line(List(3, 6, 12, 24), \"Median\"))\n", "p.add(new Area(List(4, 8, 16, 32), List(2, 4, 8, 16), new Color(255, 0, 0, 50), \"Q1 to Q3\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "val y1 = List(1,5,3,2,3)\n", "val y2 = List(7,2,4,1,3)\n", "val p = new Plot(\"Plot with XYStacker\", 200)\n", "val a1 = new Area(y1, \"y1\")\n", "val a2 = new Area(y2, \"y2\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "val p = new Plot ()\n", "p.add(new Line(List(-1, 1)))\n", "p.add(new ConstantLine(0, 0.65, StrokeType.DOT, Color.blue))\n", "p.add(new ConstantLine(0, 1, StrokeType.DASHDOT, Color.blue))\n", "p.add(new ConstantLine(1, 0.4, Color.gray, 5, true))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "val constBand = new ConstantBand(List(1, 2), List(1, 3))\n", "val lineVal = new Line(List(-3, 1, 3, 4, 5))\n", "val plot = new Plot()\n", "plot.add(lineVal)\n", "plot.add(constBand)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "val p = new Plot() \n", "p.add(new Line(List(-3, 1, 2, 4, 5), List(4, 2, 6, 1, 5)))\n", "p.add(new ConstantBand(List(Double.NegativeInfinity, 1), new Color(128, 128, 128, 50)))\n", "p.add(new ConstantBand(List(1, 2)))\n", "p.add(new ConstantBand(List(4, Double.PositiveInfinity)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "val plot = new Plot()\n", "val xs = List(1,2,3,4,5,6,7,8,9,10)\n", "val ys = List(8.6, 6.1, 7.4, 2.5, 0.4, 0.0, 0.5, 1.7, 8.4, 1)\n", "\n", "def label(i: Int) : String = { \n", " if (ys(i) > ys(i+1) && ys(i) > ys(i-1)) return \"max\"\n", " if (ys(i) < ys(i+1) && ys(i) < ys(i-1)) return \"min\"\n", " if (ys(i) > ys(i-1)) return \"rising\"\n", " if (ys(i) < ys(i-1)) return \"falling\"\n", " return \"\"\n", "}\n", "\n", "for (i <- 0 to xs.size()) {\n", " if (i > 0 && i < xs.size()-1) {\n", " plot.add(new Text(xs(i), ys(i), label(i), -i/3.0))\n", " }\n", "\n", "}\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "val plot = new Plot()\n", "val xs = List(1,2,3,4,5,6,7,8,9,10)\n", "val ys = List(8.6, 6.1, 7.4, 2.5, 0.4, 0.0, 0.5, 1.7, 8.4, 1)\n", "def label(i: Int) : String = { \n", " if (ys(i) > ys(i+1) && ys(i) > ys(i-1)) return \"max\"\n", " if (ys(i) < ys(i+1) && ys(i) < ys(i-1)) return \"min\"\n", " if (ys(i) > ys(i-1)) return \"rising\"\n", " if (ys(i) < ys(i-1)) return \"falling\"\n", " return \"\"\n", "}\n", "\n", "for (i <- 0 to xs.length) {\n", " if (i > 0 && i < (xs.length-1)) {\n", " plot.add(new Text(xs(i), ys(i), label(i), -i/3.0))\n", " }\n", "}\n", "\n", "plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "val ch = new Crosshair(new Color(255, 128, 5), 2, StrokeType.DOT)\n", "val pp = new Plot(ch, true, LegendLayout.HORIZONTAL, LegendPosition.TOP)\n", "def x = List(1, 4, 6, 8, 10)\n", "def y = List(3, 6, 4, 5, 9)\n", "pp.add(new Line(\"Line\", x, y, 3))\n", "pp.add(new Bars(\"Bar\", List(1,2,3,4,5,6,7,8,9,10), List(2, 2, 4, 4, 2, 2, 0, 2, 2, 4), 0.5))\n", "pp.add(new Points(x, y, 10, List(\"x = \",\"y = \")))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import com.twosigma.beaker.fileloader.CsvPlotReader\n", "import java.util\n", "\n", "val rates = new CsvPlotReader().readAsList(\"tableRows.csv\").asInstanceOf[util.List[util.Map[String, AnyRef]]]\n", "\n", "new SimpleTimePlot(rates, List(\"y1\", \"y10\"), \"Price\", List(\"All\", \"1 Year\", \"10 Year\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import scala.collection.mutable.ListBuffer\n", "\n", "val points = 100\n", "val logBase = 10\n", "var expys = new ListBuffer[Double]()\n", "var xs = new ListBuffer[Double]()\n", "\n", "for (i <- 0 to points) {\n", " xs += i / 15.0\n", " expys += Math.exp(xs(i))\n", "}\n", "\n", "val cplot = new CombinedPlot();\n", "val logYPlot = new Plot(\"Linear x, Log y\", \"Log\",\"\", false, 0.0, true, logBase);\n", "logYPlot.add(new Line(\"f(x) = exp(x)\", xs.toList, expys.toList, 3.0f))\n", "logYPlot.add(new Line(\"g(x) = x\", xs.toList, xs.toList, 3.0f))\n", "\n", "cplot.add(logYPlot, 3)\n", "\n", "val linearYPlot = new Plot(\"Linear x, Linear y\", \"Linear\", \"\", false, 0.0, true, logBase)\n", "linearYPlot.add(new Line(\"f(x) = exp(x)\", xs.toList, expys.toList, 3.0f))\n", "linearYPlot.add(new Line(\"g(x) = x\", xs.toList, xs.toList, 3.0f))\n", "cplot.add(linearYPlot, 3);\n", "\n", "cplot" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import scala.collection.mutable.ListBuffer\n", "\n", "val points = 100\n", "val logBase = 10\n", "var expys = new ListBuffer[Double]()\n", "var xs = new ListBuffer[Double]()\n", "\n", "for (i <- 0 to points) {\n", " xs += i / 15.0\n", " expys += Math.exp(xs(i))\n", "}\n", "\n", "val plot = new Plot(\"Log x, Log y\", \"Log\",\"Log\", true, logBase, true, logBase);\n", "\n", "plot.add(new Line(\"f(x) = exp(x)\", xs.toList, expys.toList, 3.0f))\n", "plot.add(new Line(\"g(x) = x\", xs.toList, xs.toList, 3.0f))\n", "plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import java.util.{Calendar, Date}\n", "import java.util.SimpleTimeZone\n", "\n", "val cal = Calendar.getInstance();\n", "cal.add(Calendar.HOUR, -1)\n", "\n", "val today = new Date()\n", "val millis = today.getTime()\n", "val hour = 1000 * 60 * 60;\n", "\n", "val plot = new TimePlot(new SimpleTimeZone(10800000, \"America/New_York\"))\n", "\n", "//list of milliseconds\n", "plot.add(new Points((0 to 10).map(x => millis + hour * x).toList, \n", " (0 to 10).toList,\n", " 10,\n", " \"milliseconds\"));\n", "\n", "plot.add(new Points((0 to 10).map(x => {cal.add(Calendar.HOUR, 1); cal.getTime()}).toList,\n", " (0 to 10).toList,\n", " 5,\n", " \"date objects\"));\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import java.util.Date\n", "\n", "val today = new Date()\n", "val millis = today.getTime()\n", "val nanos = millis * 1000 * 1000// g makes it arbitrary precision\n", "val np = new NanoPlot()\n", "\n", "np.add(new Points((0 to 10).map(x => nanos + 7 * x).toList, \n", " (0 to 10).toList))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import scala.util.Random\n", "\n", "val r = new Random()\n", "val p = new Plot(\"Advanced Plot Styling\",\n", " \"font-size:32px; font-weight: bold; font-family: courier; fill: green;\",\n", " \"stroke: purple; stroke-width: 3;\",\n", " \"color: green;\")\n", "\n", "p.add(new Points((1 to 1000).map(x => r.nextGaussian() * 10.0d).toList,\n", " (1 to 1000).map(x => r.nextGaussian() * 20.0d).toList))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import java.nio.file.Files\n", "\n", "byte[] picture = Files.readAllBytes(new File(\"widgetArch.png\").toPath());\n", "\n", "var p = new Plot();\n", "// x y width height are coordinates, opacity is a double in 0~1\n", "\n", "// image can be loaded via bytes, filepath, or url\n", "p.add(new Rasters(List(-10,3), List(3,1.5), List(6,5), List(10,8), List(1,0.5), dataString: picture));\n", "//p << new Rasters(x: -1, y: 4.5, width: 5, height: 8, opacity:0.5, filePath: \"widgetArch.png\");\n", "p << new Rasters(x: [-4], y: [10.5], width: [7], height: [2], opacity:[1], fileUrl: \"https://www.twosigma.com/static/img/twosigma.png\");\n", "\n", "// a list of images!\n", "def x = [-8, -5, -3, -2, -1, 1, 2, 4, 6, 8]\n", "def y = [4, 5, 1, 2, 0 ,3, 6, 4, 5, 9]\n", "def width = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n", "def opacity = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]\n", "p << new Rasters(x: x, y: y, width:width, height:width, opacity:opacity,fileUrl: \"http://icons.iconarchive.com/icons/paomedia/small-n-flat/1024/sign-check-icon.png\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import java.io.File\n", "import java.nio.file.Files\n", "\n", "var picture: Array[Byte] = Files.readAllBytes(new File(\"widgetArch.png\").toPath());\n", "\n", "var p = new Plot();\n", "// x y width height are coordinates, opacity is a double in 0~1\n", "\n", "// image can be loaded via bytes, filepath, or url\n", "p.add(new Rasters(List(-10,3), List(3,1.5), List(6,5), List(10,8), List(1,0.5), picture))\n", "p.add(new Rasters(List(-4), List(10.5), List(2), List(7), List(1), \"https://www.twosigma.com/static/img/twosigma.png\"));\n", "\n", "// a list of images!\n", "val x = List(-8, -5, -3, -2, -1, 1, 2, 4, 6, 8)\n", "val y = List(4, 5, 1, 2, 0 ,3, 6, 4, 5, 9)\n", "val width = List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)\n", "val opacity = List(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1)\n", "p.add(new Rasters(x, y, width, width, opacity, \"http://icons.iconarchive.com/icons/paomedia/small-n-flat/1024/sign-check-icon.png\"))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "val plot = new Plot(\"Setting 2nd Axis bounds\")\n", "val ys = List(0, 2, 4, 6, 15, 10)\n", "val ys2 = List(-40, 50, 6, 4, 2, 0)\n", "val ys3 = List(3, 6, 3, 6, 70, 6)\n", "plot.add(new YAxis(\"Spread\"))\n", "plot.add(new Line(ys))\n", "plot.add(new Line(ys2, \"Spread\"))\n", "\n", "plot.getYAxes().get(0).setBound(1,5)\n", "\n", "plot" ] } ], "metadata": { "beakerx_kernel_parameters": { "classpath": [], "imports": [ "com.twosigma.beaker.chart.xychart.*", "com.twosigma.beaker.easyform.EasyForm", "com.twosigma.beaker.chart.Filter", "com.twosigma.beaker.chart.Color", "com.twosigma.beaker.NamespaceClient", "com.twosigma.beaker.fileloader.CsvPlotReader", "com.twosigma.beaker.chart.treemap.*", "com.twosigma.beaker.jvm.object.OutputCell", "com.twosigma.beaker.chart.treemap.util.*", "com.twosigma.beaker.chart.categoryplot.plotitem.*", "com.twosigma.beaker.chart.categoryplot.*", "com.twosigma.beaker.scala.easyform.EasyForm", "com.twosigma.beaker.chart.xychart.plotitem.*", "com.twosigma.beaker.table.*", "com.twosigma.beaker.scala.chart.xychart._", "com.twosigma.beaker.chart.GradientColor", "com.twosigma.beaker.scala.chart.xychart.plotitem._", "com.twosigma.beaker.chart.legend.*", "net.sf.jtreemap.swing.*", "com.twosigma.beaker.chart.histogram.*", "com.twosigma.beaker.chart.heatmap.HeatMap" ] }, "kernelspec": { "display_name": "Scala", "language": "scala", "name": "scala" }, "language_info": { "codemirror_mode": "text/x-scala", "file_extension": ".scala", "mimetype": "", "name": "Scala", "nbconverter_exporter": "", "version": "2.12.2" } }, "nbformat": 4, "nbformat_minor": 2 }