Last active
May 8, 2023 20:34
-
-
Save dacr/3eea49c3da321a96d76ad388cbabffc9 to your computer and use it in GitHub Desktop.
smile visualization line charts / published by https://github.com/dacr/code-examples-manager #2ac85e0c-419d-4165-8491-9e5fc5367eba/98fa02fa8957e9f13b642571561ed187f768c667
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : smile visualization line charts | |
// keywords : smile, chart, visualization | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 2ac85e0c-419d-4165-8491-9e5fc5367eba | |
// created-on : 2021-03-05T09:23:01Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.2.2" | |
//> using dep "com.github.haifengl:smile-scala_2.13:3.0.1" | |
// --------------------- | |
import smile.plot.swing.* | |
import smile.plot.show | |
import smile.plot.Render.* | |
import java.awt.Color | |
import scala.math.* | |
// examples coming from http://haifengl.github.io/visualization.html | |
implicit val renderer:Canvas=>Unit = JWindow.apply | |
// -------------------------------------------------------------------- | |
def interval(from:Double, to:Double, count:Int):LazyList[Double] = { | |
val step = (to-from)/count | |
LazyList.iterate(from)(step.+).takeWhile(_ <= to) | |
} | |
// -------------------------------------------------------------------- | |
{ | |
val heart = -314 to 314 map { i => | |
val t = i / 100.0 | |
val x = 16 * pow(sin(t), 3) | |
val y = 13 * cos(t) - 5 * cos(2*t) - 2 * cos(3*t) - cos(4*t) | |
Array(x, y) | |
} | |
show(line(heart.toArray, color = Color.RED)) | |
} | |
// -------------------------------------------------------------------- | |
{ | |
val gauss = interval(-5, 5, 1000).map { x => | |
val y = exp(-pow(x, 2)) | |
Array(x, y) | |
} | |
show(line(gauss.toArray, color = Color.BLUE)) | |
} | |
// -------------------------------------------------------------------- | |
println("enter to exit"); scala.io.StdIn.readLine() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment