scala--使用plotly来画基本的图形
Plotly 是一个优秀的用于绘制图形的库,提供了javascript,python等的接口,本文粗略介绍它的scala接口(https://plotly.com/scala/)。
可以在jupyter lab中安装Almond(https://almond.sh/)插件来使用scala,在其中导入
import $ivy.`org.plotly-scala::plotly-almond:0.7.1`
就可以绘制图表了。
绘制散点图
val scatter1 = Scatter(
Seq(1,2,3,4,5,6),
Seq(13,24,16,23,19,23),
mode = ScatterMode(ScatterMode.Markers,ScatterMode.Text),
name = "A",
text = Seq("A-1","A-2","A-3","A-4","A-5","A-6"),
textposition = TextPosition.TopCenter,
marker = Marker(size = 16)
)
val scatter2 = Scatter(
Seq(1,2,3,4,5,6),
Seq(19,23,16,15,20,11),
mode = ScatterMode(ScatterMode.Lines,ScatterMode.Markers),
name = "B",
text = Seq("B-1","B-2","B-3","B-4","B-5","B-6"),
textposition = TextPosition.BottomCenter,
line = Line(shape=LineShape.Spline,dash=Dash.DashDot)
)
// 线的样式
val scatter3 = Scatter(
Seq(1,2,3,4,5,6),
Seq(12,13,6,13,6,9),
mode = ScatterMode(ScatterMode.Lines,ScatterMode.Markers),
name = "C",
text = Seq("C-1","C-2","C-3","C-4","C-5","C-6"),
textposition = TextPosition.MiddleRight,
marker = Marker(color=Color.RGB(219, 64, 82),size=10),
line = Line(color=Color.RGB(128, 0, 128),width=6,shape=LineShape.HV,dash=Dash.Dot)
)
val layout = Layout(
title = "散点图",
xaxis = Axis(title="时间", range=(0,7)),
yaxis = Axis(title="个数"),
width=900,
height=500
)
val data1 = Seq(scatter1,scatter2,scatter3)
plot(data1,layout) // plotly.Almond.plot
折现图
val yData = Vector(12,23,12,6,19)
val scatter11 = Scatter(
Seq(1,3,4,5,6),
yData,
mode = ScatterMode(ScatterMode.Markers,ScatterMode.Lines),
name = "A",
text = Seq("A-1","A-2","A-3","A-4","A-5","A-6"),
textposition = TextPosition.TopCenter,
marker = Marker(size = 16),
// connectgaps = true
)
val scatter22 = Scatter(
Seq(1,2,3,4,5,7),
Seq(19,23,16,15,20,18),
mode = ScatterMode(ScatterMode.Lines,ScatterMode.Markers),
name = "B",
text = Seq("B-1","B-2","B-3","B-4","B-5","B-6"),
textposition = TextPosition.BottomCenter,
line = Line(shape=LineShape.Spline,dash=Dash.DashDot),
// connectgaps = true
)
val marker = Scatter(
Seq(1,7),
Seq(19,18),
mode = ScatterMode(ScatterMode.Markers),
marker = Marker(size=16)
)
val data2 = Seq(scatter11,scatter22,marker)
val layout2 = Layout(
title = "跨越不同图",
// width = 600,
// height = 600,
// yaxis = Axis(
// showgrid=false,
// zeroline= false,
// showline= false,
// showticklabels= false
// ),
// margin = Margin(
// autoexpand= false,
// l= 100,
// r= 20,
// t= 100
// ),
// autosize = false,
annotations = Seq(
Annotation(text=s"A:${yData.head}%",xanchor=Anchor.Right,yanchor=Anchor.Middle,xref=Ref.Paper,x=0.05,y=yData.head,showarrow=false,font=Font(size=16,color=Color.RGB(255, 124, 195))), // ??? y轴的位置怎么设置准确 , 呕吼,找到了,yref 设定了就按百分比,不设定就按数值
Annotation(text=s"${yData.last}%",xanchor=Anchor.Left,yanchor=Anchor.Middle,x=6.05,y=yData.last,showarrow=false,font=Font(size=16,color=Color.StringColor("red")))
)
)
plot(data2,layout2)
5.柱状图
val item1 = Seq("A","B","C","D")
val value1 = Seq(12,-6,19,3)
val item2 = Seq("wl","zx","jwl","xx")
val value2 = Seq(3,13,8,17)
val bar1 = Bar(
x = item1,
y = value1,
name = "字母1",
text = value1.map(_.toString),
textposition = BarTextPosition.Outside,
hoverinfo = HoverInfo.none,
marker = Marker(
color = Seq(Color.RGB(204,204,204),Color.RGB(222,45,38),Color.RGB(204,204,204),Color.RGB(204,204,204)),
opacity = 0.7
)
)
val bar3 = Bar(
x = item1,
y = Seq(-6,12,9,15),
name = "字母2",
text = Seq(6,12,9,15).map(_.toString),
textposition = BarTextPosition.Inside,
hoverinfo = HoverInfo.None,
opacity = 0.3, // 透明度,
base = 2
)
val bar2 = Bar(
x = item2,
y = value2,
name = "姓名1",
text = Seq("aaaaa","bbbbb","ccccc","ddddd"),
width = Seq(0.2,0.4,0.5,0.7)
)
val bar4 = Bar(
x = item2,
y = Seq(5,12,-4,9),
name = "姓名2",
width = Seq(0.2,0.4,0.5,0.7)
)
val layoutBar1 = Layout(
barmode = BarMode.Group
)
val data1 = Seq(bar1,bar3)
plot(data1,layoutBar1)
val data2 = Seq(bar2,bar4)
val layoutBar2 = Layout(
title = "堆叠条形图",
barmode = BarMode.Stack, // Stack 只在x轴上方, Relative 根据实际值可上下方
xaxis = Axis(tickangle = -45)
)
plot(data2,layoutBar2)
上面只列出来几个简单的图形的绘制方式,可以去官网查看更多的图形绘制方式和属性设置细节。
欢迎大家一起学习scala,对于初学者可以使用《scala程序员面试算法指南》一书,根据书上的题目,自己边编写代码边体会scala语法的灵活和强大。