Lines
val mpgDf =
DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv")
mpgDf.head()
untitled | manufacturer | model | displ | year | cyl | trans | drv | cty | hwy | fl | class |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | audi | a4 | 18,0 | 1999 | 4 | auto(l5) | f | 18 | 29 | p | compact |
2 | audi | a4 | 18,0 | 1999 | 4 | manual(m5) | f | 21 | 29 | p | compact |
3 | audi | a4 | 2,0 | 2008 | 4 | manual(m6) | f | 20 | 31 | p | compact |
4 | audi | a4 | 2,0 | 2008 | 4 | auto(av) | f | 21 | 30 | p | compact |
5 | audi | a4 | 28,0 | 1999 | 6 | auto(l5) | f | 16 | 26 | p | compact |
Vertical, horizontal and oblique lines
val ctyMedian = mpgDf.median { cty }
val hwyMedian = mpgDf.median { hwy }
val medianColor = Color.hex("#756bb1")
mpgDf.plot {
points {
x(cty)
y(hwy)
}
vLine {
xIntercept.constant(ctyMedian)
color = medianColor
type = LineType.DASHED
}
hLine {
yIntercept.constant(hwyMedian)
color = medianColor
type = LineType.DASHED
}
smoothLine("cty", "hwy", method = SmoothMethod.Linear()) {
color = Color.hex("#de2d26")
}
}
Broken Lines
fun generateParabolicDataMap(n: Int = 25, a: Double = 1.0): Map<String, List<Double>> {
val rand = java.util.Random(42)
val x = List(2 * n + 1) { i -> a * (i - n).toDouble() / n }
val y = x.map { i -> i * i + rand.nextGaussian() }
return mapOf("x" to x, "y" to y)
}
val pDataMap = generateParabolicDataMap(a = 3.0)
val xSrc = column<Double>("x")
val ySrc = column<Double>("y")
plot(pDataMap) {
line {
x(xSrc)
y(ySrc)
}
}
plot(pDataMap) {
path {
x(xSrc)
y(ySrc)
}
}
plot(pDataMap) {
step {
x(xSrc)
y(ySrc)
}
}
And what is the difference between line
and path
?
Let's have a look at the following example:
fun generateArchimedeanDataMap(n: Int = 25, k: Double = 1.0, a: Double = 1.0): Map<String, List<Double>> {
val phi = List(n) { i -> 2.0 * PI * k * i.toDouble() / (n - 1) }
val r = phi.map { angle -> (a * angle) / (2.0 * PI) }
val x = (r zip phi).map { p -> p.first * cos(p.second) }
val y = (r zip phi).map { p -> p.first * sin(p.second) }
return mapOf("x" to x, "y" to y)
}
val aDataMap = generateArchimedeanDataMap(n = 200, k = 2.0)
val linePlot = plot(aDataMap) {
line {
x(xSrc)
y(ySrc)
}
layout.title = "Line Plot"
}
val pathPlot = plot(aDataMap) {
path {
x(xSrc)
y(ySrc)
}
layout.title = "Path Plot"
}
val segments = plot(generateArchimedeanDataMap(n = 50)) {
segments {
xBegin(xSrc)
yBegin(ySrc)
xEnd.constant(0.0)
yEnd.constant(0.0)
}
}
plotBunch {
add(linePlot, 0, 0, 350, 300)
add(pathPlot, 350, 0, 350, 300)
add(segments, 0, 300, 700, 300)
}
Last modified: 28 November 2023