I'm trying to use Kandy for a scatter plot, but I ...
# datascience
m
I'm trying to use Kandy for a scatter plot, but I get this error:
Copy code
Unequal column sizes. Expected rows count: 35. Actual column sizes:
x: 35
y: 35
x*: 31
My code:
Copy code
val clusters = clusterKMeans(3, vectors)

        plot {
            points {
                for ((cluster, clusterColor) in clusters.zip(colors)) {
                    x(cluster.map { it[0] })
                    y(cluster.map { it[1] })
                    color = clusterColor
                }
            }
        }.save("kmeans-3.png")
a
Hi! What you are trying to do is called "series approach" - you have a series of data (clusters in your case) and you want to add points of corresponding color for each series. Unfortunately, this approach is not supported in Kandy at the moment, so you need to transform your data a bit and change the code - you need 3 lists - all Xs and Ys points and the cluster name/number for the corresponding point and also add color scale from cluster name/index. here's more on that: https://kotlin.github.io/kandy/series-hack-guide.html
You need something like that:
Copy code
plot {
    points {
        x(clusters.flatMap { cluster -> cluster.map { it[0] } })
        y(clusters.flatMap { cluster -> cluster.map { it[1] } })
        color(clusters.flatMapIndexed { index, cluster -> List(cluster.size) { index } }) {
            scale = categorical(colors, colors.indices.toList())
        }
    }
}
m
I see. And how can I specify colors in a bar chart? I tried doing
Copy code
"color" to colors.keys.map {
                val r = (it[0] * 255).roundToInt()
                val g = (it[1] * 255).roundToInt()
                val b = (it[2] * 255).roundToInt()
                Color.rgb(r, g, b)
            }
and then specifying
fillColor("color")
but that didn't seem to work, instead putting the colors as strings in a legend.
a
Unfortunately, Kandy hasn't
identity
color scale, so you should provide list of cluster labels into
color
And then use scale to set how labels are mapped to
color
. Example:
Copy code
"color" to listOf("a", "b", "c", "a", etc)
Copy code
fillColor("color") {
   scale = categorical("a" to Color.RED, "b" to Color.BLUE, "c" to... )
   scale = categorical("a" to Color.RED, "b" to Color.BLUE, "c" to... )
}
In previous example I provided indices into color mapping and add scale from them