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]:
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]:
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]:
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]:
In [5]:
val data = Seq(
Bar(
Seq("giraffes", "orangutans", "monkeys"),
Seq(20, 14, 23)
)
)
plot(data)
Out[5]:
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]:
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]:
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]:
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]:
In [10]:
val data = Seq(Bar(
Seq(20, 14, 23),
Seq("giraffes", "orangutans", "monkeys"),
orientation = Orientation.Horizontal
))
plot(data)
Out[10]:
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]:
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]:
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]:
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]: