Please help to understand the following example of Breeze usage. The code below has both Scala object method invocations, such as f.subplot(0)
, f.saveas
etc., as well as function calls: linspace(0.0,1.0)
, plot(x, x :^ 2.0)
.
As usual, object methods are described in generated documentation: http://www.scalanlp.org/api/index.html#breeze.plot.Plot
Questions:
1) Where can I find specification of function calls: linspace(0.0,1.0)
, plot(x, x :^ 2.0)
? As far as I know for ploting Breeze uses JFreeChart (http://www.jfree.org/jfreechart/download.html). Maybe these linspace
and plot
are Java objects imported from JFreeChart package?
2) What does x :^ 3.0
mean?
import breeze.plot._
val f = Figure()
val p = f.subplot(0)
val x = linspace(0.0,1.0)
p += plot(x, x :^ 2.0)
p += plot(x, x :^ 3.0, '.')
p.xlabel = "x axis"
p.ylabel = "y axis"
f.saveas("lines.png") // save current figure as a .png, eps and pdf also supported
1 You can find the linspace
specification in the breeze package object linalg
and plot
is in the package object plog
:
http://www.scalanlp.org/api/index.html#breeze.linalg.package https://github.com/scalanlp/breeze/blob/master/math/src/main/scala/breeze/linalg/package.scala#L127
/**
* Generates a vector of linearly spaced values between a and b (inclusive).
* The returned vector will have length elements, defaulting to 100.
*/
def linspace(a : Double, b : Double, length : Int = 100) : DenseVector[Double] = {
val increment = (b - a) / (length - 1)
DenseVector.tabulate(length)(i => a + increment * i)
}
http://www.scalanlp.org/api/index.html#breeze.plot.package https://github.com/scalanlp/breeze/blob/master/viz/src/main/scala/breeze/plot/package.scala#L24
/**
* Plots the given y versus the given x with the given style.
*
* @param x X-coordinates, co-indexed with y (and indexed by keys of type K).
* @param y Y-coordinates, co-indexed with x (and indexed by keys of type K).
* @param style Matlab-like style spec of the series to plot.
* @param name Name of the series to show in the legend.
* @param labels Optional in-graph labels for some points.
* @param tips Optional mouse-over tooltips for some points.
*/
def plot[X,Y,V](x: X, y: Y, style : Char = '-', colorcode : String = null, name : String = null,
lines : Boolean = true, shapes : Boolean = false,
labels : (Int) => String = null.asInstanceOf[Int=>String],
tips : (Int) => String = null.asInstanceOf[Int=>String] )
(implicit xv: DomainFunction[X,Int,V],
yv: DomainFunction[Y, Int, V], vv: V=>Double):Series = new Series {
...
2 This is element wise exponentiation. So x :^ 3.0
would return x with every element to the third power. In the example given, x is a DenseVector
of 100 values between 0 and 1. so x :^ 3.0
would give you another DenseVector
of 100 values between 0 and 1, but they are taken to the third power, which makes a nice graph.