Last active
September 1, 2024 10:15
-
-
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/9129fa6c7fe3c42ca7a52e79fefe6b8d93c036c8
This file contains hidden or 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.4.2" | |
//> using dep "com.github.haifengl::smile-scala:3.1.1" | |
// --------------------- | |
import smile.plot.swing.* | |
import smile.plot.show | |
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