Examples of plotly-scala with Almond


In [1]:
import $ivy.`org.plotly-scala::plotly-almond:0.5.4`
import plotly._, plotly.element._, plotly.layout._, plotly.Almond._

// if you want to have the plots available without an internet connection:
// init(offline=true)

// restrict the output height to avoid scrolling in output cells
repl.pprinter() = repl.pprinter().copy(defaultHeight = 3)


Out[1]:
import $ivy.$                                      

import plotly._, plotly.element._, plotly.layout._, plotly.Almond._

// if you want to have the plots available without an internet connection:
// init(offline=true)

// restrict the output height to avoid scrolling in output cells

Line Charts

Basic Line Chart


In [2]:
val trace1 = Scatter(
  Seq(1, 2, 3, 4),
  Seq(10, 15, 13, 17)
)

val trace2 = Scatter(
  Seq(1, 2, 3, 4),
  Seq(16, 5, 11, 9)
)

val data = Seq(trace1, trace2)

plot(data)


Out[2]:
trace1: Scatter = Scatter(
  Some(Doubles(List(1.0, 2.0, 3.0, 4.0))),
...
trace2: Scatter = Scatter(
  Some(Doubles(List(1.0, 2.0, 3.0, 4.0))),
...
data: Seq[Scatter] = List(
  Scatter(
...
res1_3: String = "plot-1"

Line and Scatter Plot


In [3]:
val trace1 = Scatter(
  Seq(1, 2, 3, 4),
  Seq(10, 15, 13, 17),
  mode = ScatterMode(ScatterMode.Markers)
)

val trace2 = Scatter(
  Seq(2, 3, 4, 5),
  Seq(16, 5, 11, 9),
  mode = ScatterMode(ScatterMode.Lines)
)

val trace3 = Scatter(
  Seq(1, 2, 3, 4),
  Seq(12, 9, 15, 12),
  mode = ScatterMode(ScatterMode.Lines, ScatterMode.Markers)
)

val data = Seq(trace1, trace2, trace3)

val layout = Layout(
  title = "Line and Scatter Plot"
)

plot(data, layout)


Out[3]:
trace1: Scatter = Scatter(
  Some(Doubles(List(1.0, 2.0, 3.0, 4.0))),
...
trace2: Scatter = Scatter(
  Some(Doubles(List(2.0, 3.0, 4.0, 5.0))),
...
trace3: Scatter = Scatter(
  Some(Doubles(List(1.0, 2.0, 3.0, 4.0))),
...
data: Seq[Scatter] = List(
  Scatter(
...
layout: Layout = Layout(
  Some("Line and Scatter Plot"),
...
res2_5: String = "plot-2"

Scatter Plots

Categorical Dot Plot


In [4]:
val country = Seq(
  "Switzerland (2011)",
  "Chile (2013)",
  "Japan (2014)",
  "United States (2012)",
  "Slovenia (2014)",
  "Canada (2011)",
  "Poland (2010)",
  "Estonia (2015)",
  "Luxembourg (2013)",
  "Portugal (2011)"
)

val votingPop = Seq(
  40.0, 45.7, 52.0, 53.6, 54.1, 54.2, 54.5, 54.7, 55.1, 56.6
)

val regVoters = Seq(
  49.1, 42.0, 52.7, 84.3, 51.7, 61.1, 55.3, 64.2, 91.1, 58.9
)

val trace1 = Scatter(
  votingPop,
  country,
  mode = ScatterMode(ScatterMode.Markers),
  name = "Percent of estimated voting age population",
  marker = Marker(
    color = Color.RGBA(156, 165, 196, 0.95),
    line = Line(
      color = Color.RGBA(156, 165, 196, 1.0),
      width = 1.0
    ),
    symbol = Symbol.Circle(),
    size = 16
  )
)

val trace2 = Scatter(
  regVoters,
  country,
  mode = ScatterMode(ScatterMode.Markers),
  name = "Percent of estimated registered voters",
  marker = Marker(
    color = Color.RGBA(204, 204, 204, 0.95),
    line = Line(
      color = Color.RGBA(217, 217, 217, 1.0),
      width = 1.0
    ),
    symbol = Symbol.Circle(),
    size = 16
  )
)

val data = Seq(trace1, trace2)

val layout = Layout(
  title = "Votes cast for ten lowest voting age population in OECD countries",
  xaxis = Axis(
    showgrid = false,
    showline = true,
    linecolor = Color.RGB(102, 102, 102),
    titlefont = Font(
      color = Color.RGB(204, 204, 204)
    ),
    tickfont = Font(
      color = Color.RGB(102, 102, 102)
    ),
    autotick = false,
    dtick = 10.0,
    ticks = Ticks.Outside,
    tickcolor = Color.RGB(102, 102, 102)
  ),
  margin = Margin(
    l = 140,
    r = 40,
    b = 50,
    t = 80
  ),
  legend = Legend(
    font = Font(
      size = 10
    ),
    yanchor = Anchor.Middle,
    xanchor = Anchor.Right
  ),
  width = 600,
  height = 400,
  paper_bgcolor = Color.RGB(254, 247, 234),
  plot_bgcolor = Color.RGB(254, 247, 234),
  hovermode = HoverMode.Closest
)

plot(data, layout)


Out[4]:
country: Seq[String] = List(
  "Switzerland (2011)",
...
votingPop: Seq[Double] = List(
  40.0,
...
regVoters: Seq[Double] = List(
  49.1,
...
trace1: Scatter = Scatter(
  Some(
...
trace2: Scatter = Scatter(
  Some(
...
data: Seq[Scatter] = List(
  Scatter(
...
layout: Layout = Layout(
  Some("Votes cast for ten lowest voting age population in OECD countries"),
...
res3_7: String = "plot-3"

Bar Charts

Basic Bar Chart


In [5]:
val data = Seq(
  Bar(
    Seq("giraffes", "orangutans", "monkeys"),
    Seq(20, 14, 23)
  )
)

plot(data)


Out[5]:
data: Seq[Bar] = List(
  Bar(
...
res4_1: String = "plot-4"

Grouped Bar Chart


In [6]:
val trace1 = Bar(
  Seq("giraffes", "orangutans", "monkeys"),
  Seq(20, 14, 23),
  name = "SF Zoo"
)

val trace2 = Bar(
  Seq("giraffes", "orangutans", "monkeys"),
  Seq(12, 18, 29),
  name = "LA Zoo"
)

val data = Seq(trace1, trace2)

val layout = Layout( 
  barmode = BarMode.Group
)

plot(data, layout)


Out[6]:
trace1: Bar = Bar(
  Strings(List("giraffes", "orangutans", "monkeys")),
...
trace2: Bar = Bar(
  Strings(List("giraffes", "orangutans", "monkeys")),
...
data: Seq[Bar] = List(
  Bar(
...
layout: Layout = Layout(
  None,
...
res5_4: String = "plot-5"

Bar Chart with Direct Labels


In [7]:
val xValue = Seq("Product A", "Product B", "Product C")

val yValue = Seq(20, 14, 23)

val trace1 = Bar(
  xValue,
  yValue,
  text = Seq("27% market share", "24% market share", "19% market share"),
  marker = Marker(
    color = Color.RGB(158, 202, 225),
    opacity = 0.6,
    line = Line(
      color = Color.RGB(8, 48, 107),
      width = 1.5
    )
  )
)

val data = Seq(trace1)

val annotations = xValue.zip(yValue).map {
  case (x, y) =>
    Annotation(
      x = x,
      y = y,
      text = y.toString,
      xanchor = Anchor.Center,
      yanchor = Anchor.Bottom,
      showarrow = false
    )
}

val layout = Layout(
  title = "January 2013 Sales Report",
  annotations = annotations
)

plot(data, layout)


Out[7]:
xValue: Seq[String] = List("Product A", "Product B", "Product C")
yValue: Seq[Int] = List(20, 14, 23)
trace1: Bar = Bar(
  Strings(List("Product A", "Product B", "Product C")),
...
data: Seq[Bar] = List(
  Bar(
...
annotations: Seq[Annotation] = List(
  Annotation(
...
layout: Layout = Layout(
  Some("January 2013 Sales Report"),
...
res6_6: String = "plot-6"

Customizing Individual Bar Colors


In [8]:
val defaultColor = Color.RGBA(204,204,204,1)
val highlightColor = Color.RGBA(222,45,38,0.8)

val trace1 = Bar(
  Seq("Feature A", "Feature B", "Feature C", "Feature D", "Feature E"),
  Seq(20, 14, 23, 25, 22),
  marker = Marker(
    color = Seq(
      defaultColor, highlightColor, defaultColor, defaultColor, defaultColor
    )
  )
)

val data = Seq(trace1)

val layout = Layout(
  title = "Least Used Feature"
)

plot(data, layout)


Out[8]:
defaultColor: Color.RGBA = RGBA(204, 204, 204, 1.0)
highlightColor: Color.RGBA = RGBA(222, 45, 38, 0.8)
trace1: Bar = Bar(
  Strings(List("Feature A", "Feature B", "Feature C", "Feature D", "Feature E"))...
data: Seq[Bar] = List(
  Bar(
...
layout: Layout = Layout(
  Some("Least Used Feature"),
...
res7_5: String = "plot-7"

Waterfall Bar Chart


In [9]:
val xData = Seq(
  "Product Revenue",
  "Services Revenue",
  "Total Revenue",
  "Fixed Costs",
  "Variable Costs",
  "Total Costs",
  "Total"
)

val yData = Seq(400, 660, 660, 590, 400, 400, 340)

val textList = Seq("$430K", "$260K", "$690K", "$-120K", "$-200K", "$-320K", "$370K")

//Base

val trace1 = Bar(
  x = xData,
  y = Seq(0, 430, 0, 570, 370, 370, 0),
  marker = Marker(
    color = Color.RGBA(1, 1, 1, 0.0)
  )
)

//Revenue

val trace2 = Bar(
  xData,
  Seq(430, 260, 690, 0, 0, 0, 0),
  marker = Marker(
    color = Color.RGBA(55, 128, 191, 0.7),
    line = Line(
      color = Color.RGBA(55, 128, 191, 1.0),
      width = 2.0
    )
  )
)

//Cost

val trace3 = Bar(
  xData,
  Seq(0, 0, 0, 120, 200, 320, 0),
  marker = Marker(
    color = Color.RGBA(219, 64, 82, 0.7),
    line = Line(
      color = Color.RGBA(219, 64, 82, 1.0),
      width = 2.0
    )
  )
)

//Profit

val trace4 = Bar(
  xData,
  Seq(0, 0, 0, 0, 0, 0, 370),
  marker = Marker(
    color = Color.RGBA(50,171, 96, 0.7),
    line = Line(
      color = Color.RGBA(50, 171, 96, 1.0),
      width = 2.0
    )
  )
)

val data = Seq(trace1, trace2, trace3, trace4)

val annotations = xData.zip(yData).zip(textList).map {
  case ((x, y), text) =>
    Annotation(
      x = x,
      y = y,
      text = text,
      font = Font(
        family = "Arial",
        size = 14,
        color = Color.RGBA(245, 246, 249, 1)
      ),
      showarrow = false
    )
}

val layout = Layout(
  title = "Annual Profit 2015",
  barmode = BarMode.Stack,
  paper_bgcolor = Color.RGBA(245, 246, 249, 1),
  plot_bgcolor = Color.RGBA(245, 246, 249, 1),
  width = 600,
  height = 400,
  showlegend = false,
  annotations = annotations
)

plot(data, layout)


Out[9]:
xData: Seq[String] = List(
  "Product Revenue",
...
yData: Seq[Int] = List(400, 660, 660, 590, 400, 400, 340)
textList: Seq[String] = List(
  "$430K",
...
trace1: Bar = Bar(
  Strings(
...
trace2: Bar = Bar(
  Strings(
...
trace3: Bar = Bar(
  Strings(
...
trace4: Bar = Bar(
  Strings(
...
data: Seq[Bar] = List(
  Bar(
...
annotations: Seq[Annotation] = List(
  Annotation(
...
layout: Layout = Layout(
  Some("Annual Profit 2015"),
...
res8_10: String = "plot-8"

Horizontal Bar Charts

Basic Horizontal Bar Chart


In [10]:
val data = Seq(Bar(
  Seq(20, 14, 23),
  Seq("giraffes", "orangutans", "monkeys"),
  orientation = Orientation.Horizontal
))

plot(data)


Out[10]:
data: Seq[Bar] = List(
  Bar(
...
res9_1: String = "plot-9"

Colored Bar Chart


In [11]:
val trace1 = Bar(
  Seq(20, 14, 23),
  Seq("giraffes", "orangutans", "monkeys"),
  name = "SF Zoo",
  orientation = Orientation.Horizontal,
  marker = Marker(
    color = Color.RGBA(55, 128, 191, 0.6),
    width = 1
  )
)

val trace2 = Bar(
  Seq(12, 18, 29),
  Seq("giraffes", "orangutans", "monkeys"),
  name = "LA Zoo",
  orientation = Orientation.Horizontal,
  marker = Marker(
    color = Color.RGBA(255, 153, 51, 0.6),
    width = 1
  )
)

val data = Seq(trace1, trace2)

val layout = Layout(
  title = "Colored Bar Chart",
  barmode = BarMode.Stack
)

plot(data, layout)


Out[11]:
trace1: Bar = Bar(
  Doubles(List(20.0, 14.0, 23.0)),
...
trace2: Bar = Bar(
  Doubles(List(12.0, 18.0, 29.0)),
...
data: Seq[Bar] = List(
  Bar(
...
layout: Layout = Layout(
  Some("Colored Bar Chart"),
...
res10_4: String = "plot-10"

Time Series

Time Series Chart


In [12]:
val data = Seq(
  Scatter(
    Seq("2013-10-04 22:23:00", "2013-11-04 22:23:00", "2013-12-04 22:23:00"),
    Seq(1, 3, 6)
  )
)

plot(data)


Out[12]:
data: Seq[Scatter] = List(
  Scatter(
...
res11_1: String = "plot-11"

Bubble Charts

Hover on Text Bubble Chart


In [13]:
val trace1 = Scatter(
  Seq(1, 2, 3, 4),
  Seq(10, 11, 12, 13),
  text = Seq("""A
  size = 40""", """B
  size = 60""", """C
  size = 80""", """D
  size = 100"""),
  mode = ScatterMode(ScatterMode.Markers),
  marker = Marker(
    color = Seq(Color.RGB(93, 164, 214), Color.RGB(255, 144, 14), Color.RGB(44, 160, 101), Color.RGB(255, 65, 54)),
    size = Seq(40, 60, 80, 100)
  )
)

val data = Seq(trace1)

val layout = Layout(
  title = "Bubble Chart Hover Text",
  showlegend = false,
  height = 400,
  width = 600
)

plot(data, layout)


Out[13]:
trace1: Scatter = Scatter(
  Some(Doubles(List(1.0, 2.0, 3.0, 4.0))),
...
data: Seq[Scatter] = List(
  Scatter(
...
layout: Layout = Layout(
  Some("Bubble Chart Hover Text"),
...
res12_3: String = "plot-12"

Filled Area Plots

Basic Overlaid Area Chart


In [14]:
val trace1 = Scatter(
  Seq(1, 2, 3, 4),
  Seq(0, 2, 3, 5),
  fill = Fill.ToZeroY
)

val trace2 = Scatter(
  Seq(1, 2, 3, 4),
  Seq(3, 5, 1, 7),
  fill = Fill.ToNextY
)

val data = Seq(trace1, trace2)

plot(data)


Out[14]:
trace1: Scatter = Scatter(
  Some(Doubles(List(1.0, 2.0, 3.0, 4.0))),
...
trace2: Scatter = Scatter(
  Some(Doubles(List(1.0, 2.0, 3.0, 4.0))),
...
data: Seq[Scatter] = List(
  Scatter(
...
res13_3: String = "plot-13"