Skip to content

Commit

Permalink
Merge pull request plotly#446 from ASIDataScience/scala/v0.2-changes
Browse files Browse the repository at this point in the history
Documentation for Scala client v0.2.0
  • Loading branch information
pbugnion authored Jul 5, 2016
2 parents a5dbe72 + e804258 commit e948627
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 73 deletions.
22 changes: 20 additions & 2 deletions _posts/scala/2016-03-22-getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ language: scala
To include Plotly in your project, add the following dependency to your `build.sbt` file:

```
libraryDependencies += "co.theasi" %% "plotly" % "0.1"
libraryDependencies += "co.theasi" %% "plotly" % "0.2.0"
```

The Scala plotly client is updated frequently. Check that you include the [latest version](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22co.theasi%22) in your script.

# API Documentation and source code
# API Documentation and Source Code

If you need documentation beyond the tutorials presented here, read either the [Scaladocs](http://asidatascience.github.io/scala-plotly-client/) for API documentation, or the [Source code](https://github.com/asidatascience/scala-plotly-client).

Expand All @@ -35,3 +35,21 @@ Once you have an account, generate an [API key](https://plot.ly/settings/api/).
"api_key": "lr1c37zw81"
}
```

# Your First Plot

```
val xs = (0.0 to 2.0 by 0.1)
val ys = xs.map { x => x*x }
val plot = Plot().withScatter(xs, ys)
draw(plot, "my-first-plot")
```

<iframe width="900" height="500" frameborder="0" scrolling="no" src="https://plot.ly/~pbugnion/548.embed"></iframe>

Calling `Plot().withScatter(xs, ys)` constructs a representation of the plot in memory. This representation is dispatched to Plotly with `draw(plot, "my-first-plot")`.


For examples of more advanced plots, see the [examples](/scala) section.
45 changes: 23 additions & 22 deletions _posts/scala/line_and_scatter/2016-03-22-categorical-dot-plot.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: Categorical Dot Plot
plot_url: https://plot.ly/~pbugnion/328
plot_url: https://plot.ly/~pbugnion/467
language: scala
suite: line_and_scatter
order: 5
Expand Down Expand Up @@ -29,18 +29,28 @@
.lineWidth(1)
.size(16))

// layout
val layout = SingleAxisLayout()
val p = Plot()
.withScatter(votingPopulation, country, commonOptions
.name("Percent of estimated voting age population")
.updatedMarker(_.color(156, 165, 196, 0.95).lineColor(156, 165, 196, 1.0)))
.withScatter(registeredVoters, country, commonOptions
.name("Percent of estimated registered voters")
.updatedMarker(_.color(204, 204, 204, 0.95).lineColor(217, 217, 217, 1.0)))
.xAxisOptions( // Plot axis options
AxisOptions()
.noGrid
.withLine
.lineColor(102, 102, 102)
.titleColor(204, 204, 204)
.tickFontColor(102, 102, 102)
.noAutoTick
.tickSpacing(10.0)
.tickColor(102, 102, 102))

// Add the plot to the figure
val figure = Figure()
.plot(p)
.title("Votes cast for ten lowest voting age population in OECD countries")
.xAxisOptions(AxisOptions()
.noGrid
.withLine
.lineColor(102, 102, 102)
.titleColor(204, 204, 204)
.tickFontColor(102, 102, 102)
.noAutoTick
.tickSpacing(10.0)
.tickColor(102, 102, 102))
.legend(LegendOptions()
.yAnchor(YAnchor.Middle)
.xAnchor(XAnchor.Right))
Expand All @@ -53,13 +63,4 @@
.paperBackgroundColor(254, 247, 234)
.plotBackgroundColor(254, 247, 234)

val p = Plot()
.layout(layout)
.withScatter(votingPopulation, country, commonOptions
.name("Percent of estimated voting age population")
.updatedMarker(_.color(156, 165, 196, 0.95).lineColor(156, 165, 196, 1.0)))
.withScatter(registeredVoters, country, commonOptions
.name("Percent of estimated registered voters")
.updatedMarker(_.color(204, 204, 204, 0.95).lineColor(217, 217, 217, 1.0)))

draw(p, "lowest-oecd-votes-cast", writer.FileOptions(overwrite=true))
draw(figure, "lowest-oecd-votes-cast", writer.FileOptions(overwrite=true))
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: Data labels on the plot
language: scala
suite: line_and_scatter
plot_url: https://plot.ly/~pbugnion/342
plot_url: https://plot.ly/~pbugnion/469
order: 4
sitemap: false
arrangement: horizontal
Expand Down Expand Up @@ -106,16 +106,17 @@
val xAxisOptions = commonAxisOptions.title("GDP per capita (dollars)").noZeroLine
val yAxisOptions = commonAxisOptions.title("Life expectancy (years)")

val layout = SingleAxisLayout()
.xAxisOptions(xAxisOptions)
.yAxisOptions(yAxisOptions)
.title("Life Expectancy v. Per Capita GDP, 2007")

// The plot itself
val p = Plot()
.layout(layout)
.withScatter(gdpAmerica, lifeExpectancyAmerica,
commonOptions.name("Americas").text(labelAmerica))
.withScatter(gdpEurope, lifeExpectancyEurope,
commonOptions.name("Europe").text(labelEurope))
.xAxisOptions(xAxisOptions)
.yAxisOptions(yAxisOptions)

val figure = Figure()
.plot(p) // add the plot to the figure
.title("Life Expectancy v. Per Capita GDP, 2007")

draw(p, "life-expectancy-per-GDP-2007", writer.FileOptions(overwrite=true))
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: Lines and scatter plots
language: scala
suite: line_and_scatter
plot_url: https://plot.ly/~pbugnion/338
plot_url: https://plot.ly/~pbugnion/550
order: 2
sitemap: false
arrangement: horizontal
Expand All @@ -28,4 +28,3 @@
.withScatter(xs, ys2, ScatterOptions().mode(ScatterMode.Line).name("line"))

draw(p, "scatter-mode", writer.FileOptions(overwrite=true))
// returns PlotFile(pbugnion:338,scatter-mode)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: Style scatter plots
language: scala
suite: line_and_scatter
plot_url: https://plot.ly/~pbugnion/324
plot_url: https://plot.ly/~pbugnion/552
order: 3
sitemap: false
arrangement: horizontal
Expand Down
38 changes: 12 additions & 26 deletions _posts/scala/subplots/2016-04-17-grid-subplots.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
name: A grid of subplots
plot_url: https://plot.ly/~pbugnion/334
plot_url: https://plot.ly/~pbugnion/477
language: scala
suite: subplots
order: 2
order: 3
sitemap: false
arrangement: horizontal
---
Expand All @@ -14,32 +14,18 @@
val xs = (0 to 100).map { i => Random.nextGaussian }
def ys = (0 to 100).map { i => Random.nextGaussian }

// 2 rows, 3 columns
val layout = GridLayout(2, 3)

// Options common to all the series
val commonOptions = ScatterOptions()
.mode(ScatterMode.Marker)
.marker(MarkerOptions().size(5))

val p = Plot()
.layout(layout)
.withScatter(xs, ys, commonOptions
.onAxes(layout.ref(0, 0)) // top left
.name("top-left"))
.withScatter(xs, ys, commonOptions
.onAxes(layout.ref(0, 1)) // top middle
.name("top-middle"))
.withScatter(xs, ys, commonOptions
.onAxes(layout.ref(0, 2)) // top right
.name("top-right"))
.withScatter(xs, ys, commonOptions
.onAxes(layout.ref(1, 0)) // bottom left
.name("bottom-left"))
.withScatter(xs, ys, commonOptions
.onAxes(layout.ref(1, 1)) // bottom middle
.name("bottom-middle"))
.withScatter(xs, ys, commonOptions
.onAxes(layout.ref(1, 2)) // bottom right
.name("bottom-right"))
val figure = GridFigure(2, 3) // 2 rows, 3 columsn
.plot(0, 0) { Plot().withScatter(xs, ys, commonOptions.name("top-left")) }
.plot(0, 1) { Plot().withScatter(xs, ys, commonOptions.name("top-middle")) }
.plot(0, 2) { Plot().withScatter(xs, ys, commonOptions.name("top-right")) }
.plot(1, 0) { Plot().withScatter(xs, ys, commonOptions.name("bottom-left")) }
.plot(1, 1) { Plot().withScatter(xs, ys, commonOptions.name("bottom-middle")) }
.plot(1, 2) { Plot().withScatter(xs, ys, commonOptions.name("bottom-rigth")) }


draw(p, "subplots-grid", writer.FileOptions(overwrite=true))
draw(figure, "subplots-grid", writer.FileOptions(overwrite=true))
35 changes: 35 additions & 0 deletions _posts/scala/subplots/2016-04-17-row-subplots-mixed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
name: Mixed 2D and 3D subplots
plot_url: https://plot.ly/~pbugnion/554
language: scala
suite: subplots
order: 2
sitemap: false
arrangement: horizontal
---

import co.theasi.plotly
import util.Random

// Left-hand side: Gaussian distributed random variates
val randomXs = (0 to 100).map { i => Random.nextGaussian }
val randomYs = (0 to 100).map { i => Random.nextGaussian }

val leftPlot = Plot()
.withScatter(randomXs, randomYs, ScatterOptions().mode(ScatterMode.Marker))

// Gaussian PDF
def gaussian2D(x: Double, y: Double) = Math.exp(-x*x - y*y)

val xs = (-4.0 to 4.0 by 0.1).toVector
val ys = (-4.0 to 4.0 by 0.1).toVector
val zs = xs.map { x => ys.map { y => gaussian2D(x, y) } }

val rightPlot = ThreeDPlot().withSurface(xs, ys, zs)

// Figure with two subplots in a row
val figure = RowFigure(2)
.plot(0) { leftPlot }
.plot(1) { rightPlot }

draw(figure, "mixed-subplots", writer.FileOptions(overwrite=true))
26 changes: 13 additions & 13 deletions _posts/scala/subplots/2016-04-17-row-subplots.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: Subplots in a row
plot_url: https://plot.ly/~pbugnion/330
plot_url: https://plot.ly/~pbugnion/479
language: scala
suite: subplots
order: 1
Expand All @@ -16,16 +16,16 @@
val xsRight = (0 to 100).map { i => Random.nextDouble }
val ysRight = (0 to 100).map { i => Random.nextDouble }

// 2 subplots
val layout = RowLayout(2)
val figure = RowFigure(2)
.plot(0) { // left-hand plot
Plot().withScatter(
xsLeft, ysLeft,
ScatterOptions().mode(ScatterMode.Marker).name("left"))
}
.plot(1) { // right-hand plot
Plot().withScatter(
xsLeft, ysLeft,
ScatterOptions().mode(ScatterMode.Marker).name("right"))
}

val p = Plot()
.layout(layout)
.withScatter(xsLeft, ysLeft, ScatterOptions()
.onAxes(layout.ref(0)) // left subplot
.mode(ScatterMode.Marker))
.withScatter(xsRight, ysRight, ScatterOptions()
.onAxes(layout.ref(1)) // right subplot
.mode(ScatterMode.Marker))

draw(p, "make-subplots", writer.FileOptions(overwrite=true))
draw(figure, "make-subplots", writer.FileOptions(overwrite=true))
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: 3D Subplots | Examples | Plotly
name: 3D Subplots
permalink: scala/3d-subplots/
description: How to create subplots with 3D graphs in Scala
layout: base
thumbnail: thumbnail/3d-subplots.jpg
language: scala
page_type: example_index
has_thumbnail: true
display_as: 3d_charts
order: 5
---
{% assign examples = site.posts | where:"language","scala" | where:"suite","3d-subplots" | sort: "order" %}
{% include auto_examples.html examples=examples %}
46 changes: 46 additions & 0 deletions _posts/scala/surface-subplots/2016-06-19-surface-subplots.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
name: 3D surface subplots
plot_url: https://plot.ly/~pbugnion/546
language: scala
suite: 3d-subplots
order: 2
sitemap: false
arrangement: horizontal
---

val xs = (-3.0 to 3.0 by 0.2).toVector
val ys = (-3.0 to 3.0 by 0.2).toVector

// 2-dimensional quantum harmonic oscillator energy levels
def gaussian2D(x: Double, y: Double): Double = Math.exp(-x*x - y*y)
val z00 = xs.map { x => ys.map { y => gaussian2D(x, y) } }
val z01 = xs.map { x => ys.map { y => y*gaussian2D(x, y) } }
val z11 = xs.map { x => ys.map { y => x*y*gaussian2D(x, y) } }
val z20 = xs.map { x => ys.map { y => (1-2*x*x)*gaussian2D(x, y) } }

val options = SurfaceOptions().noScale

val figure = GridFigure(2, 2) // four plots split over two rows
.title("Eigenfunctions of the 2D quantum harmonic oscillator")
.plot(0, 0) { // top left
ThreeDPlot()
.withSurface(xs, ys, z00, options)
.zAxisOptions(AxisOptions().title("psi_00"))
}
.plot(0, 1) { // top right
ThreeDPlot()
.withSurface(xs, ys, z01, options)
.zAxisOptions(AxisOptions().title("psi_01"))
}
.plot(1, 0) { // bottom left
ThreeDPlot()
.withSurface(xs, ys, z11, options)
.zAxisOptions(AxisOptions().title("psi_11"))
}
.plot(1, 1) { // bottom right
ThreeDPlot()
.withSurface(xs, ys, z20, options)
.zAxisOptions(AxisOptions().title("psi_20"))
}

draw(figure, "qho-eigenfunctions")
19 changes: 19 additions & 0 deletions _posts/scala/surface/2016-06-19-3d-surface-plot.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
name: 3D surface plot
plot_url: https://plot.ly/~pbugnion/518
language: scala
suite: surface
order: 1
sitemap: false
arrangement: horizontal
---

val xs = (-3.0 to 3.0 by 0.1).toVector
val ys = (-3.0 to 3.0 by 0.1).toVector

def gaussian2D(x: Double, y: Double) = Math.exp(-x*x - y*y)
val zs = xs.map { x => ys.map { y => gaussian2D(x, y) } }

val p = ThreeDPlot().withSurface(xs, ys, zs)

draw(p, "gaussian-surfaces")
15 changes: 15 additions & 0 deletions _posts/scala/surface/2016-06-19-3d_surface_plots_index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: 3D Surface plots | Examples | Plotly
name: 3D Surface plots
permalink: scala/3d-surface-plots/
description: How to create surface plots in Plotly with Scala.
layout: user-guide
thumbnail: thumbnail/3d-surface.jpg
language: scala
page_type: example_index
has_thumbnail: true
display_as: 3d_charts
order: 4
---
{% assign examples = site.posts | where:"language","scala" | where:"suite","surface" | sort: "order" %}
{% include auto_examples.html examples=examples %}
Loading

0 comments on commit e948627

Please sign in to comment.