Skip to content

Instantly share code, notes, and snippets.

@erikerlandson
Created August 28, 2015 19:54
Show Gist options
  • Save erikerlandson/e238b36ae11b44de9b9b to your computer and use it in GitHub Desktop.
Save erikerlandson/e238b36ae11b44de9b9b to your computer and use it in GitHub Desktop.
A scatter plot in scala-bokeh
object charts {
import com.redhat.et.silex.util.OptionalArg
import OptionalArg.fullOptionSupport
import io.continuum.bokeh._
private def defaultTools(plot: Plot) = List(
new PanTool().plot(plot),
new BoxZoomTool().plot(plot),
new ResetTool().plot(plot),
new PreviewSaveTool().plot(plot))
def scatter[N :Numeric](xdata: Seq[N], ydata: Seq[N],
title: OptionalArg[String] = None,
xlab: OptionalArg[String] = None,
ylab: OptionalArg[String] = None,
logx: Boolean = false,
logy: Boolean = false): Plot = {
val num = implicitly[Numeric[N]]
val xdat = xdata.map(t => num.toDouble(t))
val ydat = ydata.map(t => num.toDouble(t))
object source extends ColumnDataSource {
val x = column(xdat)
val y = column(ydat)
}
val circle = new Circle()
.x(source.x)
.y(source.y)
.size(10)
.line_color(Color.Black)
val renderer = new GlyphRenderer()
.data_source(source)
.glyph(circle)
val plot = new Plot()
plot.x_range := new DataRange1d()
plot.y_range := new DataRange1d()
title.foreach { plot.title := _ }
// to-do: log axis seems to be ignored
val xaxis = if (logx) new LogAxis() else new LinearAxis()
val yaxis = if (logy) new LogAxis() else new LinearAxis()
xaxis.plot := plot
xaxis.bounds := ((xdat.min, xdat.max))
xaxis.major_tick_in := 0
xlab.foreach { xaxis.axis_label := _ }
yaxis.plot := plot
yaxis.bounds := ((ydat.min, ydat.max))
yaxis.major_tick_in := 0
ylab.foreach { yaxis.axis_label := _ }
plot.below <<= (xaxis :: _)
plot.left <<= (yaxis :: _)
val xgrid = new Grid().plot(plot).axis(xaxis).dimension(0)
val ygrid = new Grid().plot(plot).axis(yaxis).dimension(1)
plot.renderers := List(xaxis, yaxis, xgrid, ygrid, renderer)
plot.tools := defaultTools(plot)
plot
}
def save(plot: Plot, fname: String) {
val doc = new Document(plot)
doc.save(fname)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment