Scala API to Plot Widgets

This API is being improved.

In [ ]:
val plot = new Plot();
val y1 = Seq(1.5, 1, 6, 5, 2, 8)
val cs = Seq(Color.black, Color.red, Color.gray, Color.green, Color.blue, Color.pink)
val ss = Seq(StrokeType.SOLID, StrokeType.SOLID, StrokeType.DASH, StrokeType.DOT, StrokeType.DASHDOT, StrokeType.LONGDASH)
plot.add(new Stems(y1, cs, ss, 5))
In [ ]:
val plot = new Plot("Elo")
var cs = new Color(255, 0, 0, 128)// transparent bars
//cs[3] = Color.red // set color of a single bar, solid colored bar
plot.add(new Bars(Seq(1, 2, 3, 4), Seq(3, 5, 2, 3, 7), cs, Color.black, 0.3))
In [ ]:
val plot = new Plot("Changing Point Size, Color, Shape")
val y1 = Seq(6, 7, 12, 11, 8, 14)
val y2 = y1.map(x => x - 1)
val y3 = y1.map(x => x - 2)
val y4 = y1.map(x => x - 3)
plot.add(new Points(y1))
plot.add(new Points(y2, ShapeType.CIRCLE))
plot.add(new Points(y3, 8.0, ShapeType.DIAMOND))
plot.add(new Points(y4, 12.0, Color.orange, Color.red))
In [ ]:
val plot = new Plot("Changing point properties with list")
val cs = Seq(Color.black, Color.red, Color.orange, Color.green, Color.blue, Color.pink)
val ss = Seq(6.0, 9.0, 12.0, 15.0, 18.0, 21.0)
val fs = Seq(false, false, false, true, false, false)

val list = List(new Points(Seq(5,5,5,5,5,5), 12.0, cs), 
                new Points(Seq(4,4,4,4,4,4), 12.0, Color.gray, cs),
                new Points(Seq(3,3,3,3,3,3), 12, Color.red),
                new Points(Seq(2,2,2,2,2,2), 12.0, Color.black, fs, Color.black))

plot.add(list)
In [ ]:
val plot = new Plot()
val y = Seq(3, 5, 2, 3)
val x0 = Seq(0, 1, 2, 3)
val x1 = Seq(3, 4, 5, 8)
plot.add(new Area(x0, y))
plot.add(new Area(x1, y, new Color(128, 128, 128, 50), 0))
In [ ]:
val p = new Plot()
p.add(new Line(Seq(3, 6, 12, 24), "Median"))
p.add(new Area(Seq(4, 8, 16, 32), Seq(2, 4, 8, 16), new Color(255, 0, 0, 50), "Q1 to Q3"))
In [ ]:
val y1 = Seq(1,5,3,2,3)
val y2 = Seq(7,2,4,1,3)
val p = new Plot("Plot with XYStacker", 200)
val a1 = new Area(y1, "y1")
val a2 = new Area(y2, "y2")
In [ ]:
val p = new Plot ()
p.add(new Line(Seq(-1, 1)))
p.add(new ConstantLine(0, 0.65, StrokeType.DOT, Color.blue))
p.add(new ConstantLine(0, 1, StrokeType.DASHDOT, Color.blue))
p.add(new ConstantLine(1, 0.4, Color.gray, 5, true))
In [ ]:
val constBand = new ConstantBand(Seq(1, 2), Seq(1, 3))
val lineVal = new Line(Seq(-3, 1, 3, 4, 5))
val plot = new Plot()
plot.add(lineVal)
plot.add(constBand)
In [ ]:
val p = new Plot() 
p.add(new Line(Seq(-3, 1, 2, 4, 5), Seq(4, 2, 6, 1, 5)))
p.add(new ConstantBand(Seq(Double.NegativeInfinity, 1), new Color(128, 128, 128, 50)))
p.add(new ConstantBand(Seq(1, 2)))
p.add(new ConstantBand(Seq(4, Double.PositiveInfinity)))
In [ ]:
val plot = new Plot()
val xs = Seq(1,2,3,4,5,6,7,8,9,10)
val ys = Seq(8.6, 6.1, 7.4, 2.5, 0.4, 0.0, 0.5, 1.7, 8.4, 1)

plot.add(new Line(xs, ys))

def label(i: Int) : String = { 
  if (ys(i) > ys(i+1) && ys(i) > ys(i-1)) return "max"
  if (ys(i) < ys(i+1) && ys(i) < ys(i-1)) return "min"
  if (ys(i) > ys(i-1)) return "rising"
  if (ys(i) < ys(i-1)) return "falling"
  return ""
}

for (i <- 0 to xs.size) {
  if (i > 0 && i < xs.size - 1) {
    plot.add(new Text(xs(i), ys(i), label(i), -i/3.0))
  }
}

plot
In [ ]:
val ch = new Crosshair(new Color(255, 128, 5), 2, StrokeType.DOT)
val pp = new Plot(ch, true, LegendLayout.HORIZONTAL, LegendPosition.TOP)
def x = Seq(1, 4, 6, 8, 10)
def y = Seq(3, 6, 4, 5, 9)
pp.add(new Line("Line", x, y, 3))
pp.add(new Bars("Bar", Seq(1,2,3,4,5,6,7,8,9,10), Seq(2, 2, 4, 4, 2, 2, 0, 2, 2, 4), 0.5))
pp.add(new Points(x, y, 10, Seq("x = ","y = ")))
In [ ]:
import com.twosigma.beakerx.scala.fileloader.CsvPlotReader

val rates = new CsvPlotReader().read("../resources/data/interest-rates.csv")

new SimpleTimePlot(rates, Seq("y1", "y10"), "Price", Seq("All", "1 Year", "10 Year"))
In [ ]:
val points = 100
val logBase = 10

val xs = for (i <- 0 to points) yield i / 15.0
val expys = for (x <- xs) yield Math.exp(x)

val cplot = new CombinedPlot()
val logYPlot = new Plot("Linear x, Log y", "Log","", logX=false, 0.0, logY=true, logBase);
logYPlot.add(new Line("f(x) = exp(x)", xs, expys, 3.0f))
logYPlot.add(new Line("g(x) = x", xs, xs, 3.0f))

cplot.add(logYPlot, 3)

val linearYPlot = new Plot("Linear x, Linear y", "Linear", "", logX=false, 0.0, logY=false, logBase)
linearYPlot.add(new Line("f(x) = exp(x)", xs, expys, 3.0f))
linearYPlot.add(new Line("g(x) = x", xs, xs, 3.0f))
cplot.add(linearYPlot, 3);

cplot
In [ ]:
val points = 100
val logBase = 10
val xs = for (i <- 0 to points) yield i / 15.0
val expys = for (x <- xs) yield Math.exp(x)

val plot = new Plot("Log x, Log y", "Log","Log", logX=true, logBase, logY=true, logBase);

plot.add(new Line("f(x) = exp(x)", xs, expys, 3.0f))
plot.add(new Line("g(x) = x", xs, xs, 3.0f))
plot
In [ ]:
import java.util.{Calendar, Date}
import java.util.SimpleTimeZone

val cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -1)

val today = new Date()
val millis = today.getTime()
val hour = 1000 * 60 * 60;

val plot = new TimePlot(new SimpleTimeZone(10800000, "America/New_York"))

//list of milliseconds
plot.add(new Points((0 to 10).map(x => millis + hour * x), 
                    (0 to 10),
                    10,
                    "milliseconds"));

plot.add(new Points((0 to 10).map(x => {cal.add(Calendar.HOUR, 1); cal.getTime()}),
                    (0 to 10),
                    5,
                    "date objects"));
In [ ]:
import java.util.Date

val today  = new Date()
val millis = today.getTime()
val nanos  = millis * 1000 * 1000// g makes it arbitrary precision
val np = new NanoPlot()

np.add(new Points((0 to 10).map(x => nanos + 7 * x), 
                  (0 to 10)))
In [ ]:
import scala.util.Random

val r = new Random()
val p = new Plot("Advanced Plot Styling",
                 "font-size:32px; font-weight: bold; font-family: courier; fill: green;",
                 "stroke: purple; stroke-width: 3;",
                 "color: green;")

p.add(new Points((1 to 1000).map(x => r.nextGaussian() * 10.0d),
                 (1 to 1000).map(x => r.nextGaussian() * 20.0d)))
In [ ]:
import java.nio.file.Files
import java.io.File

val picture: Array[Byte] = Files.readAllBytes(new File("../resources/img/widgetArch.png").toPath())

var p =  new Plot();
// x y width height are coordinates, opacity is a double in 0~1

// image can be loaded via bytes, filepath, or url
p.add(new Rasters(List(-10,3), List(3,1.5), List(6,5), List(10,8), List(1,0.5), picture));
//p << new Rasters(x: -1, y: 4.5, width: 5, height: 8, opacity:0.5, filePath: "../resources/img/widgetArch.png");
p.add(new Rasters(List(-4), List(10.5), List(2), List(10), List(1), "https://www.twosigma.com/static/img/twosigma.png"));

// a list of images!
def x = List(-8, -5, -3, -2, -1, 1, 2, 4, 6, 8)
def y = List(4, 5, 1, 2, 0 ,3, 6, 4, 5, 9)
def width = List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
def opacity = List(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1)
p.add(new Rasters(x, y, width, width, opacity, "http://icons.iconarchive.com/icons/paomedia/small-n-flat/1024/sign-check-icon.png"))
In [ ]:
val plot = new Plot("Setting 2nd Axis bounds")
val ys = List(0, 2, 4, 6, 15, 10)
val ys2 = List(-40, 50, 6, 4, 2, 0)
val ys3 = List(3, 6, 3, 6, 70, 6)
plot.add(new YAxis("Spread"))
plot.add(new Line(ys))
plot.add(new Line(ys2, "Spread"))

plot.getYAxes().get(0).setBound(1,5)

plot
In [ ]: