Kandy로 데이터 시각화하기
Kandy로 데이터 시각화하기 (Data visualization with Kandy)
데이터를 제대로 파악하려면 눈으로 직접 보는 게 최고예요. Kotlin은 강력하고 유연한 데이터 시각화를 한곳에서 제공해서, 복잡한 모델을 만지기 전에 데이터를 직관적으로 보여주고 탐색할 수 있게 해 줘요. 이 글에서는 IntelliJ IDEA에서 Kandy와 Kotlin DataFrame 라이브러리를 사용해 여러 차트를 만드는 법을 배워 볼게요.
출처: Kotlin 공식 문서
본문
Kotlin은 강력하고 유연한 데이터 시각화를 한곳에서 해결할 수 있는 솔루션을 제공해요. 복잡한 모델로 들어가기 전에 데이터를 직관적으로 제시하고 탐색할 수 있는 방법을 제공하죠.
이 튜토리얼은 IntelliJ IDEA에서 Kandy와 Kotlin DataFrame 라이브러리를 사용해 다양한 차트 유형을 만드는 방법을 보여줘요.
시작하기 전에
IntelliJ IDEA 2026.2부터 Kotlin Notebook은 더 이상 IDE에 번들로 포함되지 않고, JetBrains에서 공식 지원하지도 않게 돼요. 소스 코드는 GitHub에 계속 남아 있어요.
자세한 내용은 블로그 포스트에서 확인할 수 있어요.
이 튜토리얼을 따라 하려면:
-
File | New | Kotlin Notebook을 선택해요.
-
노트북에서 Kandy와 Kotlin DataFrame을 가져와요.
%use kandy %use dataframe다른 코드 셀보다 먼저
%use dataframe줄이 있는 코드 셀을 실행해서, 노트북에서 DataFrame 라이브러리와 그 API를 사용할 수 있게 준비해 두세요.
DataFrame 만들기
시작하려면 시각화할 데이터가 담긴 DataFrame을 만들어 볼게요. 이 DataFrame은 베를린, 마드리드, 카라카스의 시뮬레이션된 월평균 기온을 저장해요.
// The months variable stores a list with the 12 months of the year
val months = listOf(
"January", "February",
"March", "April", "May",
"June", "July", "August",
"September", "October", "November",
"December"
)
// The tempBerlin, tempMadrid, and tempCaracas variables store a list
// with temperature values for each month
val tempBerlin =
listOf(-0.5, 0.0, 4.8, 9.0, 14.3, 17.5, 19.2, 18.9, 14.5, 9.7, 4.7, 1.0)
val tempMadrid =
listOf(6.3, 7.9, 11.2, 12.9, 16.7, 21.1, 24.7, 24.2, 20.3, 15.4, 9.9, 6.6)
val tempCaracas =
listOf(27.5, 28.9, 29.6, 30.9, 31.7, 35.1, 33.8, 32.2, 31.3, 29.4, 28.9, 27.6)
이제 새 변수(df)를 만들고 dataFrameOf() 함수를 사용해 세 개의 컬럼(Month, Temperature, City)을 가진 DataFrame을 생성해요.
val df = dataFrameOf(
"Month" to months + months + months,
"Temperature" to tempBerlin + tempMadrid + tempCaracas,
"City" to List(12) { "Berlin" } + List(12) { "Madrid" } + List(12) { "Caracas" }
)
데이터를 미리 보려면 .head() 함수를 사용해요.
df.head(4) // Returns the first four rows
우리 데이터셋에서 처음 네 개 행은 1월부터 4월까지 베를린의 기온을 저장해요.
컬럼의 레코드에 접근하는 방법은 여러 가지가 있어요. 이는 Kandy와 Kotlin DataFrame 라이브러리를 함께 사용할 때 타입 안전성을 높이는 데 도움이 돼요. 자세한 내용은 Access APIs를 참고해요.
선 차트 만들기 (Create a line chart)
앞서 만든 df DataFrame으로 선 차트를 만들어 볼게요.
-
Kandy 라이브러리의
.plot()함수를 호출해요. -
line()레이어를 적용해요. -
Month와Temperature컬럼을 각각X와Y축에 매핑해요. -
(선택) 색상과 크기를 커스터마이즈해요.
df.plot {
line {
x(Month)
y(Temperature)
color(City) {
scale = categorical(
"Berlin" to Color.hex("#6F4E37"),
"Madrid" to Color.hex("#C2D4AB"),
"Caracas" to Color.hex("#B5651D")
)
}
width = 1.5
}
layout {
size = 1000 to 450
}
}
결과는 이렇게 나와요.
점 차트 만들기 (Create a points chart)
이제 df DataFrame을 점(산점도) 차트로 시각화해 볼게요.
-
Kandy 라이브러리의
.plot()함수를 호출해요. -
points()레이어를 적용해요. -
Month와Temperature컬럼을 각각X와Y축에 매핑해요. -
(선택) 색상, 축 레이블, 점 크기, 차트 제목을 커스터마이즈해요.
df.plot {
points {
x(Month) {
axis.name = "Month"
}
y(Temperature) {
axis.name = "Temperature"
}
color(City) {
scale = categorical(
"Berlin" to Color.hex("#6F4E37"),
"Madrid" to Color.hex("#C2D4AB"),
"Caracas" to Color.hex("#B5651D")
)
}
size = 5.5
}
layout {
title = "Temperature per month"
}
}
결과는 이렇게 나와요.
막대 차트 만들기 (Create a bar chart)
마지막으로 각 도시에 대한 막대 차트를 만들어 볼게요.
-
.groupBy()함수를 사용해 DataFrame을City컬럼으로 그룹화해요. -
Kandy 라이브러리의
plot()함수를 호출해요. -
bars()레이어를 적용해요. -
(선택) 차트에 제목을 추가하고 색상을 커스터마이즈해요.
df.groupBy { City }.plot {
bars {
x(Month)
y(Temperature)
fillColor(City) {
scale = categorical(
"Berlin" to Color.hex("#6F4E37"),
"Madrid" to Color.hex("#C2D4AB"),
"Caracas" to Color.hex("#B5651D")
)
}
}
layout.title {
title = "Temperature per month"
}
}
결과는 이렇게 나와요.
다음 단계
-
Kandy 라이브러리 문서에서 더 많은 차트 예제 살펴보기
-
Lets-Plot 라이브러리 문서에서 더 고급 플로팅 옵션 살펴보기
-
Kotlin DataFrame 라이브러리 문서에서 데이터 프레임 생성·탐색·관리에 대한 추가 정보 찾아보기