Plot with Cartesian axes
Plot with Cartesian axes
This class draws a sub-plot on Cartesian axes. This can be used to draw bar charts, scatter plots, box plots, contour plots etc.: anything that has an x- and a y-axis.
CartesianPlot
instances can be sent directly to Plotly:
val xs = Vector(1, 2, 5) val ys = Vector(2, 4, 9) val plot = CartesianPlot().withScatter(xs, ys) draw(plot, "my-scatter-plot")
They can also be embedded in Figure instances for more complex plot layouts:
val xs = Vector(1, 2, 5) val ys1 = Vector(2, 4, 9) val ys2 = Vector(2, 6, 12) val figure = RowFigure(2) // 2 sub-plots next to each other .plot(0) { CartesianPlot().withScatter(xs, ys1) } // left subplot .plot(1) { CartesianPlot().withScatter(xs, ys2) } // right subplot draw(figure, "multiple-plots")
CartesianPlot
instances support multiple series:
val xs = Vector(1, 2, 3) val ys1 = xs.map { 2 * _ } val ys2 = xs.map { -2 * _ } val p = CartesianPlot() .withScatter(xs, ys1, ScatterOptions().name("series-1")) .withScatter(xs, ys2, ScatterOptions().name("series-2")) draw(p, "multiple-series")
When you call the .withScatter
method on a CartesianPlot
object, it
returns a new plot with the new data series added.
CartesianPlot
instances are immutable. Thus, the following will not
work:
// Not what you want! val p = CartesianPlot().withScatter(xs, ys1) p.withScatter(xs, ys2) // No: this does not modify 'p' in place! draw(p, "my-other-plot")
Do this instead:
val p = CartesianPlot().withScatter(xs, ys1) val newPlot = p.withScatter(xs, ys2) draw(newPlot, "my-other-plot")
Or, better yet, use chaining to avoid creating temporary variables:
val p = CartesianPlot() .withScatter(xs, ys1) .withScatter(xs, ys2) draw(p, "another-plot")
All methods in this class work in the same way: they return a new
instance of CartesianPlot
. This pattern is called the immutable builder
pattern: it is a variant of the common
builder pattern
adapted for immutable objects.
Colorscale predefined by Plotly
Figure containing plots arranged on a grid.
Figure containing plots arranged on a grid.
This Figure subclass is designed for equally spaced subplots on a grid.
Use the companion object's apply
method to construct a new instance,
specifying the number of rows and columns. For instance,
val figure = GridFigure(2, 3)
will build a new figure with 6 subplots,
arranged in a grid with two rows and three columns. You can then use
the plot method to set the content of specific sub-plots.
import util.Random val xs = (0 to 100).map { i => Random.nextGaussian } val ys = (0 to 100).map { i => Random.nextGaussian } val ys2 = (0 to 100).map { i => Random.nextGaussian } val ys3 = (0 to 100).map { i => Random.nextGaussian } val figure = GridFigure(2, 3) // 2 rows, 3 columns .plot(0, 0) { CartesianPlot().withScatter(xs, ys) } // top left .plot(0, 1) { CartesianPlot().withScatter(xs, ys2) } // top middle .plot(1, 2) { CartesianPlot().withScatter(xs, ys3) } // bottom right .title("My grid figure") draw(figure, "grid-figure")
Figure containing plots arranged in a row.
Figure containing plots arranged in a row.
This Figure subclass is designed for equally spaced subplots
in a row.
Use the companion object's apply
method to construct a new
instance, specifying the number of subplots. For instance,
val figure = RowFigure(2)
will build a new figure with two
subplots in a row. You can then use the plot method to
set the content of specific sub-plots.
import util.Random val xs = (0 to 100).map { i => Random.nextGaussian } val ys = (0 to 100).map { i => Random.nextGaussian } val ys2 = (0 to 100).map { i => Random.nextGaussian } val figure = RowFigure(2) // 2 subplots .plot(0) { CartesianPlot().withScatter(xs, ys) } // left .plot(1) { CartesianPlot().withScatter(xs, ys2) } // right .title("My row figure") draw(figure, "row-figure")
Options controlling how scatter and line plots are plotted.
Figure containing a single plot
Figure containing a single plot
val xs = Vector(1, 2, 5) val ys = Vector(5, 9, 11) val figure = SinglePlotFigure() .plot { CartesianPlot().withScatter(xs, ys) } .title("My awesome plot") draw(figure, "test-plot")
Use the companion object's apply
method to construct
a new figure. Set the content of the plot on the figure
with the plot method.
Note that all the methods on this class return a
copy of the current instance. They do not modify the
instance in place. Instances of SinglePlotFigure
are immutable.
Options controlling how surface plots are drawn.
Options controlling how surface plots are drawn.
val surfaceOptions = SurfaceOptions().opacity(0.9).colorscale("Electric") val plot = ThreeDPlot().withSurface(xs, ys, zs, surfaceOptions)
Plot with 3D axes
Plot with 3D axes
This class represents plots with 3D Cartesian axes. This can be used to draw 3D surface plots.
ThreeDPlot
instances can be sent directly to Plotly:
val zs = Vector( Vector(1.0, 2.0, 1.0), Vector(5.0, 4.0, 5.0), Vector(1.0, 2.0, 1.0) ) val plot = ThreeDPlot().withSurface(zs) draw(plot, "my-3d-plot")
They can also be embedded in Figure instances for more complex plot layouts:
val zs1 = Vector( Vector(1.0, 2.0, 1.0), Vector(5.0, 4.0, 5.0), Vector(1.0, 2.0, 1.0) ) val zs2 = zs1.map { row => row.map { _ - 2.0 } } val figure = RowFigure(2) // 2 sub-plots next to each other .plot(0) { ThreeDPlot().withSurface(zs1) } // left sub-plot .plot(1) { ThreeDPlot().withSurface(zs2) } // right sub-plot draw(figure, "3d-subplots")
Add a surface plot with the withSurface
method:
val xs = Vector(-1.0, 0.0, 1.0) val ys = Vector(0.0, 10.0) val zs = Vector( Vector(1.0, 2.0, 1.0), Vector(5,0, 4.0, 5.0) ) val p = ThreeDPlot().withSurface(xs, ys, zs)
The z-values are assumed to be nested iterables, oriented such that
zs(0)(1)
is the value of z at xs(1)
and ys(0)
.
You can also pass in options to control how the surface is represented:
val p = ThreeDPlot().withSurface(xs, ys, zs, SurfaceOptions().opacity(0.4))
See the documentation for SurfaceOptions for a list of available options.
.withSurface
also supports passing a zs
iterable
without xs
and ys
.
This is equivalent to having xs = (0 to zs(0).size)
and
ys = (0 to zs.size)
.
ThreeDPlot
instances support multiple surfaces:
val zs1 = Vector( Vector(1.0, 2.0, 1.0), Vector(5.0, 4.0, 5.0), Vector(1.0, 2.0, 1.0) ) val zs2 = zs1.map { row => row.map { _ - 2.0 } } val p = ThreeDPlot() .withSurface(zs1, SurfaceOptions().name("top")) .withSurface(zs2, SurfaceOptions().name("bottom"))
When you call the .withSurface
method on a ThreeDPlot
object, it
returns a new plot with the series added. ThreeDPlot
instances
are immutable. Thus, the following does not work as expected:
// Not what you want! val plot = ThreeDPlot().withSurface(zs) plot.withSurface(zs2) // No: this does not modify 'plot' in place!
You should chain calls to withSurface
instead:
val plot = ThreeDPlot()
.withSurface(zs1)
.withSurface(zs2)
All methods in this class work in the same way: they return a new
instance of ThreeDPlot
. This pattern is called the immutable builder
pattern: it is a variant of the common
builder pattern
adapted for immutable objects.
Factory methods for building instances of GridFigure
.
Factory methods for building instances of SinglePlotFigure
.
Scala Plotly client
Plotly is a platform for creating and hosting plots. This library lets you create, modify and delete plots on Plotly.
Build a plot using Plot, and send that plot to the Plotly servers with draw.
Useful links
For more information on how to build complex plot objects, read the documentation for CartesianPlot and ThreeDPlot.
For information on structuring complex figures with subplots, read the documentation for RowFigure and GridFigure.
To control how the client deals with existing files with the same name on the Plotly server, and to control the privacy options, read the documentation for writer.FileOptions