콘텐츠로 이동

hossam.hs_plot

hossam.hs_plot

set_dpi

set_dpi(dpi=DEFAULT_DPI)

전역 설정 객체의 DPI 및 폰트 크기를 설정한다.

Parameters:

Name Type Description Default
dpi int

설정할 DPI 값.

DEFAULT_DPI

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def set_dpi(dpi: int = DEFAULT_DPI) -> None:
    """
    전역 설정 객체의 DPI 및 폰트 크기를 설정한다.

    Args:
        dpi (int): 설정할 DPI 값.

    Returns:
        None
    """
    config.dpi = dpi

    if dpi > 200:
        config.font_size = config.font_size * (dpi * 0.0011 + 0.7)
        config.text_font_size = config.text_font_size * (dpi * 0.0011 + 0.7)
        config.title_font_size = config.title_font_size * (dpi * 0.0011 + 0.7)
        config.title_pad = config.title_pad * (dpi * 0.0011 + 0.7)
        config.label_font_size = config.label_font_size * (dpi * 0.0011 + 0.7)
    elif dpi > 100:
        config.font_size = config.font_size * (dpi * 0.0012 + 0.75)
        config.text_font_size = config.text_font_size * (dpi * 0.0012 + 0.75)
        config.title_font_size = config.title_font_size * (dpi * 0.0012 + 0.75)
        config.title_pad = config.title_pad * (dpi * 0.0012 + 0.75)
        config.label_font_size = config.label_font_size * (dpi * 0.0012 + 0.75)
    else:
        config.font_size = 10
        config.text_font_size = 8
        config.title_font_size = 18
        config.title_pad = 15
        config.label_font_size = 14

get_default_ax

get_default_ax(
    width=config.width,
    height=config.height,
    rows=1,
    cols=1,
    flatten=False,
    ws=None,
    hs=None,
    title=None,
)

기본 크기의 Figure와 Axes를 생성한다.

Parameters:

Name Type Description Default
width int

가로 픽셀 크기.

width
height int

세로 픽셀 크기.

height
rows int

서브플롯 행 개수.

1
cols int

서브플롯 열 개수.

1
flatten bool

Axes 배열을 1차원 리스트로 평탄화할지 여부.

False
ws int | None

서브플롯 가로 간격(wspace). rows/cols가 1보다 클 때만 적용.

None
hs int | None

서브플롯 세로 간격(hspace). rows/cols가 1보다 클 때만 적용.

None
title str | None

Figure 제목.

None

Returns:

Type Description

tuple[Figure, Axes]: 생성된 matplotlib Figure와 Axes 객체.

Source code in hossam/hs_plot.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
def get_default_ax(
    width: int = config.width,
    height: int = config.height,
    rows: int = 1,
    cols: int = 1,
    flatten: bool = False,
    ws: int | None = None,
    hs: int | None = None,
    title: str | None = None,
):
    """기본 크기의 Figure와 Axes를 생성한다.

    Args:
        width (int): 가로 픽셀 크기.
        height (int): 세로 픽셀 크기.
        rows (int): 서브플롯 행 개수.
        cols (int): 서브플롯 열 개수.
        flatten (bool): Axes 배열을 1차원 리스트로 평탄화할지 여부.
        ws (int|None): 서브플롯 가로 간격(`wspace`). rows/cols가 1보다 클 때만 적용.
        hs (int|None): 서브플롯 세로 간격(`hspace`). rows/cols가 1보다 클 때만 적용.
        title (str|None): Figure 제목.

    Returns:
        tuple[Figure, Axes]: 생성된 matplotlib Figure와 Axes 객체.
    """
    figsize = (width * cols / 100, height * rows / 100)
    #print(f"📐 Figure 크기: {figsize[0]:.2f} x {figsize[1]:.2f} 인치 (DPI: {dpi})")
    fig, ax = plt.subplots(rows, cols, figsize=figsize, dpi=config.dpi)

    # ax가 배열 (subplots)인지 단일 Axes인지 확인
    is_array = isinstance(ax, (np.ndarray, list))

    if is_array and (ws != None and hs != None):
        fig.subplots_adjust(wspace=ws, hspace=hs)

    if title and is_array:
        fig.suptitle(title, fontsize=config.font_size * 1.5, fontweight="bold", pad=15)

    if flatten == True:
        # 단일 Axes인 경우 리스트로 변환
        if rows == 1 and cols == 1:
            ax = [ax]
        else:
            ax = ax.flatten()

    # 테두리 굵기 설정
    if flatten and isinstance(ax, list):
        for a in ax:
            for spine in a.spines.values(): # type: ignore
                spine.set_linewidth(config.frame_width)
    elif isinstance(ax, np.ndarray):
        for a in ax.flat:
            for spine in a.spines.values(): # type: ignore
                spine.set_linewidth(config.frame_width)
    else:
        for spine in ax.spines.values():  # type: ignore
            spine.set_linewidth(config.frame_width)

    return fig, ax

create_figure

create_figure(
    width=config.width,
    height=config.height,
    rows=1,
    cols=1,
    flatten=False,
    ws=None,
    hs=None,
    title=None,
)

기본 크기의 Figure와 Axes를 생성한다. get_default_ax의 래퍼 함수.

Parameters:

Name Type Description Default
width int

가로 픽셀 크기.

width
height int

세로 픽셀 크기.

height
rows int

서브플롯 행 개수.

1
cols int

서브플롯 열 개수.

1
flatten bool

Axes 배열을 1차원 리스트로 평탄화할지 여부.

False
ws int | None

서브플롯 가로 간격(wspace). rows/cols가 1보다 클 때만 적용.

None
hs int | None

서브플롯 세로 간격(hspace). rows/cols가 1보다 클 때만 적용.

None
title str | None

Figure 제목.

None

Returns:

Type Description
tuple[Figure, Axes]

tuple[Figure, Axes]: 생성된 matplotlib Figure와 Axes 객체.

Source code in hossam/hs_plot.py
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
def create_figure(
    width: int = config.width,
    height: int = config.height,
    rows: int = 1,
    cols: int = 1,
    flatten: bool = False,
    ws: int | None = None,
    hs: int | None = None,
    title: str | None = None,
) -> tuple[Figure, Axes]:
    """기본 크기의 Figure와 Axes를 생성한다. get_default_ax의 래퍼 함수.

    Args:
        width (int): 가로 픽셀 크기.
        height (int): 세로 픽셀 크기.
        rows (int): 서브플롯 행 개수.
        cols (int): 서브플롯 열 개수.
        flatten (bool): Axes 배열을 1차원 리스트로 평탄화할지 여부.
        ws (int|None): 서브플롯 가로 간격(`wspace`). rows/cols가 1보다 클 때만 적용.
        hs (int|None): 서브플롯 세로 간격(`hspace`). rows/cols가 1보다 클 때만 적용.
        title (str|None): Figure 제목.

    Returns:
        tuple[Figure, Axes]: 생성된 matplotlib Figure와 Axes 객체.
    """
    fig, ax = get_default_ax(width, height, rows, cols, flatten, ws, hs, title)
    return fig, ax  # type: ignore

finalize_plot

finalize_plot(
    ax,
    callback=None,
    outparams=False,
    save_path=None,
    grid=True,
    title=None,
)

공통 후처리를 수행한다: 콜백 실행, 레이아웃 정리, 필요 시 표시/종료.

Parameters:

Name Type Description Default
ax Axes | ndarray

대상 Axes (단일 Axes 또는 subplots 배열).

required
callback Callable | None

추가 설정을 위한 사용자 콜백.

None
outparams bool

내부에서 생성한 Figure인 경우 True.

False
save_path str | None

이미지 저장 경로. None이 아니면 해당 경로로 저장.

None
grid bool

그리드 표시 여부. 기본값은 True입니다.

True
title str | None

그래프 제목.

None

Returns: None

Source code in hossam/hs_plot.py
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
def finalize_plot(
    ax: Axes | np.ndarray | list,
    callback: Callable | None = None,
    outparams: bool = False,
    save_path: str | None = None,
    grid: bool = True,
    title: str | None = None,
) -> None:
    """공통 후처리를 수행한다: 콜백 실행, 레이아웃 정리, 필요 시 표시/종료.

    Args:
        ax (Axes|np.ndarray): 대상 Axes (단일 Axes 또는 subplots 배열).
        callback (Callable|None): 추가 설정을 위한 사용자 콜백.
        outparams (bool): 내부에서 생성한 Figure인 경우 True.
        save_path (str|None): 이미지 저장 경로. None이 아니면 해당 경로로 저장.
        grid (bool): 그리드 표시 여부. 기본값은 True입니다.
        title (str|None): 그래프 제목.
    Returns:
        None
    """
    # ax가 배열 (subplots)인지 단일 Axes인지 확인
    is_array = isinstance(ax, (np.ndarray, list))

    # callback 실행
    if callback:
        if is_array:
            for a in ax.flat if isinstance(ax, np.ndarray) else ax:
                callback(a)
        else:
            callback(ax)

    # grid 설정
    if grid:
        if is_array:
            for a in ax.flat if isinstance(ax, np.ndarray) else ax:
                a.grid(True, alpha=config.grid_alpha, linewidth=config.grid_width)
        else:
            ax.grid(True, alpha=config.grid_alpha, linewidth=config.grid_width)

    plt.tight_layout()

    if title and not is_array:
        ax.set_title(title, fontsize=config.title_font_size, pad=config.title_pad)

    if save_path is not None:
        plt.savefig(save_path, bbox_inches="tight")

    if outparams:
        plt.show()
        plt.close()

show_figure

show_figure(
    ax,
    callback=None,
    outparams=False,
    save_path=None,
    grid=True,
    title=None,
)

공통 후처리를 수행한다: 콜백 실행, 레이아웃 정리, 필요 시 표시/종료. finalize_plot의 래퍼 함수.

Parameters:

Name Type Description Default
ax Axes | ndarray

대상 Axes (단일 Axes 또는 subplots 배열).

required
callback Callable | None

추가 설정을 위한 사용자 콜백.

None
outparams bool

내부에서 생성한 Figure인 경우 True.

False
save_path str | None

이미지 저장 경로. None이 아니면 해당 경로로 저장.

None
grid bool

그리드 표시 여부. 기본값은 True입니다.

True
title str | None

그래프 제목.

None

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
def show_figure(
    ax: Axes | np.ndarray,
    callback: Callable | None = None,
    outparams: bool = False,
    save_path: str | None = None,
    grid: bool = True,
    title: str | None = None,
) -> None:
    """공통 후처리를 수행한다: 콜백 실행, 레이아웃 정리, 필요 시 표시/종료.
    finalize_plot의 래퍼 함수.

    Args:
        ax (Axes|np.ndarray): 대상 Axes (단일 Axes 또는 subplots 배열).
        callback (Callable|None): 추가 설정을 위한 사용자 콜백.
        outparams (bool): 내부에서 생성한 Figure인 경우 True.
        save_path (str|None): 이미지 저장 경로. None이 아니면 해당 경로로 저장.
        grid (bool): 그리드 표시 여부. 기본값은 True입니다.
        title (str|None): 그래프 제목.

    Returns:
        None
    """
    finalize_plot(ax, callback, outparams, save_path, grid, title)

lineplot

lineplot(
    df=None,
    xname=None,
    yname=None,
    hue=None,
    title=None,
    marker=None,
    palette=None,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

선 그래프를 그린다.

Parameters:

Name Type Description Default
df DataFrame | None

시각화할 데이터.

None
xname str | Series | ndarray | list | None

x축 컬럼명 혹은 x축 값 시퀀스.

None
yname str | Series | ndarray | list | None

y축 컬럼명 혹은 y축 값 시퀀스.

None
hue str | None

범주 구분 컬럼명.

None
title str | None

그래프 제목.

None
marker str | None

마커 모양.

None
palette str | None

팔레트 이름.

None
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
save_path str | None

이미지 저장 경로. None이면 화면에 표시.

None
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None
**params

seaborn lineplot 추가 인자.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
def lineplot(
    df: DataFrame | None = None,
    xname: str | Series | np.ndarray | list | None = None,
    yname: str | Series | np.ndarray | list | None = None,
    hue: str | None = None,
    title: str | None = None,
    marker: str | None = None,
    palette: str | None = None,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """선 그래프를 그린다.

    Args:
        df (DataFrame | None): 시각화할 데이터.
        xname (str | Series | np.ndarray | list | None): x축 컬럼명 혹은 x축 값 시퀀스.
        yname (str | Series | np.ndarray | list | None): y축 컬럼명 혹은 y축 값 시퀀스.
        hue (str | None): 범주 구분 컬럼명.
        title (str | None): 그래프 제목.
        marker (str | None): 마커 모양.
        palette (str | None): 팔레트 이름.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        save_path (str|None): 이미지 저장 경로. None이면 화면에 표시.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.
        **params: seaborn lineplot 추가 인자.

    Returns:
        None
    """
    outparams = False

    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    # hue가 있을 때만 palette 사용, 없으면 color 사용
    lineplot_kwargs = {
        "data": df,
        "x": xname,
        "y": yname,
        "hue": hue,
        "marker": marker,
        "ax": ax,
    }

    if hue is not None and palette is not None:
        lineplot_kwargs["palette"] = palette
    elif hue is None and palette is not None:
        lineplot_kwargs["color"] = sb.color_palette(palette)[0]

    lineplot_kwargs.update(params)

    sb.lineplot(**lineplot_kwargs, linewidth=linewidth)
    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

boxplot

boxplot(
    df=None,
    xname=None,
    yname=None,
    title=None,
    orient="v",
    stat_test=None,
    stat_pairs=None,
    stat_text_format="star",
    stat_loc="inside",
    palette=None,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

상자그림(boxplot)을 그린다.

Parameters:

Name Type Description Default
df DataFrame | None

시각화할 데이터.

None
xname str | None

x축 범주 컬럼명.

None
yname str | None

y축 값 컬럼명.

None
title str | None

그래프 제목.

None
orient str

'v' 또는 'h' 방향.

'v'
stat_test str | None

통계 검정 방법. None이면 검정 안함. xname과 yname이 모두 지정되어야 함.

None
stat_pairs list[tuple] | None

통계 검정할 그룹 쌍 목록.

None
stat_text_format str

통계 결과 표시 형식.

'star'
stat_loc str

통계 결과 위치.

'inside'
palette str | None

팔레트 이름.

None
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
save_path str | None

이미지 저장 경로. None이면 화면에 표시.

None
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None
**params

seaborn boxplot 추가 인자.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
def boxplot(
    df: DataFrame | None = None,
    xname: str | None = None,
    yname: str | None = None,
    title: str | None = None,
    orient: str = "v",
    stat_test: str | None = None,
    stat_pairs: list[tuple] | None = None,
    stat_text_format: str = "star",
    stat_loc: str = "inside",
    palette: str | None = None,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """상자그림(boxplot)을 그린다.

    Args:
        df (DataFrame|None): 시각화할 데이터.
        xname (str|None): x축 범주 컬럼명.
        yname (str|None): y축 값 컬럼명.
        title (str|None): 그래프 제목.
        orient (str): 'v' 또는 'h' 방향.
        stat_test (str|None): 통계 검정 방법. None이면 검정 안함. xname과 yname이 모두 지정되어야 함.
        stat_pairs (list[tuple]|None): 통계 검정할 그룹 쌍 목록.
        stat_text_format (str): 통계 결과 표시 형식.
        stat_loc (str): 통계 결과 위치.
        palette (str|None): 팔레트 이름.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        save_path (str|None): 이미지 저장 경로. None이면 화면에 표시.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.
        **params: seaborn boxplot 추가 인자.

    Returns:
        None
    """
    outparams = False

    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    if xname is not None or yname is not None:
        if xname is not None and yname is None:
            orient = "h"
        elif xname is None and yname is not None:
            orient = "v"


        boxplot_kwargs = {
            "data": df,
            "x": xname,
            "y": yname,
            "orient": orient,
            "ax": ax,
            "linewidth": linewidth,
        }

        # hue 파라미터 확인 (params에 있을 수 있음)
        hue_value = params.get("hue", None)

        if hue_value is not None and palette is not None:
            boxplot_kwargs["palette"] = palette
        elif hue_value is None and palette is not None:
            boxplot_kwargs["color"] = sb.color_palette(palette)[0]

        boxplot_kwargs.update(params)
        sb.boxplot(**boxplot_kwargs)

        # 통계 검정 추가
        if stat_test is not None:
            if stat_pairs is None:
                stat_pairs = [df[xname].dropna().unique().tolist()] # type: ignore

            annotator = Annotator(
                ax, data=df, x=xname, y=yname, pairs=stat_pairs, orient=orient
            )
            annotator.configure(
                test=stat_test, text_format=stat_text_format, loc=stat_loc
            )
            annotator.apply_and_annotate()
    else:
        sb.boxplot(data=df, orient=orient, ax=ax, linewidth=linewidth, **params)  # type: ignore

    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

pvalue1_anotation

pvalue1_anotation(
    data,
    target,
    hue,
    title=None,
    pairs=None,
    test="t-test_ind",
    text_format="star",
    loc="outside",
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

boxplot의 wrapper 함수로, 상자그림에 p-value 주석을 추가한다.

Source code in hossam/hs_plot.py
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
def pvalue1_anotation(
    data: DataFrame,
    target: str,
    hue: str,
    title: str | None = None,
    pairs: list | None = None,
    test: str = "t-test_ind",
    text_format: str = "star",
    loc: str = "outside",
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """
    boxplot의 wrapper 함수로, 상자그림에 p-value 주석을 추가한다.
    """
    boxplot(
        data,
        xname=hue,
        yname=target,
        title=title,
        orient="v",
        stat_test=test,
        stat_pairs=pairs,
        stat_text_format=text_format,
        stat_loc=loc,
        palette=None,
        width=width,
        height=height,
        linewidth=linewidth,
        save_path=save_path,
        callback=callback,
        ax=ax,
        **params,
    )

kdeplot

kdeplot(
    df,
    xname=None,
    yname=None,
    hue=None,
    title=None,
    palette=None,
    fill=False,
    fill_alpha=config.fill_alpha,
    linewidth=config.line_width,
    quartile_split=False,
    width=config.width,
    height=config.height,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

커널 밀도 추정(KDE) 그래프를 그린다.

quartile_split=True일 때는 1차원 KDE(xname 지정, yname 없음)를 사분위수 구간(Q1~Q4)으로 나누어 4개의 서브플롯에 그린다.

Parameters:

Name Type Description Default
df DataFrame

시각화할 데이터.

required
xname str | None

x축 컬럼명.

None
yname str | None

y축 컬럼명.

None
hue str | None

범주 컬럼명.

None
title str | None

그래프 제목.

None
palette str | None

팔레트 이름.

None
fill bool

면적 채우기 여부.

False
fill_alpha float

채움 투명도.

fill_alpha
quartile_split bool

True면 1D KDE를 사분위수별 서브플롯으로 분할.

False
linewidth float

선 굵기.

line_width
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None
**params

seaborn kdeplot 추가 인자.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
def kdeplot(
    df: DataFrame,
    xname: str | None = None,
    yname: str | None = None,
    hue: str | None = None,
    title: str | None = None,
    palette: str | None = None,
    fill: bool = False,
    fill_alpha: float = config.fill_alpha,
    linewidth: float = config.line_width,
    quartile_split: bool = False,
    width: int = config.width,
    height: int = config.height,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """커널 밀도 추정(KDE) 그래프를 그린다.

    quartile_split=True일 때는 1차원 KDE(xname 지정, yname 없음)를
    사분위수 구간(Q1~Q4)으로 나누어 4개의 서브플롯에 그린다.

    Args:
        df (DataFrame): 시각화할 데이터.
        xname (str|None): x축 컬럼명.
        yname (str|None): y축 컬럼명.
        hue (str|None): 범주 컬럼명.
        title (str|None): 그래프 제목.
        palette (str|None): 팔레트 이름.
        fill (bool): 면적 채우기 여부.
        fill_alpha (float): 채움 투명도.
        quartile_split (bool): True면 1D KDE를 사분위수별 서브플롯으로 분할.
        linewidth (float): 선 굵기.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.
        **params: seaborn kdeplot 추가 인자.

    Returns:
        None
    """
    outparams = False

    # 사분위수 분할 전용 처리 (1D KDE만 지원)
    if quartile_split:
        if yname is not None:
            raise ValueError(
                "quartile_split은 1차원 KDE(xname)에서만 사용할 수 있습니다."
            )

        series = df[xname].dropna()
        if series.empty:
            return

        q = series.quantile([0.0, 0.25, 0.5, 0.75, 1.0]).values
        bounds = list(zip(q[:-1], q[1:]))  # [(Q0,Q1),(Q1,Q2),(Q2,Q3),(Q3,Q4)]

        fig, axes = get_default_ax(width, height, len(bounds), 1, flatten=True)
        outparams = True

        for idx, (lo, hi) in enumerate(bounds):
            subset = series[(series >= lo) & (series <= hi)]
            if subset.empty:
                continue

            # hue를 지원하려면 원본 데이터에서 해당 인덱스로 슬라이싱
            cols = [xname]
            if hue is not None and hue in df.columns:
                cols.append(hue)
            df_quartile = df.loc[subset.index, cols].copy()

            kdeplot_kwargs = {
                "data": df_quartile,
                "x": xname,
                "fill": fill,
                "ax": axes[idx],
            }

            if hue is not None and hue in df_quartile.columns:
                kdeplot_kwargs["hue"] = hue
            if fill:
                kdeplot_kwargs["alpha"] = fill_alpha
            if hue is not None and palette is not None:
                kdeplot_kwargs["palette"] = palette
            kdeplot_kwargs["linewidth"] = linewidth
            kdeplot_kwargs.update(params)

            sb.kdeplot(**kdeplot_kwargs)
            axes[idx].set_title(f"Q{idx+1}: [{lo:.3g}, {hi:.3g}]", fontsize=config.title_font_size, pad=config.title_pad) # type: ignore
            axes[idx].grid(True, alpha=config.grid_alpha, linewidth=config.grid_width) # type: ignore

        finalize_plot(axes[0], callback, outparams, save_path, True, title)
        return

    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    # 기본 kwargs 설정
    kdeplot_kwargs = {
        "data": df,
        "x": xname,
        "y": yname,
        "hue": hue,
        "fill": fill,
        "ax": ax,
    }

    # fill이 True일 때 alpha 추가
    if fill:
        kdeplot_kwargs["alpha"] = fill_alpha

    # hue가 있을 때만 palette 추가
    if hue is not None and palette is not None:
        kdeplot_kwargs["palette"] = palette

    # yname이 없을 때만 linewidth 추가 (1D KDE에서만 사용)
    if yname is None:
        kdeplot_kwargs["linewidth"] = linewidth

    # 추가 params 병합
    kdeplot_kwargs.update(params)

    sb.kdeplot(**kdeplot_kwargs)

    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

histplot

histplot(
    df,
    xname,
    hue=None,
    title=None,
    bins=None,
    kde=True,
    palette=None,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

히스토그램을 그리고 필요 시 KDE를 함께 표시한다.

Parameters:

Name Type Description Default
df DataFrame

시각화할 데이터.

required
xname str

히스토그램 대상 컬럼명.

required
hue str | None

범주 컬럼명.

None
title str | None

그래프 제목.

None
bins int | sequence | None

구간 수 또는 경계.

None
kde bool

KDE 표시 여부.

True
palette str | None

팔레트 이름.

None
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None
**params

seaborn histplot 추가 인자.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
def histplot(
    df: DataFrame,
    xname: str,
    hue: str | None = None,
    title: str | None = None,
    bins: int | None = None,
    kde: bool = True,
    palette: str | None = None,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """히스토그램을 그리고 필요 시 KDE를 함께 표시한다.

    Args:
        df (DataFrame): 시각화할 데이터.
        xname (str): 히스토그램 대상 컬럼명.
        hue (str|None): 범주 컬럼명.
        title (str|None): 그래프 제목.
        bins (int|sequence|None): 구간 수 또는 경계.
        kde (bool): KDE 표시 여부.
        palette (str|None): 팔레트 이름.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.
        **params: seaborn histplot 추가 인자.

    Returns:
        None
    """
    outparams = False

    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    if bins:
        histplot_kwargs = {
            "data": df,
            "x": xname,
            "hue": hue,
            "kde": kde,
            "bins": bins,
            "linewidth": linewidth,
            "ax": ax,
        }

        if hue is not None and palette is not None:
            histplot_kwargs["palette"] = palette
        elif hue is None and palette is not None:
            histplot_kwargs["color"] = sb.color_palette(palette)[0]

        histplot_kwargs.update(params)
        sb.histplot(**histplot_kwargs)
    else:
        histplot_kwargs = {
            "data": df,
            "x": xname,
            "hue": hue,
            "kde": kde,
            "linewidth": linewidth,
            "ax": ax,
        }

        if hue is not None and palette is not None:
            histplot_kwargs["palette"] = palette
        elif hue is None and palette is not None:
            histplot_kwargs["color"] = sb.color_palette(palette)[0]

        histplot_kwargs.update(params)
        sb.histplot(**histplot_kwargs)

    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

stackplot

stackplot(
    df,
    xname,
    hue,
    title=None,
    palette=None,
    width=config.width,
    height=config.height,
    linewidth=0.25,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

클래스 비율을 100% 누적 막대로 표현한다.

Parameters:

Name Type Description Default
df DataFrame

시각화할 데이터.

required
xname str

x축 기준 컬럼.

required
hue str

클래스 컬럼.

required
title str | None

그래프 제목.

None
palette str | None

팔레트 이름.

None
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

0.25
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None
**params

seaborn histplot 추가 인자.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
def stackplot(
    df: DataFrame,
    xname: str,
    hue: str,
    title: str | None = None,
    palette: str | None = None,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = 0.25,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """클래스 비율을 100% 누적 막대로 표현한다.

    Args:
        df (DataFrame): 시각화할 데이터.
        xname (str): x축 기준 컬럼.
        hue (str): 클래스 컬럼.
        title (str|None): 그래프 제목.
        palette (str|None): 팔레트 이름.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.
        **params: seaborn histplot 추가 인자.

    Returns:
        None
    """
    outparams = False

    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    df2 = df[[xname, hue]].copy()
    df2[xname] = df2[xname].astype(str)

    # stackplot은 hue가 필수이므로 palette를 그대로 사용
    stackplot_kwargs = {
        "data": df2,
        "x": xname,
        "hue": hue,
        "linewidth": linewidth,
        "stat": "probability",  # 전체에서의 비율로 그리기
        "multiple": "fill",  # 전체를 100%로 그리기
        "shrink": 0.8,  # 막대의 폭
        "linewidth": linewidth,
        "ax": ax,
    }

    if palette is not None:
        stackplot_kwargs["palette"] = palette

    stackplot_kwargs.update(params)

    sb.histplot(**stackplot_kwargs)

    # 그래프의 x축 항목 수 만큼 반복
    for p in ax.patches:  # type: ignore
        # 각 막대의 위치, 넓이, 높이
        left, bottom, width, height = p.get_bbox().bounds  # type: ignore
        # 막대의 중앙에 글자 표시하기
        ax.annotate(  # type: ignore
            "%0.1f%%" % (height * 100),
            xy=(left + width / 2, bottom + height / 2),
            ha="center",
            va="center",
        )

    if str(df[xname].dtype) in ["int", "int32", "int64", "float", "float32", "float64"]:
        xticks = list(df[xname].unique())
        ax.set_xticks(xticks)  # type: ignore
        ax.set_xticklabels(xticks)  # type: ignore

    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

scatterplot

scatterplot(
    df,
    xname,
    yname,
    hue=None,
    vector=None,
    outline=False,
    title=None,
    palette=None,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

산점도를 그린다.

Parameters:

Name Type Description Default
df DataFrame | None

시각화할 데이터.

required
xname str | Index

x축 컬럼.

required
yname str | Index

y축 컬럼.

required
hue str | None

범주 컬럼.

None
vector str | None

벡터 종류 컬럼.

None
outline bool

점 외곽선 표시 여부.

False
title str | None

그래프 제목.

None
palette str | None

팔레트 이름.

None
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None
**params

seaborn scatterplot 추가 인자.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
def scatterplot(
    df: DataFrame | None,
    xname: str | Index,
    yname: str | Index,
    hue=None,
    vector: str | None = None,
    outline: bool = False,
    title: str | None = None,
    palette: str | None = None,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """산점도를 그린다.

    Args:
        df (DataFrame | None): 시각화할 데이터.
        xname (str | Index): x축 컬럼.
        yname (str | Index): y축 컬럼.
        hue (str|None): 범주 컬럼.
        vector (str|None): 벡터 종류 컬럼.
        outline (bool): 점 외곽선 표시 여부.
        title (str|None): 그래프 제목.
        palette (str|None): 팔레트 이름.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.
        **params: seaborn scatterplot 추가 인자.

    Returns:
        None
    """
    outparams = False

    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    if outline and hue is not None:
        # 군집별 값의 종류별로 반복 수행
        for c in df[hue].unique():  # type: ignore
            if c == -1:
                continue

            # 한 종류만 필터링한 결과에서 두 변수만 선택
            df_c = df.loc[df[hue] == c, [xname, yname]] # type: ignore

            try:
                # 외각선 좌표 계산
                hull = ConvexHull(df_c)

                # 마지막 좌표 이후에 첫 번째 좌표를 연결
                points = np.append(hull.vertices, hull.vertices[0])

                ax.plot(  # type: ignore
                    df_c.iloc[points, 0],
                    df_c.iloc[points, 1],
                    linewidth=linewidth,
                    linestyle=":",
                )
                ax.fill(df_c.iloc[points, 0], df_c.iloc[points, 1], alpha=0.1)  # type: ignore
            except:
                pass

    # hue가 있을 때만 palette 사용, 없으면 color 사용
    scatterplot_kwargs = {
        "x": xname,
        "y": yname,
        "hue": hue,
        "linewidth": linewidth,
        "ax": ax,
    }

    if hue is not None and palette is not None:
        scatterplot_kwargs["palette"] = palette
    elif hue is None and palette is not None:
        scatterplot_kwargs["color"] = sb.color_palette(palette)[0]

    scatterplot_kwargs.update(params)

    # 백터 종류 구분 필드가 전달되지 않은 경우에는 원본 데이터를 그대로 사용
    if vector is None:
        sb.scatterplot(data=df, **scatterplot_kwargs)
    else:
        # 핵심벡터
        scatterplot_kwargs["edgecolor"] = "#ffffff"
        sb.scatterplot(data=df[df[vector] == "core"], **scatterplot_kwargs) # type: ignore

        # 외곽백터
        scatterplot_kwargs["edgecolor"] = "#000000"
        scatterplot_kwargs["s"] = 25
        scatterplot_kwargs["marker"] = "^"
        scatterplot_kwargs["linewidth"] = 0.8
        sb.scatterplot(data=df[df[vector] == "border"], **scatterplot_kwargs) # type: ignore

        # 노이즈벡터
        scatterplot_kwargs["edgecolor"] = None
        scatterplot_kwargs["s"] = 25
        scatterplot_kwargs["marker"] = "x"
        scatterplot_kwargs["linewidth"] = 2
        scatterplot_kwargs["color"] = "#ff0000"
        scatterplot_kwargs["hue"] = None
        sb.scatterplot(data=df[df[vector] == "noise"], **scatterplot_kwargs)    # type: ignore

    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

regplot

regplot(
    df,
    xname,
    yname,
    title=None,
    palette=None,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

단순 회귀선이 포함된 산점도를 그린다.

Parameters:

Name Type Description Default
df DataFrame

시각화할 데이터.

required
xname str

독립변수 컬럼.

required
yname str

종속변수 컬럼.

required
title str | None

그래프 제목.

None
palette str | None

선/점 색상.

None
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None
**params

seaborn regplot 추가 인자.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
def regplot(
    df: DataFrame,
    xname: str,
    yname: str,
    title: str | None = None,
    palette: str | None = None,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """단순 회귀선이 포함된 산점도를 그린다.

    Args:
        df (DataFrame): 시각화할 데이터.
        xname (str): 독립변수 컬럼.
        yname (str): 종속변수 컬럼.
        title (str|None): 그래프 제목.
        palette (str|None): 선/점 색상.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.
        **params: seaborn regplot 추가 인자.

    Returns:
        None
    """
    outparams = False

    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    # regplot은 hue를 지원하지 않으므로 palette를 color로 변환
    scatter_color = None
    if palette is not None:
        scatter_color = sb.color_palette(palette)[0]

    regplot_kwargs = {
        "data": df,
        "x": xname,
        "y": yname,
        "scatter_kws": {
            "s": 20,
            "linewidths": 0.5,
            "edgecolor": "w",
            "color": scatter_color,
        },
        "line_kws": {"color": "red", "linestyle": "--", "linewidth": linewidth},
        "ax": ax,
    }

    regplot_kwargs.update(params)

    sb.regplot(**regplot_kwargs)

    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

lmplot

lmplot(
    df,
    xname,
    yname,
    hue=None,
    title=None,
    palette=None,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    **params
)

seaborn lmplot으로 선형 모델 시각화를 수행한다.

Parameters:

Name Type Description Default
df DataFrame

시각화할 데이터.

required
xname str

독립변수 컬럼.

required
yname str

종속변수 컬럼.

required
hue str | None

범주 컬럼.

None
title str | None

그래프 제목.

None
palette str | None

팔레트 이름.

None
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
**params

seaborn lmplot 추가 인자.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
def lmplot(
    df: DataFrame,
    xname: str,
    yname: str,
    hue=None,
    title: str | None = None,
    palette: str | None = None,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    **params,
) -> None:
    """seaborn lmplot으로 선형 모델 시각화를 수행한다.

    Args:
        df (DataFrame): 시각화할 데이터.
        xname (str): 독립변수 컬럼.
        yname (str): 종속변수 컬럼.
        hue (str|None): 범주 컬럼.
        title (str|None): 그래프 제목.
        palette (str|None): 팔레트 이름.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        **params: seaborn lmplot 추가 인자.

    Returns:
        None
    """
    # hue가 있을 때만 palette 사용, 없으면 scatter_kws에 color 설정
    lmplot_kwargs = {
        "data": df,
        "x": xname,
        "y": yname,
        "hue": hue,
    }

    if hue is not None and palette is not None:
        lmplot_kwargs["palette"] = palette
    elif hue is None and palette is not None:
        lmplot_kwargs["scatter_kws"] = {"color": sb.color_palette(palette)[0]}

    lmplot_kwargs.update(params)

    g = sb.lmplot(**lmplot_kwargs)
    g.fig.set_size_inches(width / config.dpi, height / config.dpi)
    g.fig.set_dpi(config.dpi)

    # 회귀선에 linewidth 적용
    for ax in g.axes.flat:
        for line in ax.get_lines():
            if line.get_marker() == "o":  # 산점도는 건너뛰기
                continue
            line.set_linewidth(linewidth)

    g.fig.grid(True, alpha=config.grid_alpha, linewidth=config.grid_width)  # type: ignore

    if title:
        g.fig.suptitle(title, fontsize=config.font_size * 1.5, fontweight="bold")

    plt.tight_layout()

    if save_path is not None:
        plt.savefig(save_path, bbox_inches="tight")

    plt.show()
    plt.close()

pairplot

pairplot(
    df,
    xnames=None,
    title=None,
    diag_kind="kde",
    hue=None,
    palette=None,
    width=config.height,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    **params
)

연속형 변수의 숫자형 컬럼 쌍에 대한 관계를 그린다.

Parameters:

Name Type Description Default
df DataFrame

시각화할 데이터.

required
xnames str | list | None

대상 컬럼명. - None: 모든 연속형(숫자형) 데이터에 대해 처리. - str: 해당 컬럼에 대해서만 처리. - list: 주어진 컬럼들에 대해서만 처리. 기본값은 None.

None
title str | None

그래프 제목.

None
diag_kind str

대각선 플롯 종류('kde' 등).

'kde'
hue str | None

범주 컬럼.

None
palette str | None

팔레트 이름.

None
width int

캔버스 가로 픽셀.

height
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
dpi int

기본 크기 및 해상도(컬럼 수에 비례해 확대됨).

required
**params

seaborn pairplot 추가 인자.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
def pairplot(
    df: DataFrame,
    xnames=None,
    title: str | None = None,
    diag_kind: str = "kde",
    hue=None,
    palette: str | None = None,
    width: int = config.height,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    **params,
) -> None:
    """연속형 변수의 숫자형 컬럼 쌍에 대한 관계를 그린다.

    Args:
        df (DataFrame): 시각화할 데이터.
        xnames (str|list|None): 대상 컬럼명.
            - None: 모든 연속형(숫자형) 데이터에 대해 처리.
            - str: 해당 컬럼에 대해서만 처리.
            - list: 주어진 컬럼들에 대해서만 처리.
            기본값은 None.
        title (str|None): 그래프 제목.
        diag_kind (str): 대각선 플롯 종류('kde' 등).
        hue (str|None): 범주 컬럼.
        palette (str|None): 팔레트 이름.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        dpi (int): 기본 크기 및 해상도(컬럼 수에 비례해 확대됨).
        **params: seaborn pairplot 추가 인자.

    Returns:
        None
    """
    # xnames 파라미터 처리 (연속형 변수만, 명목형 제외)
    if xnames is None:
        # 모든 연속형(숫자형) 컬럼 선택 (명목형/카테고리 제외)
        numeric_cols = df.select_dtypes(include=[np.number]).columns
        target_cols = [col for col in numeric_cols if df[col].dtype.name != "category"]
    elif isinstance(xnames, str):
        # 문자열: 해당 컬럼만
        target_cols = [xnames]
    elif isinstance(xnames, list):
        # 리스트: 주어진 컬럼들
        target_cols = xnames
    else:
        # 기본값으로 연속형 컬럼
        numeric_cols = df.select_dtypes(include=[np.number]).columns
        target_cols = [col for col in numeric_cols if df[col].dtype.name != "category"]

    # hue 컬럼이 있으면 target_cols에 포함시키기 (pairplot 자체에서 필요)
    if hue is not None and hue not in target_cols:
        target_cols = target_cols + [hue]

    # target_cols를 포함하는 부분 데이터프레임 생성
    df_filtered = df[target_cols].copy()

    # hue가 있을 때만 palette 사용
    pairplot_kwargs = {
        "data": df_filtered,
        "hue": hue,
        "diag_kind": diag_kind,
    }

    if hue is not None and palette is not None:
        pairplot_kwargs["palette"] = palette
    # pairplot은 hue 없이 palette만 쓰는 경우가 드물어서 color로 변환 불필요

    pairplot_kwargs.update(params)

    g = sb.pairplot(**pairplot_kwargs)
    scale = len(target_cols)
    g.fig.set_size_inches(w=(width / config.dpi) * scale, h=(height / config.dpi) * scale)
    g.fig.set_dpi(config.dpi)

    if title:
        g.fig.suptitle(title, fontsize=config.font_size * 1.5, fontweight="bold")

    g.map_lower(
        func=sb.kdeplot, fill=True, alpha=config.fill_alpha
    )
    g.map_upper(func=sb.scatterplot)

    plt.tight_layout()

    if save_path is not None:
        plt.savefig(save_path, bbox_inches="tight")

    plt.show()
    plt.close()

countplot

countplot(
    df,
    xname,
    hue=None,
    title=None,
    palette=None,
    order=1,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

범주 빈도 막대그래프를 그린다.

Parameters:

Name Type Description Default
df DataFrame

시각화할 데이터.

required
xname str

범주 컬럼.

required
hue str | None

보조 범주 컬럼.

None
title str | None

그래프 제목.

None
palette str | None

팔레트 이름.

None
order int

숫자형일 때 정렬 방식(1: 값 기준, 기타: 빈도 기준).

1
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None
**params

seaborn countplot 추가 인자.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
def countplot(
    df: DataFrame,
    xname: str,
    hue=None,
    title: str | None = None,
    palette: str | None = None,
    order: int = 1,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """범주 빈도 막대그래프를 그린다.

    Args:
        df (DataFrame): 시각화할 데이터.
        xname (str): 범주 컬럼.
        hue (str|None): 보조 범주 컬럼.
        title (str|None): 그래프 제목.
        palette (str|None): 팔레트 이름.
        order (int): 숫자형일 때 정렬 방식(1: 값 기준, 기타: 빈도 기준).
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.
        **params: seaborn countplot 추가 인자.

    Returns:
        None
    """
    outparams = False
    sort = None
    if str(df[xname].dtype) in ["int", "int32", "int64", "float", "float32", "float64"]:
        if order == 1:
            sort = sorted(list(df[xname].unique()))
        else:
            sort = sorted(list(df[xname].value_counts().index))

    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    # hue가 있을 때만 palette 사용, 없으면 color 사용
    countplot_kwargs = {
        "data": df,
        "x": xname,
        "hue": hue,
        "order": sort,
        "linewidth": linewidth,
        "ax": ax,
    }

    if hue is not None and palette is not None:
        countplot_kwargs["palette"] = palette
    elif hue is None and palette is not None:
        # palette의 첫 번째 색상을 color로 사용
        countplot_kwargs["color"] = sb.color_palette(palette)[0]

    countplot_kwargs.update(params)

    sb.countplot(**countplot_kwargs)

    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

barplot

barplot(
    df,
    xname,
    yname,
    hue=None,
    title=None,
    palette=None,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

막대그래프를 그린다.

Parameters:

Name Type Description Default
df DataFrame

시각화할 데이터.

required
xname str | Index

범주 컬럼.

required
yname str | Index

값 컬럼.

required
hue str | None

보조 범주 컬럼.

None
title str | None

그래프 제목.

None
palette str | None

팔레트 이름.

None
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None
**params

seaborn barplot 추가 인자.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
def barplot(
    df: DataFrame,
    xname: str | Index,
    yname: str | Index,
    hue=None,
    title: str | None = None,
    palette: str | None = None,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """막대그래프를 그린다.

    Args:
        df (DataFrame): 시각화할 데이터.
        xname (str | Index): 범주 컬럼.
        yname (str | Index): 값 컬럼.
        hue (str|None): 보조 범주 컬럼.
        title (str|None): 그래프 제목.
        palette (str|None): 팔레트 이름.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.
        **params: seaborn barplot 추가 인자.

    Returns:
        None
    """
    outparams = False

    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    # hue가 있을 때만 palette 사용, 없으면 color 사용
    barplot_kwargs = {
        "data": df,
        "x": xname,
        "y": yname,
        "hue": hue,
        "linewidth": linewidth,
        "ax": ax,
    }

    if hue is not None and palette is not None:
        barplot_kwargs["palette"] = palette
    elif hue is None and palette is not None:
        barplot_kwargs["color"] = sb.color_palette(palette)[0]

    barplot_kwargs.update(params)

    sb.barplot(**barplot_kwargs)
    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

boxenplot

boxenplot(
    df,
    xname,
    yname,
    hue=None,
    title=None,
    palette=None,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

박스앤 위스커 확장(boxen) 플롯을 그린다.

Parameters:

Name Type Description Default
df DataFrame

시각화할 데이터.

required
xname str

범주 컬럼.

required
yname str

값 컬럼.

required
hue str | None

보조 범주 컬럼.

None
title str | None

그래프 제목.

None
palette str | None

팔레트 이름.

None
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None
**params

seaborn boxenplot 추가 인자.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
def boxenplot(
    df: DataFrame,
    xname: str,
    yname: str,
    hue=None,
    title: str | None = None,
    palette: str | None = None,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """박스앤 위스커 확장(boxen) 플롯을 그린다.

    Args:
        df (DataFrame): 시각화할 데이터.
        xname (str): 범주 컬럼.
        yname (str): 값 컬럼.
        hue (str|None): 보조 범주 컬럼.
        title (str|None): 그래프 제목.
        palette (str|None): 팔레트 이름.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.
        **params: seaborn boxenplot 추가 인자.

    Returns:
        None
    """
    outparams = False

    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    # palette은 hue가 있을 때만 사용
    boxenplot_kwargs = {
        "data": df,
        "x": xname,
        "y": yname,
        "hue": hue,
        "linewidth": linewidth,
        "ax": ax,
    }

    if hue is not None and palette is not None:
        boxenplot_kwargs["palette"] = palette

    boxenplot_kwargs.update(params)

    sb.boxenplot(**boxenplot_kwargs)
    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

violinplot

violinplot(
    df,
    xname,
    yname,
    hue=None,
    title=None,
    palette=None,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

바이올린 플롯을 그린다.

Parameters:

Name Type Description Default
df DataFrame

시각화할 데이터.

required
xname str

범주 컬럼.

required
yname str

값 컬럼.

required
hue str | None

보조 범주 컬럼.

None
title str | None

그래프 제목.

None
palette str | None

팔레트 이름.

None
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None
**params

seaborn violinplot 추가 인자.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
def violinplot(
    df: DataFrame,
    xname: str,
    yname: str,
    hue=None,
    title: str | None = None,
    palette: str | None = None,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """바이올린 플롯을 그린다.

    Args:
        df (DataFrame): 시각화할 데이터.
        xname (str): 범주 컬럼.
        yname (str): 값 컬럼.
        hue (str|None): 보조 범주 컬럼.
        title (str|None): 그래프 제목.
        palette (str|None): 팔레트 이름.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.
        **params: seaborn violinplot 추가 인자.

    Returns:
        None
    """
    outparams = False

    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    # palette은 hue가 있을 때만 사용
    violinplot_kwargs = {
        "data": df,
        "x": xname,
        "y": yname,
        "hue": hue,
        "linewidth": linewidth,
        "ax": ax,
    }

    if hue is not None and palette is not None:
        violinplot_kwargs["palette"] = palette

    violinplot_kwargs.update(params)
    sb.violinplot(**violinplot_kwargs)
    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

pointplot

pointplot(
    df,
    xname,
    yname,
    hue=None,
    title=None,
    palette=None,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

포인트 플롯을 그린다.

Parameters:

Name Type Description Default
df DataFrame

시각화할 데이터.

required
xname str

범주 컬럼.

required
yname str

값 컬럼.

required
hue str | None

보조 범주 컬럼.

None
title str | None

그래프 제목.

None
palette str | None

팔레트 이름.

None
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None
**params

seaborn pointplot 추가 인자.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
def pointplot(
    df: DataFrame,
    xname: str,
    yname: str,
    hue=None,
    title: str | None = None,
    palette: str | None = None,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """포인트 플롯을 그린다.

    Args:
        df (DataFrame): 시각화할 데이터.
        xname (str): 범주 컬럼.
        yname (str): 값 컬럼.
        hue (str|None): 보조 범주 컬럼.
        title (str|None): 그래프 제목.
        palette (str|None): 팔레트 이름.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.
        **params: seaborn pointplot 추가 인자.

    Returns:
        None
    """
    outparams = False

    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    # hue가 있을 때만 palette 사용, 없으면 color 사용
    pointplot_kwargs = {
        "data": df,
        "x": xname,
        "y": yname,
        "hue": hue,
        "linewidth": linewidth,
        "ax": ax,
    }

    if hue is not None and palette is not None:
        pointplot_kwargs["palette"] = palette
    elif hue is None and palette is not None:
        pointplot_kwargs["color"] = sb.color_palette(palette)[0]

    pointplot_kwargs.update(params)
    sb.pointplot(**pointplot_kwargs)
    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

jointplot

jointplot(
    df,
    xname,
    yname,
    hue=None,
    title=None,
    palette=None,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    **params
)

공동 분포(joint) 플롯을 그린다.

Parameters:

Name Type Description Default
df DataFrame

시각화할 데이터.

required
xname str

x축 컬럼.

required
yname str

y축 컬럼.

required
hue str | None

범주 컬럼.

None
title str | None

그래프 제목.

None
palette str | None

팔레트 이름.

None
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
**params

seaborn jointplot 추가 인자.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
def jointplot(
    df: DataFrame,
    xname: str,
    yname: str,
    hue=None,
    title: str | None = None,
    palette: str | None = None,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    **params,
) -> None:
    """공동 분포(joint) 플롯을 그린다.

    Args:
        df (DataFrame): 시각화할 데이터.
        xname (str): x축 컬럼.
        yname (str): y축 컬럼.
        hue (str|None): 범주 컬럼.
        title (str|None): 그래프 제목.
        palette (str|None): 팔레트 이름.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        **params: seaborn jointplot 추가 인자.

    Returns:
        None
    """
    # hue가 있을 때만 palette 사용
    jointplot_kwargs = {
        "data": df,
        "x": xname,
        "y": yname,
        "linewidth": linewidth,
        "hue": hue,
    }

    if hue is not None and palette is not None:
        jointplot_kwargs["palette"] = palette
    # jointplot은 hue 없이 palette만 쓰는 경우가 드물어서 color로 변환 불필요

    jointplot_kwargs.update(params)

    g = sb.jointplot(**jointplot_kwargs)
    g.fig.set_size_inches(width / config.dpi, height / config.dpi)
    g.fig.set_dpi(config.dpi)

    if title:
        g.fig.suptitle(title, fontsize=config.font_size * 1.5, fontweight="bold")

    # 중앙 및 주변 플롯에 grid 추가
    g.ax_joint.grid(True, alpha=config.grid_alpha, linewidth=config.grid_width)
    g.ax_marg_x.grid(True, alpha=config.grid_alpha, linewidth=config.grid_width)
    g.ax_marg_y.grid(True, alpha=config.grid_alpha, linewidth=config.grid_width)

    plt.tight_layout()

    if save_path is not None:
        plt.savefig(save_path, bbox_inches="tight")

    plt.show()
    plt.close()

heatmap

heatmap(
    data,
    title=None,
    palette=None,
    width=None,
    height=None,
    linewidth=0.25,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

히트맵을 그린다(값 주석 포함).

Parameters:

Name Type Description Default
data DataFrame

행렬 형태 데이터.

required
title str | None

그래프 제목.

None
palette str | None

컬러맵 이름.

None
width int | None

캔버스 가로 픽셀. None이면 자동 계산.

None
height int | None

캔버스 세로 픽셀. None이면 자동 계산.

None
linewidth float

격자 선 굵기.

0.25
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None
**params

seaborn heatmap 추가 인자.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
def heatmap(
    data: DataFrame,
    title: str | None = None,
    palette: str | None = None,
    width: int | None = None,
    height: int | None = None,
    linewidth: float = 0.25,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """히트맵을 그린다(값 주석 포함).

    Args:
        data (DataFrame): 행렬 형태 데이터.
        title (str|None): 그래프 제목.
        palette (str|None): 컬러맵 이름.
        width (int|None): 캔버스 가로 픽셀. None이면 자동 계산.
        height (int|None): 캔버스 세로 픽셀. None이면 자동 계산.
        linewidth (float): 격자 선 굵기.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.
        **params: seaborn heatmap 추가 인자.

    Returns:
        None
    """
    outparams = False

    if width == None or height == None:
        width = (config.font_size * config.dpi / 72) * 4.5 * len(data.columns)
        height = width * 0.8  # type: ignore

    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    heatmatp_kwargs = {
        "data": data,
        "annot": True,
        "cmap": palette,
        "fmt": ".2f",
        "ax": ax,
        "linewidths": linewidth,
        "annot_kws": {"size": 10},
    }

    heatmatp_kwargs.update(params)

    # heatmap은 hue를 지원하지 않으므로 cmap에 palette 사용
    sb.heatmap(**heatmatp_kwargs)

    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

kde_confidence_interval

kde_confidence_interval(
    data,
    xnames=None,
    title=None,
    clevel=0.95,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    fill=False,
    save_path=None,
    callback=None,
    ax=None,
)

각 숫자 컬럼에 대해 KDE와 t-분포 기반 신뢰구간을 그린다.

Parameters:

Name Type Description Default
data DataFrame

시각화할 데이터.

required
xnames str | list | None

대상 컬럼명. - None: 모든 연속형 데이터에 대해 처리. - str: 해당 컬럼에 대해서만 처리. - list: 주어진 컬럼들에 대해서만 처리. 기본값은 None.

None
title str | None

그래프 제목.

None
clevel float

신뢰수준(0~1).

0.95
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
fill bool

KDE 채우기 여부.

False
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
def kde_confidence_interval(
    data: DataFrame,
    xnames=None,
    title: str | None = None,
    clevel=0.95,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    fill: bool = False,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
) -> None:
    """각 숫자 컬럼에 대해 KDE와 t-분포 기반 신뢰구간을 그린다.

    Args:
        data (DataFrame): 시각화할 데이터.
        xnames (str|list|None): 대상 컬럼명.
            - None: 모든 연속형 데이터에 대해 처리.
            - str: 해당 컬럼에 대해서만 처리.
            - list: 주어진 컬럼들에 대해서만 처리.
            기본값은 None.
        title (str|None): 그래프 제목.
        clevel (float): 신뢰수준(0~1).
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        fill (bool): KDE 채우기 여부.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.

    Returns:
        None
    """
    outparams = False

    # xnames 파라미터 처리
    if xnames is None:
        # 모든 연속형(숫자형) 컬럼 선택
        target_cols = list(data.select_dtypes(include=[np.number]).columns)
    elif isinstance(xnames, str):
        # 문자열: 해당 컬럼만
        target_cols = [xnames]
    elif isinstance(xnames, list):
        # 리스트: 주어진 컬럼들
        target_cols = xnames
    else:
        # 기본값으로 전체 컬럼
        target_cols = list(data.columns)

    # 외부에서 ax를 전달하지 않은 경우 서브플롯 생성
    if ax is None:
        n_cols = len(target_cols)
        fig, axes = get_default_ax(width, height, n_cols, 1, flatten=True)
        outparams = True
    else:
        # 외부에서 ax를 전달한 경우 (시뮬레이션용)
        axes = [ax]
        outparams = False

    # 데이터 프레임의 컬럼별로 개별 서브플롯에 처리
    for idx, c in enumerate(target_cols):
        if idx >= len(axes):
            break

        current_ax = axes[idx]
        column = data[c].dropna()

        if len(column) < 2:
            continue

        dof = len(column) - 1  # 자유도
        sample_mean = column.mean()  # 표본평균
        sample_std = column.std(ddof=1)  # 표본표준편차
        sample_std_error = sample_std / sqrt(len(column))  # 표본표준오차

        # 신뢰구간
        cmin, cmax = t.interval(clevel, dof, loc=sample_mean, scale=sample_std_error)

        # 현재 컬럼에 대한 커널밀도추정
        sb.kdeplot(data=column, linewidth=linewidth, ax=current_ax, fill=fill, alpha=config.fill_alpha)  # type: ignore

        # 그래프 축의 범위
        xmin, xmax, ymin, ymax = current_ax.get_position().bounds  # type: ignore
        ymin_val, ymax_val = 0, current_ax.get_ylim()[1]    # type: ignore

        # 신뢰구간 그리기
        current_ax.plot(    # type: ignore
            [cmin, cmin], [ymin_val, ymax_val], linestyle=":", linewidth=linewidth * 0.5
        )
        current_ax.plot(    # type: ignore
            [cmax, cmax], [ymin_val, ymax_val], linestyle=":", linewidth=linewidth * 0.5
        )
        current_ax.fill_between(    # type: ignore
            [cmin, cmax], y1=ymin_val, y2=ymax_val, alpha=config.fill_alpha
        )

        # 평균 그리기
        current_ax.plot(    # type: ignore
            [sample_mean, sample_mean],
            [0, ymax_val],
            linestyle="--",
            linewidth=linewidth,
        )

        current_ax.text(    # type: ignore
            x=(cmax - cmin) / 2 + cmin,
            y=ymax_val,
            s="[%s] %0.1f ~ %0.1f" % (column.name, cmin, cmax),
            horizontalalignment="center",
            verticalalignment="bottom",
            fontdict={"color": "red"},
        )

        current_ax.grid(True, alpha=config.grid_alpha, linewidth=config.grid_width) # type: ignore

    finalize_plot(axes[0] if isinstance(axes, list) and len(axes) > 0 else ax, callback, outparams, save_path, True, title)  # type: ignore

ols_residplot

ols_residplot(
    fit,
    title=None,
    lowess=False,
    mse=False,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

잔차도를 그린다(선택적으로 MSE 범위와 LOWESS 포함).

회귀모형의 선형성을 시각적으로 평가하기 위한 그래프를 생성한다. 점들이 무작위로 흩어져 있으면 선형성 가정이 만족되며, 특정 패턴이 보이면 비선형 관계가 존재할 가능성을 시사한다.

Parameters:

Name Type Description Default
fit

회귀 모형 객체 (statsmodels의 RegressionResultsWrapper). fit.resid와 fit.fittedvalues를 통해 잔차와 적합값을 추출한다.

required
title str | None

그래프 제목.

None
lowess bool

LOWESS 스무딩 적용 여부.

False
mse bool

√MSE, 2√MSE, 3√MSE 대역선과 비율 표시 여부.

False
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
save_path str | None

저장 경로.

None
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None
**params

seaborn residplot 추가 인자.

{}

Returns:

Type Description
None

None

Examples:

from hossam import *
fit = hs_stats.ols(data, yname='target', report=False)
residplot(fit, lowess=True, mse=True)
Source code in hossam/hs_plot.py
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
def ols_residplot(
    fit,
    title: str | None = None,
    lowess: bool = False,
    mse: bool = False,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """잔차도를 그린다(선택적으로 MSE 범위와 LOWESS 포함).

    회귀모형의 선형성을 시각적으로 평가하기 위한 그래프를 생성한다.
    점들이 무작위로 흩어져 있으면 선형성 가정이 만족되며,
    특정 패턴이 보이면 비선형 관계가 존재할 가능성을 시사한다.

    Args:
        fit: 회귀 모형 객체 (statsmodels의 RegressionResultsWrapper).
             fit.resid와 fit.fittedvalues를 통해 잔차와 적합값을 추출한다.
        title (str|None): 그래프 제목.
        lowess (bool): LOWESS 스무딩 적용 여부.
        mse (bool): √MSE, 2√MSE, 3√MSE 대역선과 비율 표시 여부.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        save_path (str|None): 저장 경로.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.
        **params: seaborn residplot 추가 인자.

    Returns:
        None

    Examples:
        ```python
        from hossam import *
        fit = hs_stats.ols(data, yname='target', report=False)
        residplot(fit, lowess=True, mse=True)
        ```
    """
    outparams = False

    # fit 객체에서 잔차와 적합값 추출
    resid = fit.resid
    y_pred = fit.fittedvalues
    y = y_pred + resid  # 실제값 = 적합값 + 잔차

    if ax is None:
        fig, ax = get_default_ax(width + 150 if mse else width, height, 1, 1)  # type: ignore
        outparams = True

    sb.residplot(
        x=y_pred,
        y=resid,
        lowess=True,  # 잔차의 추세선 표시
        line_kws={"color": "red", "linewidth": linewidth * 0.7},  # 추세선 스타일
        scatter_kws={"edgecolor": "white", "alpha": config.alpha},
        **params
    )

    if mse:
        mse_val = mean_squared_error(y, y_pred)
        mse_sq = np.sqrt(mse_val)

        r1 = resid[(resid > -mse_sq) & (resid < mse_sq)].size / resid.size * 100
        r2 = resid[(resid > -2 * mse_sq) & (resid < 2 * mse_sq)].size / resid.size * 100
        r3 = resid[(resid > -3 * mse_sq) & (resid < 3 * mse_sq)].size / resid.size * 100

        mse_r = [r1, r2, r3]

        xmin, xmax = ax.get_xlim()  # type: ignore

        # 구간별 반투명 색상 채우기 (안쪽부터 바깥쪽으로, 진한 색에서 연한 색으로)
        colors = ["red", "green", "blue"]
        alphas = [0.15, 0.10, 0.05]  # 안쪽이 더 진하게

        # 3σ 영역 (가장 바깥쪽, 가장 연함)
        ax.axhspan(-3 * mse_sq, 3 * mse_sq, facecolor=colors[2], alpha=alphas[2], zorder=0)  # type: ignore
        # 2σ 영역 (중간)
        ax.axhspan(-2 * mse_sq, 2 * mse_sq, facecolor=colors[1], alpha=alphas[1], zorder=1)  # type: ignore
        # 1σ 영역 (가장 안쪽, 가장 진함)
        ax.axhspan(-mse_sq, mse_sq, facecolor=colors[0], alpha=alphas[0], zorder=2)  # type: ignore

        # 경계선 그리기
        for i, c in enumerate(["red", "green", "blue"]):
            ax.axhline(mse_sq * (i + 1), color=c, linestyle="--", linewidth=linewidth / 2)  # type: ignore
            ax.axhline(mse_sq * (-(i + 1)), color=c, linestyle="--", linewidth=linewidth / 2)  # type: ignore

        target = [68, 95, 99.7]
        for i, c in enumerate(["red", "green", "blue"]):
            ax.text(  # type: ignore
                s=f"{i+1} sqrt(MSE) = {mse_r[i]:.2f}% ({mse_r[i] - target[i]:.2f}%)",
                x=xmax + 0.05,
                y=(i + 1) * mse_sq,
                color=c,
            )
            ax.text(  # type: ignore
                s=f"-{i+1} sqrt(MSE) = {mse_r[i]:.2f}% ({mse_r[i] - target[i]:.2f}%)",
                x=xmax + 0.05,
                y=-(i + 1) * mse_sq,
                color=c,
            )

    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

ols_qqplot

ols_qqplot(
    fit,
    title=None,
    line="s",
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

표준화된 잔차의 정규성 확인을 위한 QQ 플롯을 그린다.

statsmodels의 qqplot 함수를 사용하여 최적화된 Q-Q plot을 생성한다. 이론적 분위수와 표본 분위수를 비교하여 잔차의 정규성을 시각적으로 평가한다.

Parameters:

Name Type Description Default
fit

회귀 모형 객체 (statsmodels의 RegressionResultsWrapper 등). fit.resid 속성을 통해 잔차를 추출하여 정규성을 확인한다.

required
title str | None

그래프 제목.

None
line str

참조선의 유형. 기본값 's' (standardized). - 's': 표본의 표준편차와 평균을 기반으로 조정된 선 (권장) - 'r': 실제 점들에 대한 회귀선 (데이터 추세 반영) - 'q': 1사분위수와 3사분위수를 통과하는 선 - '45': 45도 대각선 (이론적 정규분포)

's'
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
save_path str | None

저장 경로.

None
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None
**params

statsmodels qqplot 추가 인자.

{}

Returns:

Type Description
None

None

Examples:

from hossam import *
# 선형회귀 모형 적합
fit = hs_stats.ols(data, yname='target', report=False)
# 표준화된 선 (권장)
qqplot(fit)
# 회귀선 (데이터 추세 반영)
qqplot(fit, line='r')
# 45도 대각선 (전통적 방식)
qqplot(fit, line='45')
Source code in hossam/hs_plot.py
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
def ols_qqplot(
    fit,
    title: str | None = None,
    line: str = "s",
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """표준화된 잔차의 정규성 확인을 위한 QQ 플롯을 그린다.

    statsmodels의 qqplot 함수를 사용하여 최적화된 Q-Q plot을 생성한다.
    이론적 분위수와 표본 분위수를 비교하여 잔차의 정규성을 시각적으로 평가한다.

    Args:
        fit: 회귀 모형 객체 (statsmodels의 RegressionResultsWrapper 등).
             fit.resid 속성을 통해 잔차를 추출하여 정규성을 확인한다.
        title (str|None): 그래프 제목.
        line (str): 참조선의 유형. 기본값 's' (standardized).
                    - 's': 표본의 표준편차와 평균을 기반으로 조정된 선 (권장)
                    - 'r': 실제 점들에 대한 회귀선 (데이터 추세 반영)
                    - 'q': 1사분위수와 3사분위수를 통과하는 선
                    - '45': 45도 대각선 (이론적 정규분포)
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        save_path (str|None): 저장 경로.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.
        **params: statsmodels qqplot 추가 인자.

    Returns:
        None

    Examples:
        ```python
        from hossam import *
        # 선형회귀 모형 적합
        fit = hs_stats.ols(data, yname='target', report=False)
        # 표준화된 선 (권장)
        qqplot(fit)
        # 회귀선 (데이터 추세 반영)
        qqplot(fit, line='r')
        # 45도 대각선 (전통적 방식)
        qqplot(fit, line='45')
        ```
    """
    outparams = False

    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    # fit 객체에서 잔차(residuals) 추출
    residuals = fit.resid

    # markersize 기본값 설정 (기존 크기의 2/3)
    if "markersize" not in params:
        params["markersize"] = 2

    # statsmodels의 qqplot 사용 (더 전문적이고 최적화된 구현)
    # line 옵션으로 다양한 참조선 지원
    sm_qqplot(residuals, line=line, ax=ax, **params)

    # 점의 스타일 개선: 연한 내부, 진한 테두리
    for collection in ax.collections:  # type: ignore
        # PathCollection (scatter plot의 점들)
        collection.set_facecolor("#4A90E2")  # 연한 파란색 내부
        collection.set_edgecolor("#1E3A8A")  # 진한 파란색 테두리
        collection.set_linewidth(0.8)  # 테두리 굵기
        collection.set_alpha(0.7)  # 약간의 투명도

    # 선 굵기 조정
    for line in ax.get_lines():  # type: ignore
        line.set_linewidth(linewidth)  # type: ignore

    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

distribution_by_class

distribution_by_class(
    data,
    title=None,
    xnames=None,
    hue=None,
    type="kde",
    bins=5,
    palette=None,
    fill=False,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
)

클래스별로 각 숫자형 특징의 분포를 KDE 또는 히스토그램으로 그린다.

Parameters:

Name Type Description Default
data DataFrame

시각화할 데이터.

required
xnames list | None

대상 컬럼 목록(None이면 전 컬럼).

None
hue str | None

클래스 컬럼.

None
title str | None

그래프 제목.

None
type str

'kde' | 'hist' | 'histkde'.

'kde'
bins int | sequence | None

히스토그램 구간.

5
palette str | None

팔레트 이름.

None
fill bool

KDE 채움 여부.

False
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
callback Callable | None

Axes 후처리 콜백.

None

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
def distribution_by_class(
    data: DataFrame,
    title: str | None = None,
    xnames: list | None = None,
    hue: str | None = None,
    type: str = "kde",
    bins: list[int] | int = 5,
    palette: str | None = None,
    fill: bool = False,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
) -> None:
    """클래스별로 각 숫자형 특징의 분포를 KDE 또는 히스토그램으로 그린다.

    Args:
        data (DataFrame): 시각화할 데이터.
        xnames (list|None): 대상 컬럼 목록(None이면 전 컬럼).
        hue (str|None): 클래스 컬럼.
        title (str|None): 그래프 제목.
        type (str): 'kde' | 'hist' | 'histkde'.
        bins (int|sequence|None): 히스토그램 구간.
        palette (str|None): 팔레트 이름.
        fill (bool): KDE 채움 여부.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        callback (Callable|None): Axes 후처리 콜백.

    Returns:
        None
    """
    if xnames is None:
        xnames = data.columns  # type: ignore

    for i, v in enumerate(xnames):  # type: ignore
        # 종속변수이거나 숫자형이 아닌 경우는 제외
        if v == hue or data[v].dtype not in [
            "int",
            "int32",
            "int64",
            "float",
            "float32",
            "float64",
        ]:
            continue

        if type == "kde":
            kdeplot(
                df=data,
                xname=v,
                hue=hue,
                palette=palette,
                fill=fill,
                width=width,
                height=height,
                linewidth=linewidth,
                callback=callback,
                save_path=save_path,
            )
        elif type == "hist":
            histplot(
                df=data,
                xname=v,
                hue=hue,
                bins=bins,  # type: ignore
                kde=False,
                palette=palette,
                width=width,
                height=height,
                linewidth=linewidth,
                callback=callback,
                save_path=save_path,
            )
        elif type == "histkde":
            histplot(
                df=data,
                xname=v,
                hue=hue,
                bins=bins,  # type: ignore
                kde=True,
                palette=palette,
                width=width,
                height=height,
                linewidth=linewidth,
                callback=callback,
                save_path=save_path,
            )

scatter_by_class

scatter_by_class(
    data,
    yname,
    group=None,
    hue=None,
    title=None,
    palette=None,
    outline=False,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
)

종속변수(y)와 각 연속형 독립변수(x) 간 산점도/볼록껍질을 그린다.

Parameters:

Name Type Description Default
data DataFrame

시각화할 데이터.

required
yname str

종속변수 컬럼명(필수).

required
group list | None

x 컬럼 목록 또는 [[x, y], ...] 형태. None이면 자동 생성.

None
hue str | None

클래스 컬럼.

None
title str | None

그래프 제목.

None
palette str | None

팔레트 이름.

None
outline bool

볼록 껍질을 표시할지 여부.

False
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
callback Callable | None

Axes 후처리 콜백.

None

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
def scatter_by_class(
    data: DataFrame,
    yname: str,
    group: list | None = None,
    hue: str | None = None,
    title: str | None = None,
    palette: str | None = None,
    outline: bool = False,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
) -> None:
    """종속변수(y)와 각 연속형 독립변수(x) 간 산점도/볼록껍질을 그린다.

    Args:
        data (DataFrame): 시각화할 데이터.
        yname (str): 종속변수 컬럼명(필수).
        group (list|None): x 컬럼 목록 또는 [[x, y], ...] 형태. None이면 자동 생성.
        hue (str|None): 클래스 컬럼.
        title (str|None): 그래프 제목.
        palette (str|None): 팔레트 이름.
        outline (bool): 볼록 껍질을 표시할지 여부.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        callback (Callable|None): Axes 후처리 콜백.

    Returns:
        None
    """

    # 자동 생성: yname 제외, hue 제외, 연속형만
    if group is None:
        group = []

        numeric_cols = list(data.select_dtypes(include=[np.number]).columns)
        xnames = [
            col
            for col in numeric_cols
            if col not in [yname, hue]
            and data[col].dtype.name not in ["category", "bool", "boolean"]
        ]

        for v in xnames:
            group.append([v, yname])
    else:
        # 사용자가 지정한 경우: 문자열 리스트면 yname과 페어링, 이미 페어면 그대로 사용
        processed = []
        for item in group:
            if isinstance(item, (list, tuple)) and len(item) == 2:
                processed.append(list(item))
            else:
                processed.append([item, yname])
        group = processed

    for v in group:
        scatterplot(data=data, xname=v[0], yname=v[1], outline=outline, hue=hue, palette=palette, width=width, height=height, linewidth=linewidth, dpi=dpi, callback=callback, save_path=save_path)  # type: ignore

categorical_target_distribution

categorical_target_distribution(
    data,
    yname,
    hue=None,
    title=None,
    kind="box",
    kde_fill=True,
    palette=None,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    cols=2,
    save_path=None,
    callback=None,
)

명목형 변수별로 종속변수 분포 차이를 시각화한다.

Parameters:

Name Type Description Default
data DataFrame

시각화할 데이터.

required
yname str

종속변수 컬럼명(연속형 추천).

required
hue list | str | None

명목형 독립변수 목록. None이면 자동 탐지.

None
title str | None

그래프 제목.

None
kind str

'box', 'violin', 'kde'.

'box'
kde_fill bool

kind='kde'일 때 영역 채우기 여부.

True
palette str | None

팔레트 이름.

None
width int

개별 서브플롯 가로 픽셀.

width
height int

개별 서브플롯 세로 픽셀.

height
linewidth float

선 굵기.

line_width
cols int

서브플롯 열 수.

2
callback Callable | None

Axes 후처리 콜백.

None

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
def categorical_target_distribution(
    data: DataFrame,
    yname: str,
    hue: list | str | None = None,
    title: str | None = None,
    kind: str = "box",
    kde_fill: bool = True,
    palette: str | None = None,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    cols: int = 2,
    save_path: str | None = None,
    callback: Callable | None = None,
) -> None:
    """명목형 변수별로 종속변수 분포 차이를 시각화한다.

    Args:
        data (DataFrame): 시각화할 데이터.
        yname (str): 종속변수 컬럼명(연속형 추천).
        hue (list|str|None): 명목형 독립변수 목록. None이면 자동 탐지.
        title (str|None): 그래프 제목.
        kind (str): 'box', 'violin', 'kde'.
        kde_fill (bool): kind='kde'일 때 영역 채우기 여부.
        palette (str|None): 팔레트 이름.
        width (int): 개별 서브플롯 가로 픽셀.
        height (int): 개별 서브플롯 세로 픽셀.
        linewidth (float): 선 굵기.
        cols (int): 서브플롯 열 수.
        callback (Callable|None): Axes 후처리 콜백.

    Returns:
        None
    """

    # 명목형 컬럼 후보: object, category, bool
    if hue is None:
        cat_cols = data.select_dtypes(
            include=["object", "category", "bool", "boolean"]
        ).columns
        target_cols = [c for c in cat_cols if c != yname]
    elif isinstance(hue, str):
        target_cols = [hue]
    else:
        target_cols = list(hue)

    if len(target_cols) == 0:
        return

    n_plots = len(target_cols)
    rows = (n_plots + cols - 1) // cols

    fig, axes = get_default_ax(width, height, rows, cols, dpi, flatten=True) # type: ignore
    outparams = True

    for idx, col in enumerate(target_cols):
        if idx >= len(axes):
            break

        ax = axes[idx]
        plot_kwargs = {
            "data": data.dropna(subset=[col, yname]),
            "ax": ax,
        }

        if kind == "violin":
            plot_kwargs.update({"x": col, "y": yname, "palette": palette})
            sb.violinplot(**plot_kwargs, linewidth=linewidth)
        elif kind == "kde":
            plot_kwargs.update(
                {
                    "x": yname,
                    "hue": col,
                    "palette": palette,
                    "fill": kde_fill,
                    "common_norm": False,
                    "linewidth": linewidth,
                }
            )
            sb.kdeplot(**plot_kwargs)
        else:  # box
            plot_kwargs.update({"x": col, "y": yname, "hue": col, "palette": palette})
            sb.boxplot(**plot_kwargs, linewidth=linewidth)

        ax.set_title(f"{col} vs {yname}", fontsize=config.title_font_size, pad=config.title_pad)  # type: ignore

    # 불필요한 빈 축 숨기기
    for j in range(n_plots, len(axes)):
        axes[j].set_visible(False) # type: ignore

    finalize_plot(axes[0], callback, outparams, save_path, True, title)

roc_curve_plot

roc_curve_plot(
    fit,
    y=None,
    X=None,
    title=None,
    width=config.height,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
    ax=None,
)

로지스틱 회귀 적합 결과의 ROC 곡선을 시각화한다.

Parameters:

Name Type Description Default
fit

statsmodels Logit 결과 객체 (fit.predict()로 예측 확률을 계산 가능해야 함).

required
y array - like | None

외부 데이터의 실제 레이블. 제공 시 이를 실제값으로 사용.

None
X array - like | None

외부 데이터의 설계행렬(독립변수). 제공 시 해당 데이터로 예측 확률 계산.

None
title str | None

그래프 제목.

None
width int

캔버스 가로 픽셀.

height
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes. None이면 새로 생성.

None
Notes
  • 실제값: y가 주어지면 이를 사용, 없으면 fit.model.endog를 사용합니다.
  • 예측 확률: X가 주어지면 fit.predict(X)를 사용, 없으면 fit.predict(fit.model.exog)를 사용합니다.

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
def roc_curve_plot(
    fit,
    y: np.ndarray | Series | None = None,
    X: DataFrame | np.ndarray | None = None,
    title: str | None = None,
    width: int = config.height,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
) -> None:
    """로지스틱 회귀 적합 결과의 ROC 곡선을 시각화한다.

    Args:
        fit: statsmodels Logit 결과 객체 (`fit.predict()`로 예측 확률을 계산 가능해야 함).
        y (array-like|None): 외부 데이터의 실제 레이블. 제공 시 이를 실제값으로 사용.
        X (array-like|None): 외부 데이터의 설계행렬(독립변수). 제공 시 해당 데이터로 예측 확률 계산.
        title (str|None): 그래프 제목.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes. None이면 새로 생성.

    Notes:
        - 실제값: `y`가 주어지면 이를 사용, 없으면 `fit.model.endog`를 사용합니다.
        - 예측 확률: `X`가 주어지면 `fit.predict(X)`를 사용, 없으면 `fit.predict(fit.model.exog)`를 사용합니다.

    Returns:
        None
    """
    outparams = False
    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    # 실제값(y_true) 결정
    if y is not None:
        y_true = np.asarray(y)
    else:
        # 학습 데이터의 종속변수 사용
        y_true = np.asarray(fit.model.endog)

    # 예측 확률 결정
    if X is not None:
        y_pred_proba = np.asarray(fit.predict(X))
    else:
        y_pred_proba = np.asarray(fit.predict(fit.model.exog))

    # ROC 곡선 계산
    fpr, tpr, thresholds = roc_curve(y_true, y_pred_proba)
    roc_auc = auc(fpr, tpr)

    # ROC 곡선 그리기
    ax.plot(fpr, tpr, color="darkorange", lw=linewidth, label=f"ROC curve (AUC = {roc_auc:.4f})")  # type: ignore
    ax.plot([0, 1], [0, 1], color="navy", lw=linewidth, linestyle="--", label="Random Classifier")  # type: ignore

    ax.set_xlim([0.0, 1.0])  # type: ignore
    ax.set_ylim([0.0, 1.05])  # type: ignore
    ax.set_xlabel("위양성율 (False Positive Rate)", fontsize=config.label_font_size)  # type: ignore
    ax.set_ylabel("재현율 (True Positive Rate)", fontsize=config.label_font_size)  # type: ignore
    ax.set_title("ROC 곡선", fontsize=config.title_font_size, pad=config.title_pad)  # type: ignore
    ax.legend(loc="lower right", fontsize=config.label_font_size)  # type: ignore
    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

confusion_matrix_plot

confusion_matrix_plot(
    fit,
    title=None,
    threshold=0.5,
    width=config.width,
    height=config.height,
    save_path=None,
    callback=None,
    ax=None,
)

로지스틱 회귀 적합 결과의 혼동행렬을 시각화한다.

Parameters:

Name Type Description Default
fit

statsmodels Logit 결과 객체 (fit.predict()로 예측 확률을 계산 가능해야 함).

required
title str | None

그래프 제목.

None
threshold float

예측 확률을 이진 분류로 변환할 임계값. 기본값 0.5.

0.5
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes. None이면 새로 생성.

None

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
def confusion_matrix_plot(
    fit,
    title: str | None = None,
    threshold: float = 0.5,
    width: int = config.width,
    height: int = config.height,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
) -> None:
    """로지스틱 회귀 적합 결과의 혼동행렬을 시각화한다.

    Args:
        fit: statsmodels Logit 결과 객체 (`fit.predict()`로 예측 확률을 계산 가능해야 함).
        title (str|None): 그래프 제목.
        threshold (float): 예측 확률을 이진 분류로 변환할 임계값. 기본값 0.5.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes. None이면 새로 생성.

    Returns:
        None
    """
    outparams = False
    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    # 학습 데이터 기반 실제값/예측 확률 결정
    y_true = np.asarray(fit.model.endog)
    y_pred_proba = np.asarray(fit.predict(fit.model.exog))
    y_pred = (y_pred_proba >= threshold).astype(int)

    # 혼동행렬 계산
    cm = confusion_matrix(y_true, y_pred)

    # 혼동행렬 시각화
    disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=["음성", "양성"])
    # 가독성을 위해 텍스트 크기/굵기 조정
    disp.plot(
        ax=ax,
        cmap="Blues",
        values_format="d",
        text_kw={"fontsize": 16, "weight": "bold"},
    )

    ax.set_title(f"혼동행렬 (임계값: {threshold})", fontsize=config.title_font_size, pad=config.title_pad)  # type: ignore

    finalize_plot(ax, callback, outparams, save_path, False, title)  # type: ignore

radarplot

radarplot(
    df,
    columns=None,
    hue=None,
    title=None,
    normalize=True,
    fill=True,
    fill_alpha=0.25,
    palette=None,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
    ax=None,
    **params
)

레이더 차트(방사형 차트)를 그린다.

Parameters:

Name Type Description Default
df DataFrame

시각화할 데이터.

required
columns list | None

레이더 차트에 표시할 컬럼 목록. None이면 모든 숫자형 컬럼 사용.

None
hue str | None

집단 구분 컬럼. None이면 각 행을 개별 객체로 표시.

None
title str | None

그래프 제목.

None
normalize bool

0-1 범위로 정규화 여부. 기본값 True.

True
fill bool

영역 채우기 여부.

True
fill_alpha float

채움 투명도.

0.25
palette str | None

팔레트 이름.

None
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes.

None
**params

추가 플롯 옵션.

{}

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
def radarplot(
    df: DataFrame,
    columns: list | None = None,
    hue: str | None = None,
    title: str | None = None,
    normalize: bool = True,
    fill: bool = True,
    fill_alpha: float = 0.25,
    palette: str | None = None,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
    **params,
) -> None:
    """레이더 차트(방사형 차트)를 그린다.

    Args:
        df (DataFrame): 시각화할 데이터.
        columns (list|None): 레이더 차트에 표시할 컬럼 목록. None이면 모든 숫자형 컬럼 사용.
        hue (str|None): 집단 구분 컬럼. None이면 각 행을 개별 객체로 표시.
        title (str|None): 그래프 제목.
        normalize (bool): 0-1 범위로 정규화 여부. 기본값 True.
        fill (bool): 영역 채우기 여부.
        fill_alpha (float): 채움 투명도.
        palette (str|None): 팔레트 이름.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes.
        **params: 추가 플롯 옵션.

    Returns:
        None
    """
    outparams = False

    # 컬럼 선택
    if columns is None:
        # 숫자형 컬럼만 선택 (hue 제외)
        numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
        if hue is not None and hue in numeric_cols:
            numeric_cols.remove(hue)
        columns = numeric_cols

    if len(columns) == 0:
        raise ValueError("레이더 차트에 표시할 숫자형 컬럼이 없습니다.")

    # 데이터 준비
    if hue is not None:
        # 집단별 평균 계산
        plot_data = df.groupby(hue)[columns].mean()
        labels = plot_data.index.tolist()
    else:
        # 각 행을 개별 객체로 사용
        plot_data = df[columns].copy()
        if plot_data.index.name:
            labels = plot_data.index.tolist()
        else:
            labels = [f"Row {i}" for i in range(len(plot_data))]

    # 정규화
    if normalize:
        for col in columns:
            min_val = plot_data[col].min()
            max_val = plot_data[col].max()
            if max_val - min_val > 0:
                plot_data[col] = (plot_data[col] - min_val) / (max_val - min_val)
            else:
                plot_data[col] = 0.5

    # Axes 생성 (polar projection)
    if ax is None:
        fig = plt.figure(figsize=(width / 100, height / 100), dpi=config.dpi)
        ax = fig.add_subplot(111, projection="polar")
        outparams = True

    # 각도 계산
    num_vars = len(columns)
    angles = np.linspace(0, 2 * np.pi, num_vars, endpoint=False).tolist()
    angles += angles[:1]  # 닫힌 도형을 만들기 위해 첫 번째 각도 추가

    # 색상 팔레트 설정
    if palette is not None:
        colors = sb.color_palette(palette, len(labels))
    else:
        colors = sb.color_palette("husl", len(labels))

    # 각 집단/객체별로 플롯
    for idx, (label_name, row) in enumerate(plot_data.iterrows()):
        values = row.tolist()
        values += values[:1]  # 닫힌 도형을 만들기 위해 첫 번째 값 추가

        color = colors[idx]

        # 선 그리기
        ax.plot(
            angles,
            values,
            "o-",
            linewidth=linewidth,
            label=str(label_name),
            color=color,
            **params,
        )

        # 영역 채우기
        if fill:
            ax.fill(angles, values, alpha=fill_alpha, color=color)

    # 축 레이블 설정
    ax.set_xticks(angles[:-1])
    ax.set_xticklabels(columns)

    # y축 범위 설정
    if normalize:
        ax.set_ylim(0, 1)

    # 범례
    if len(labels) <= 10:  # 너무 많으면 범례 생략
        ax.legend(loc="upper right", bbox_to_anchor=(1.3, 1.1))

    # 제목
    if hue is not None:
        title = title if title else f"Radar Chart by {hue}"
    else:
        title = title if title else "Radar Chart"

    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

distribution_plot

distribution_plot(
    data,
    column,
    clevel=0.95,
    orient="h",
    hue=None,
    kind="boxplot",
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
)

연속형 데이터의 분포를 KDE와 Boxplot으로 시각화한다.

1행 2열의 서브플롯을 생성하여: - 왼쪽: KDE with 신뢰구간 - 오른쪽: Boxplot

Parameters:

Name Type Description Default
data DataFrame

시각화할 데이터.

required
column str

분석할 컬럼명.

required
clevel float

KDE 신뢰수준 (0~1). 기본값 0.95.

0.95
orient str

Boxplot 방향 ('v' 또는 'h'). 기본값 'h'.

'h'
hue str | None

명목형 컬럼명. 지정하면 각 범주별로 행을 늘려 KDE와 boxplot을 그림.

None
kind str

두 번째 그래프의 유형 (boxplot, hist). 기본값 "boxplot".

'boxplot'
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
save_path str | None

저장 경로.

None
callback Callable | None

Axes 후처리 콜백.

None

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
def distribution_plot(
    data: DataFrame,
    column: str | list[str],
    clevel: float = 0.95,
    orient: str = "h",
    hue: str | None = None,
    kind: str = "boxplot",
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
) -> None:
    """연속형 데이터의 분포를 KDE와 Boxplot으로 시각화한다.

    1행 2열의 서브플롯을 생성하여:
    - 왼쪽: KDE with 신뢰구간
    - 오른쪽: Boxplot

    Args:
        data (DataFrame): 시각화할 데이터.
        column (str): 분석할 컬럼명.
        clevel (float): KDE 신뢰수준 (0~1). 기본값 0.95.
        orient (str): Boxplot 방향 ('v' 또는 'h'). 기본값 'h'.
        hue (str|None): 명목형 컬럼명. 지정하면 각 범주별로 행을 늘려 KDE와 boxplot을 그림.
        kind (str): 두 번째 그래프의 유형 (boxplot, hist). 기본값 "boxplot".
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        save_path (str|None): 저장 경로.
        callback (Callable|None): Axes 후처리 콜백.

    Returns:
        None
    """
    if isinstance(column, str):
        column = [column]

    for c in column:
        title = f"Distribution Plot of {c}"

        if hue is None:
            # 1행 2열 서브플롯 생성
            fig, axes = get_default_ax(
                width, height, rows=1, cols=2, title=title
            )

            kde_confidence_interval(
                data=data,
                xnames=c,
                clevel=clevel,
                linewidth=linewidth,
                ax=axes[0], # type: ignore
            )

            if kind == "hist":
                histplot(df=data, xname=c, linewidth=linewidth, ax=axes[1])  # type: ignore
            else:
                boxplot(
                    df=data[column], linewidth=linewidth, ax=axes[1]  # type: ignore
                )

            fig.suptitle(title, fontsize=14, y=1.02)
        else:
            if hue not in data.columns:
                raise ValueError(f"hue column '{hue}' not found in DataFrame")

            categories = list(Series(data[hue].dropna().unique()).sort_values())
            n_cat = len(categories) if categories else 1

            fig, axes = get_default_ax(
                width, height, rows=n_cat, cols=2, title=title
            )
            axes_2d = np.atleast_2d(axes)

            for idx, cat in enumerate(categories):
                subset = data[data[hue] == cat]
                left_ax, right_ax = axes_2d[idx, 0], axes_2d[idx, 1]

                kde_confidence_interval(
                    data=subset,
                    xnames=c,
                    clevel=clevel,
                    linewidth=linewidth,
                    ax=left_ax,
                )
                left_ax.set_title(f"{hue} = {cat}", fontsize=config.title_font_size, pad=config.title_pad)  # type: ignore

                if kind == "hist":
                    histplot(
                        df=subset,
                        xname=c,
                        linewidth=linewidth,
                        ax=right_ax,
                    )
                else:
                    boxplot(
                        df=subset[c], linewidth=linewidth, ax=right_ax  # type: ignore
                    )

            fig.suptitle(f"{title} by {hue}", fontsize=14, y=1.02)

            plt.tight_layout()

            if save_path:
                plt.savefig(save_path, bbox_inches="tight")

            plt.show()
            plt.close()

silhouette_plot

silhouette_plot(
    estimator,
    data,
    title=None,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
    ax=None,
)

군집분석 결과의 실루엣 플롯을 시각화함.

Parameters:

Name Type Description Default
estimator KMeans | AgglomerativeClustering

학습된 KMeans 또는 AgglomerativeClustering 군집 모델 객체.

required
data DataFrame

군집분석에 사용된 입력 데이터 (n_samples, n_features).

required
title str

플롯 제목. None이면 자동 생성.

None
width int

플롯 가로 크기 (inch 단위).

width
height int

플롯 세로 크기 (inch 단위).

height
linewidth float

기준선 등 선 두께.

line_width
save_path str

저장 경로 지정 시 파일로 저장.

None
callback Callable

추가 커스텀 콜백 함수.

None
ax Axes

기존 matplotlib Axes 객체. None이면 새로 생성.

None

Returns:

Type Description
None

None

Note
  • 각 군집별 실루엣 계수 분포를 막대그래프로 시각화
  • 군집 품질(응집도/분리도) 평가에 활용
  • 붉은색 세로선은 전체 평균 실루엣 스코어를 의미
Source code in hossam/hs_plot.py
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
def silhouette_plot(
    estimator: KMeans | AgglomerativeClustering,
    data: DataFrame,
    title: str | None = None,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None,
) -> None:
    """
    군집분석 결과의 실루엣 플롯을 시각화함.

    Args:
        estimator (KMeans | AgglomerativeClustering): 학습된 KMeans 또는 AgglomerativeClustering 군집 모델 객체.
        data (DataFrame): 군집분석에 사용된 입력 데이터 (n_samples, n_features).
        title (str, optional): 플롯 제목. None이면 자동 생성.
        width (int, optional): 플롯 가로 크기 (inch 단위).
        height (int, optional): 플롯 세로 크기 (inch 단위).
        linewidth (float, optional): 기준선 등 선 두께.
        save_path (str, optional): 저장 경로 지정 시 파일로 저장.
        callback (Callable, optional): 추가 커스텀 콜백 함수.
        ax (Axes, optional): 기존 matplotlib Axes 객체. None이면 새로 생성.

    Returns:
        None

    Note:
        - 각 군집별 실루엣 계수 분포를 막대그래프로 시각화
        - 군집 품질(응집도/분리도) 평가에 활용
        - 붉은색 세로선은 전체 평균 실루엣 스코어를 의미
    """

    outparams = False
    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    sil_avg = silhouette_score(X=data, labels=estimator.labels_)
    sil_values = silhouette_samples(X=data, labels=estimator.labels_)

    y_lower = 10

    # 클러스터링 갯수별로 fill_betweenx( )형태의 막대 그래프 표현.
    n_clusters: int = 0
    if hasattr(estimator, "n_clusters") and estimator.n_clusters is not None:   # type: ignore
        n_clusters = estimator.n_clusters  # type: ignore
    elif hasattr(estimator, "n_clusters_") and estimator.n_clusters_ is not None:    # type: ignore
        n_clusters = estimator.n_clusters_  # type: ignore
    else:
        n_clusters = len(np.unique(estimator.labels_))  # type: ignore

    for i in range(n_clusters):  # type: ignore
        ith_cluster_sil_values = sil_values[estimator.labels_ == i]  # type: ignore
        ith_cluster_sil_values.sort()  # type: ignore

        size_cluster_i = ith_cluster_sil_values.shape[0]  # type: ignore
        y_upper = y_lower + size_cluster_i

        ax.fill_betweenx(  # type: ignore
            np.arange(y_lower, y_upper),
            0,
            ith_cluster_sil_values,  # type: ignore
            alpha=0.7,
        )
        ax.text(-0.05, y_lower + 0.5 * size_cluster_i, str(i))  # type: ignore
        y_lower = y_upper + 10

    ax.axvline(x=sil_avg, color="red", linestyle="--", linewidth=linewidth)  # type: ignore

    ax.set_xlabel("The silhouette coefficient values", fontsize=config.label_font_size)  # type: ignore
    ax.set_ylabel("Cluster label", fontsize=config.label_font_size)  # type: ignore
    ax.set_xlim([-0.1, 1])  # type: ignore
    ax.set_ylim([0, len(data) + (n_clusters + 1) * 10])  # type: ignore
    ax.set_yticks([])  # type: ignore
    ax.set_xticks([0, 0.2, 0.4, 0.6, 0.8, 1])  # type: ignore

    if title is None:
        title = "Number of Cluster : " + str(n_clusters) + ", Silhouette Score :" + str(round(sil_avg, 3))  # type: ignore

    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

cluster_plot

cluster_plot(
    estimator=None,
    data=None,
    xname=None,
    yname=None,
    hue=None,
    vector=None,
    title=None,
    palette=None,
    outline=True,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    ax=None,
)

2차원 공간에서 군집분석 결과를 산점도로 시각화함.

Parameters:

Name Type Description Default
estimator KMeans

학습된 KMeans 군집 모델 객체.

None
data DataFrame

군집분석에 사용된 입력 데이터 (n_samples, n_features).

None
xname str

x축에 사용할 컬럼명. None이면 첫 번째 컬럼 사용.

None
yname str

y축에 사용할 컬럼명. None이면 두 번째 컬럼 사용.

None
hue str

군집 구분에 사용할 컬럼명. None이면 'cluster' 자동 생성.

None
vector str

벡터 종류를 의미하는 컬럼명. None이면 사용 안함.

None
title str

플롯 제목. None이면 기본값 사용.

None
palette str

색상 팔레트.

None
outline bool

외곽선 표시 여부.

True
width int

플롯 가로 크기 (inch 단위).

width
height int

플롯 세로 크기 (inch 단위).

height
linewidth float

중심점 등 선 두께.

line_width
save_path str

저장 경로 지정 시 파일로 저장.

None
ax Axes

기존 matplotlib Axes 객체. None이면 새로 생성.

None

Returns:

Type Description
None

None

Example
cluster_plot(estimator, data, xname='Sepal.Length', yname='Sepal.Width')
Note
  • 각 군집별 산점도와 중심점(빨간색 원/숫자) 표시
  • 2차원 특성 공간에서 군집 분포와 분리도 시각화
Source code in hossam/hs_plot.py
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
def cluster_plot(
    estimator: KMeans | AgglomerativeClustering | None = None,
    data: DataFrame | None = None,
    xname: str | None = None,
    yname: str | None = None,
    hue: str | None = None,
    vector: str | None = None,
    title: str | None = None,
    palette: str | None = None,
    outline: bool = True,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    ax: Axes | None = None,
) -> None:
    """
    2차원 공간에서 군집분석 결과를 산점도로 시각화함.

    Args:
        estimator (KMeans): 학습된 KMeans 군집 모델 객체.
        data (DataFrame): 군집분석에 사용된 입력 데이터 (n_samples, n_features).
        xname (str, optional): x축에 사용할 컬럼명. None이면 첫 번째 컬럼 사용.
        yname (str, optional): y축에 사용할 컬럼명. None이면 두 번째 컬럼 사용.
        hue (str, optional): 군집 구분에 사용할 컬럼명. None이면 'cluster' 자동 생성.
        vector (str, optional): 벡터 종류를 의미하는 컬럼명. None이면 사용 안함.
        title (str, optional): 플롯 제목. None이면 기본값 사용.
        palette (str, optional): 색상 팔레트.
        outline (bool, optional): 외곽선 표시 여부.
        width (int, optional): 플롯 가로 크기 (inch 단위).
        height (int, optional): 플롯 세로 크기 (inch 단위).
        linewidth (float, optional): 중심점 등 선 두께.
        save_path (str, optional): 저장 경로 지정 시 파일로 저장.
        ax (Axes, optional): 기존 matplotlib Axes 객체. None이면 새로 생성.

    Returns:
        None

    Example:
        ```python
        cluster_plot(estimator, data, xname='Sepal.Length', yname='Sepal.Width')
        ```

    Note:
        - 각 군집별 산점도와 중심점(빨간색 원/숫자) 표시
        - 2차원 특성 공간에서 군집 분포와 분리도 시각화
    """
    outparams = False
    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True

    df = data.copy() if data is not None else None  # type: ignore

    if not hue:
        df["cluster"] = estimator.labels_  # type: ignore
        hue = "cluster"

    if xname is None:
        xname = df.columns[0]  # type: ignore

    if yname is None:
        yname = df.columns[1]  # type: ignore

    xindex = df.columns.get_loc(xname)  # type: ignore
    yindex = df.columns.get_loc(yname)  # type: ignore

    def callback(ax: Axes) -> None:
        ax.set_xlabel("Feature space for the " + xname, fontsize=config.label_font_size)
        ax.set_ylabel("Feature space for the " + yname, fontsize=config.label_font_size)

        if hasattr(estimator, "cluster_centers_"):
            # 클러스터 중심점 표시
            centers = estimator.cluster_centers_  # type: ignore
            ax.scatter(  # type: ignore
                centers[:, xindex],
                centers[:, yindex],
                marker="o",  # type: ignore
                color="white",
                alpha=1,
                s=200,
                edgecolor="r",
                linewidth=linewidth,  # type: ignore
            )

            for i, c in enumerate(centers):
                ax.scatter(
                    c[xindex], c[yindex], marker="$%d$" % i, alpha=1, s=50, edgecolor="k"  # type: ignore
                )

    scatterplot(
        df=df,          # type: ignore
        xname=xname,
        yname=yname,
        hue=hue,
        vector=vector,
        title="The visualization of the clustered data." if title is None else title,
        outline=outline,
        palette=palette,
        width=width,
        height=height,
        linewidth=linewidth,
        save_path=save_path,
        callback=callback,
        ax=ax,
    )

visualize_silhouette

visualize_silhouette(
    estimator,
    data,
    xname=None,
    yname=None,
    title=None,
    palette=None,
    outline=True,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
)

군집분석 결과의 실루엣 플롯과 군집 산점도를 한 화면에 함께 시각화함.

수업에서 사용한 visualize_silhouette 함수와 동일한 기능을 수행함.

Parameters:

Name Type Description Default
estimator KMeans | AgglomerativeClustering

학습된 KMeans 또는 AgglomerativeClustering 군집 모델 객체.

required
data DataFrame

군집분석에 사용된 입력 데이터 (n_samples, n_features).

required
xname str

산점도 x축에 사용할 컬럼명. None이면 첫 번째 컬럼 사용.

None
yname str

산점도 y축에 사용할 컬럼명. None이면 두 번째 컬럼 사용.

None
title str

플롯 제목. None이면 기본값 사용.

None
palette str

색상 팔레트.

None
outline bool

산점도 외곽선 표시 여부.

True
width int

플롯 가로 크기 (inch 단위).

width
height int

플롯 세로 크기 (inch 단위).

height
linewidth float

기준선 등 선 두께.

line_width
save_path str

저장 경로 지정 시 파일로 저장.

None

Returns:

Type Description
None

None

Note
  • 실루엣 플롯(왼쪽)과 2차원 군집 산점도(오른쪽)를 동시에 확인 가능
  • 군집 품질과 분포를 한눈에 비교·분석할 때 유용
Source code in hossam/hs_plot.py
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
def visualize_silhouette(
    estimator: KMeans | AgglomerativeClustering,
    data: DataFrame,
    xname: str | None = None,
    yname: str | None = None,
    title: str | None = None,
    palette: str | None = None,
    outline: bool = True,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
) -> None:
    """
    군집분석 결과의 실루엣 플롯과 군집 산점도를 한 화면에 함께 시각화함.

    수업에서 사용한 visualize_silhouette 함수와 동일한 기능을 수행함.

    Args:
        estimator (KMeans | AgglomerativeClustering): 학습된 KMeans 또는 AgglomerativeClustering 군집 모델 객체.
        data (DataFrame): 군집분석에 사용된 입력 데이터 (n_samples, n_features).
        xname (str, optional): 산점도 x축에 사용할 컬럼명. None이면 첫 번째 컬럼 사용.
        yname (str, optional): 산점도 y축에 사용할 컬럼명. None이면 두 번째 컬럼 사용.
        title (str, optional): 플롯 제목. None이면 기본값 사용.
        palette (str, optional): 색상 팔레트.
        outline (bool, optional): 산점도 외곽선 표시 여부.
        width (int, optional): 플롯 가로 크기 (inch 단위).
        height (int, optional): 플롯 세로 크기 (inch 단위).
        linewidth (float, optional): 기준선 등 선 두께.
        save_path (str, optional): 저장 경로 지정 시 파일로 저장.

    Returns:
        None

    Note:
        - 실루엣 플롯(왼쪽)과 2차원 군집 산점도(오른쪽)를 동시에 확인 가능
        - 군집 품질과 분포를 한눈에 비교·분석할 때 유용
    """
    fig, ax = get_default_ax(rows=1, cols=2, width=width, height=height, title=title)

    silhouette_plot(
        estimator=estimator,
        data=data,
        ax=ax[0],  # type: ignore
        linewidth=linewidth,
        width=width,
        height=height
    )

    cluster_plot(
        estimator=estimator,
        data=data,
        xname=xname,
        yname=yname,
        ax=ax[1],  # type: ignore
        outline=outline,
        palette=palette,
        width=width,
        height=height
    )

    finalize_plot(ax)

dandrogram

dandrogram(
    estimator,
    p=30,
    count_sort="ascending",
    title=None,
    width=config.width,
    height=config.height,
    save_path=None,
    callback=None,
    ax=None,
)

덴드로그램 시각화

Parameters:

Name Type Description Default
estimator AgglomerativeClustering

학습된 AgglomerativeClustering 군집 모델 객체.

required
p int

덴드로그램에서 표시할 마지막 병합된 군집 수. 기본값 30.

30
count_sort str

'ascending' 또는 'descending'으로 병합 순서 정렬.

'ascending'
title str | None

그래프 제목.

None
palette str | None

팔레트 이름.

required
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
save_path str | None

저장 경로.

None
callback Callable | None

Axes 후처리 콜백.

None
ax Axes | None

외부에서 전달한 Axes. None이면 새로 생성.

None

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
def dandrogram(
    estimator: AgglomerativeClustering,
    p: int = 30,
    count_sort: Literal["ascending", "descending", False] = "ascending",
    title: str | None = None,
    width: int = config.width,
    height: int = config.height,
    save_path: str | None = None,
    callback: Callable | None = None,
    ax: Axes | None = None
) -> None:
    """덴드로그램 시각화

    Args:
        estimator (AgglomerativeClustering): 학습된 AgglomerativeClustering 군집 모델 객체.
        p (int): 덴드로그램에서 표시할 마지막 병합된 군집 수. 기본값 30.
        count_sort (str): 'ascending' 또는 'descending'으로 병합 순서 정렬.
        title (str|None): 그래프 제목.
        palette (str|None): 팔레트 이름.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        save_path (str|None): 저장 경로.
        callback (Callable|None): Axes 후처리 콜백.
        ax (Axes|None): 외부에서 전달한 Axes. None이면 새로 생성.

    Returns:
        None
    """
    # 덴드로그램을 그리기 위해 linkage 행렬 생성
    counts = np.zeros(estimator.children_.shape[0]) # type: ignore
    n_samples = len(estimator.labels_)

    for i, merge in enumerate(estimator.children_): # type: ignore
        current_count = 0
        for child_idx in merge:  # type: ignore
            if child_idx < n_samples:
                current_count += 1  # leaf node
            else:
                current_count += counts[child_idx - n_samples]
        counts[i] = current_count

        linkage_matrix = np.column_stack(
            [estimator.children_, estimator.distances_, counts]
        ).astype(float)

    outparams = False

    if ax is None:
        fig, ax = get_default_ax(width, height, 1, 1)  # type: ignore
        outparams = True


    sch_dendrogram(
        linkage_matrix,
        ax=ax,
        p=p,
        truncate_mode="lastp" if p > 0 else None,
        leaf_rotation=0,
        leaf_font_size=8,
        count_sort=count_sort,
        color_threshold=None,
        above_threshold_color="grey",
    )

    finalize_plot(ax, callback, outparams, save_path, True, title)  # type: ignore

pca_plot

pca_plot(
    estimator,
    data,
    yname=None,
    fields=None,
    hue=None,
    palette=None,
    width=config.width,
    height=config.height,
    linewidth=config.line_width,
    save_path=None,
    callback=None,
)

PCA 분석 결과에 대한 biplot 시각화

Parameters:

Name Type Description Default
estimator PCA

학습된 PCA 객체.

required
data DataFrame

PCA에 사용된 원본 데이터.

required
yname str | None

종속변수 컬럼명.

None
fields list | tuple | list[list] | tuple[list] | list[tuple] | tuple[tuple] | None

시각화할 독립변수 목록. None이면 자동 탐지.

None
hue str | None

집단 구분 컬럼명.

None
palette str | None

팔레트 이름.

None
width int

캔버스 가로 픽셀.

width
height int

캔버스 세로 픽셀.

height
linewidth float

선 굵기.

line_width
save_path str | None

저장 경로.

None
callback Callable | None

Axes 후처리 콜백.

None

Returns:

Type Description
None

None

Source code in hossam/hs_plot.py
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
def pca_plot(
    estimator: PCA,
    data: DataFrame,
    yname: str | None = None,
    fields: list | tuple | list[list] | tuple[list] | list[tuple] | tuple[tuple] | None = None,
    hue: str | None = None,
    palette: str | None = None,
    width: int = config.width,
    height: int = config.height,
    linewidth: float = config.line_width,
    save_path: str | None = None,
    callback: Callable | None = None,
) -> None:
    """
    PCA 분석 결과에 대한 biplot 시각화

    Args:
        estimator (PCA): 학습된 PCA 객체.
        data (DataFrame): PCA에 사용된 원본 데이터.
        yname (str | None): 종속변수 컬럼명.
        fields (list | tuple | list[list] | tuple[list] | list[tuple] | tuple[tuple] | None): 시각화할 독립변수 목록. None이면 자동 탐지.
        hue (str|None): 집단 구분 컬럼명.
        palette (str|None): 팔레트 이름.
        width (int): 캔버스 가로 픽셀.
        height (int): 캔버스 세로 픽셀.
        linewidth (float): 선 굵기.
        save_path (str|None): 저장 경로.
        callback (Callable|None): Axes 후처리 콜백.

    Returns:
        None
    """
    df = data.copy()
    df_columns = df.columns.tolist()

    # 종속변수가 지정되었다면 해당 컬럼 추출
    yfield = None
    if yname is not None and yname in data.columns:
        yfield = df[[yname]].copy()
        df = df.drop(columns=[yname])

    # PCA 변환 수행
    #display(df)
    score = estimator.transform(df)
    #print(score)

    # 추정기로부터 PCA 결과 데이터 프레임 생성
    pca_df = DataFrame(
        data=score,
        columns=[f"PC{i+1}" for i in range(estimator.n_components_)],
    )
    #display(pca_df)

    # 종속변수 컬럼 추가
    if yfield is not None:
        pca_df[yname] = yfield

    # 모든 컬럼명에 대한 조합 생성
    if fields is None:
        feature_cols = pca_df.columns.tolist()
        if yname is not None and yname in feature_cols:
            feature_cols.remove(yname)
        fields = list(combinations(feature_cols, 2))

    if not is_2d(fields):
        fields = [fields]   # type: ignore

    components = estimator.components_

    x_index: int = 0
    y_index: int = 0

    def __callable(ax) -> None:
        for i in range(n):
            ax.arrow(
                0,
                0,
                components[x_index, i],
                components[y_index, i],
                color="r",
                head_width=0.007,
                head_length=0.007,
                linewidth=linewidth * 0.75,
                alpha=0.75,
            )
            ax.text(
                components[x_index, i] * 1.15,
                components[y_index, i] * 1.15,
                f"{df_columns[i]} ({components[x_index, i]:.2f})",
                color="b",
                ha="center",
                va="center",
            )

        if callback is not None:
            callback(ax)

    for field_group in fields:  # type: ignore
        x_index = int(pca_df.columns.get_loc(field_group[0]))   # type: ignore
        y_index = int(pca_df.columns.get_loc(field_group[1]))   # type: ignore

        xs = score[:, x_index]
        ys = score[:, y_index]
        n = score.shape[1]

        scalex = 1.0 / (xs.max() - xs.min())
        scaley = 1.0 / (ys.max() - ys.min())

        title = "PCA Biplot"
        if field_group is not None:
            title += " - " + ", ".join(field_group)

        tdf = DataFrame({
            field_group[0]: xs * scalex,
            field_group[1]: ys * scaley,
        })

        scatterplot(
            df=tdf,
            xname=field_group[0],
            yname=field_group[1],
            hue=pca_df[hue] if hue is not None else None,
            outline=False,
            palette=palette,
            width=width,
            height=height,
            linewidth=linewidth,
            save_path=save_path,
            title=title,
            callback=__callable,
        )